dtv
December 7th, 2007, 03:18 AM
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:
lblSound.Text=""
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.
Public Shared MyRootFolder As String
Public Shared DiskPathThatKeepsMyMediaFiles As String
Include the following line in the Application_Start event
MyRootFolder = Server.MapPath("") & "\"
DiskPathThatKeepsMyMediaFiles = MyRootFolder & "mp3\"
Place the following code in a public module:
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.
Public Const URLThatKeepsMyMediaFiles = "http://www.my-site.com/mp3/"
Continuing in the module code:
Public Sub PlaySound(ByRef obj As Label,ByVal MediaFile As String)
If Not IO.File.Exists(Global.DiskPathThatKeepsMyMediaFiles & MediaFile) Then Exit Sub
obj.Text =htmlSound(MediaFile)
End Sub
And final, here is the "htmlSound" declaration:
Public Function htmlSound(ByVal MediaFile As String) As String
Dim FFile As Integer = FreeFile
Dim Temp As String,All As String
Try
FileOpen(FFile,Global.MyRootFolder & "playsound.htm",OpenMode.Input)
Do While Not Eof(FFile)
Temp= LineInput(FFile)
All += Temp
Loop
FileClose(FFile)
Return All.Replace("$MEDIA_URL$",URLThatKeepsMyMediaFiles & MediaFile)
Catch : End Try
End Function
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.
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).
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:
lblSound.Text=""
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.
Public Shared MyRootFolder As String
Public Shared DiskPathThatKeepsMyMediaFiles As String
Include the following line in the Application_Start event
MyRootFolder = Server.MapPath("") & "\"
DiskPathThatKeepsMyMediaFiles = MyRootFolder & "mp3\"
Place the following code in a public module:
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.
Public Const URLThatKeepsMyMediaFiles = "http://www.my-site.com/mp3/"
Continuing in the module code:
Public Sub PlaySound(ByRef obj As Label,ByVal MediaFile As String)
If Not IO.File.Exists(Global.DiskPathThatKeepsMyMediaFiles & MediaFile) Then Exit Sub
obj.Text =htmlSound(MediaFile)
End Sub
And final, here is the "htmlSound" declaration:
Public Function htmlSound(ByVal MediaFile As String) As String
Dim FFile As Integer = FreeFile
Dim Temp As String,All As String
Try
FileOpen(FFile,Global.MyRootFolder & "playsound.htm",OpenMode.Input)
Do While Not Eof(FFile)
Temp= LineInput(FFile)
All += Temp
Loop
FileClose(FFile)
Return All.Replace("$MEDIA_URL$",URLThatKeepsMyMediaFiles & MediaFile)
Catch : End Try
End Function
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.
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).