Jump to content

play sound for timer1 on client side web


eddirae

Recommended Posts

Hello,

I am trying to play a sound on the client side when the timer goes down to 0.

It works on my local, but it isn't working when I place it on the server that I am running the website.

 

Here is my code.

Private Sub PlaySound()        Dim strpath As String = Server.MapPath("mp3/Ship_Brass_Bell.wav")        Dim sp_sm As SoundPlayer        sp_sm = New SoundPlayer        sp_sm.SoundLocation = strpath        sp_sm.Play()    End Sub

I don't have any reference on the web page itself.

 

Please let me know where I am going wrong with this.

 

Thanks for your help in advance,

Eddi Rae

  • Like 1
Link to comment
Share on other sites

Serverside languages can't play sounds on the client side. You should use Javascript.

In Javascript you would play a sound like this:

var sound = new Audio("mp3/Ship_Brass_Bell.wav");sound.play();
Link to comment
Share on other sites

Thanks for the input Foxy Mod. This is very helpful.

 

One question....How do I place the sound.play into the timer when the timer goes to zero? Below is the code for what I am doing.

     Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs)        Dim timer, timer_arr() As String        timer = lblTimer.Text        timer_arr = timer.Split(":")        Dim seconds As Integer = Double.Parse(timer_arr(2))        Dim minutes As Integer = Integer.Parse(timer_arr(1))        Dim hours As Integer = Integer.Parse(timer_arr(0))        If seconds = 0 And minutes = 0 And hours = 0 Then            PlaySound()            Timer1.Enabled = False            getNextLevel()            Exit Sub        Else            seconds = seconds - 1        End If        If (seconds < 0) Then            seconds = 59            minutes = minutes - 1        End If        If minutes < 0 Then            minutes = 59            hours = hours - 1        End If        lblTimer.Text = Format(hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00")    End Sub
Edited by eddirae
Link to comment
Share on other sites

Foxy Mod,

I got it to work!!

Thanks for all of your help. This is the code that I used.

   Private Sub PlaySound()        Dim scriptKey As String = "UniqueKeyForThisScript"        Dim javaScript As String = "<script type='text/javascript'>var sound = new Audio('mp3/Ship_Brass_Bell.wav');sound.play();</script>"        ClientScript.RegisterStartupScript(Me.GetType(), scriptKey, javaScript)    End Sub
Link to comment
Share on other sites

Ouch, this is a bad memory leak. Each time you call this script you're creating another copy of the same sound file in memory.

 

I guess it works as a quick fix, but you should really learn Javascript for client-side programming. Have you tested your program in Firefox or Google Chrome?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...