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

Threaded View

  1. #1
    Join Date
    Feb 2003

    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.

    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
            FileOpen(FFile,Global.MyRootFolder & "playsound.htm",OpenMode.Input)
            Do While Not Eof(FFile)
                Temp= LineInput(FFile)
                All += Temp
            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 -

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

On-Demand Webinars (sponsored)