Guide to play sound in web page
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Guide to play sound in web page

  1. #1
    Join Date
    Feb 2003
    Location
    Greece
    Posts
    529

    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:

    Code:
    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.

    Code:
    Public Shared MyRootFolder As String
    Public Shared DiskPathThatKeepsMyMediaFiles As String
    Include the following line in the Application_Start event

    Code:
        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.

    Code:
    Public Const URLThatKeepsMyMediaFiles = "http://www.my-site.com/mp3/"
    Continuing in the module code:

    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:

    Code:
    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).
    Attached Files Attached Files
    Last edited by dtv; December 8th, 2007 at 02:56 AM.
    - Better live in the digital world -

  2. #2
    Join Date
    Feb 2003
    Location
    Greece
    Posts
    529

    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:

    Code:
    obj.Text = htmlSound.Replace("$MEDIA_URL$",URLThatKeepsMyMediaFiles & MediaFile)
    and also no need for the file playsound.htm to exist in your app's root folder.

    There is a demo of this technique in a game with questions i made :

    http://www.deltatv.gr/games/questions.aspx

    A sound is played when ever a user selects an answer (correct or wrong).

    Ok its greeks, sorry for that

    p/s
    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 05:19 AM.
    - Better live in the digital world -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center