December 7th, 2007, 03:18 AM
Guide to play sound in web page
This is my way of playing a sound in a aspx page, and I want to shared it with you. Its so easy as it can be.
First of all, from the time hspc recommended Anthen AJAX controls, almost all of controls are replaced from the related Anthem controls, in order to replace ugly postbacks with smart callbacks (when it needed), but thats not a necessery thing to do in your page if there is no reason.
In your page include a label control. It must be VISIBLE, with TEXT property empty. Lets name this label as "lblSound". This label is for playing actually the sound and dont worry, it wont be visible cause the embedded WMP code will have zero width and height as you will se below.
In your Page_Load event, at the end of sub, place this line:
This will make sure that only once your sound will be played, if you ordered by any other event.
The following lines needs to be modified in GLOBAL.ASAX to fit your needs.
Include the following line in the Application_Start event
Public Shared MyRootFolder As String
Public Shared DiskPathThatKeepsMyMediaFiles As String
Place the following code in a public module:
MyRootFolder = Server.MapPath("") & "\"
DiskPathThatKeepsMyMediaFiles = MyRootFolder & "mp3\"
The following line needs attention and must be modified to fit your server paths.It must be not a related URL, but including the http prefix. A complete URL that is.
Continuing in the module code:
Public Const URLThatKeepsMyMediaFiles = "http://www.my-site.com/mp3/"
And final, here is the "htmlSound" declaration:
Public Sub PlaySound(ByRef obj As Label,ByVal MediaFile As String)
If Not IO.File.Exists(Global.DiskPathThatKeepsMyMediaFiles & MediaFile) Then Exit Sub
Type PlaySound(lblSound,"correct_answer.mp3") for example in any of your postback or callback events in your page code. The sound will be played once. Please keep your media files (mp3 or wav recommended) in the lowest possible size, about 2-5kb is very good and fast to load. Before uploading your mp3, save it via a sound editor in 32kbs, 22Khz, mono format, its ok for an internet sound.
Public Function htmlSound(ByVal MediaFile As String) As String
Dim FFile As Integer = FreeFile
Dim Temp As String,All As String
FileOpen(FFile,Global.MyRootFolder & "playsound.htm",OpenMode.Input)
Do While Not Eof(FFile)
All += Temp
Return All.Replace("$MEDIA_URL$",URLThatKeepsMyMediaFiles & MediaFile)
Catch : End Try
Enjoy. Dont forget to include the attached file in your applications web root, after you renaming it to .htm and not .txt. (includes the WMP embedded tag).
Last edited by dtv; December 8th, 2007 at 01:56 AM.
- Better live in the digital world -
December 7th, 2007, 03:26 AM
Re: Guide to play sound in web page
In the above code, its not a good thing to do to load PLAYSOUND.HTM (function htmlSound) everytime a sound needs to be played. You can replace the htmlSound function with a public constant named htmlSound with the file's content as a continued string, without the Chr(13) and Chr(10), just one line. Be carefull with the " characters, it needs to be doubled in vb.net's environment.
If you do that, then the following line must be replace in sub PlaySound as following:
and also no need for the file playsound.htm to exist in your app's root folder.
obj.Text = htmlSound.Replace("$MEDIA_URL$",URLThatKeepsMyMediaFiles & MediaFile)
There is a demo of this technique in a game with questions i made :
A sound is played when ever a user selects an answer (correct or wrong).
Ok its greeks, sorry for that
if you are going to replace the lblSound object with a Anthem label control and not a asp one, be sure to set the AutoUpdateAfterCallback property to TRUE.
Last edited by dtv; December 7th, 2007 at 04:19 AM.
- Better live in the digital world -
Click Here to Expand Forum to Full Width