eddirae 1 Posted September 7, 2015 Report Share Posted September 7, 2015 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 1 Quote Link to post Share on other sites
Ingolme 1,031 Posted September 7, 2015 Report Share Posted September 7, 2015 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(); Quote Link to post Share on other sites
eddirae 1 Posted September 7, 2015 Author Report Share Posted September 7, 2015 (edited) 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 September 7, 2015 by eddirae Quote Link to post Share on other sites
Ingolme 1,031 Posted September 8, 2015 Report Share Posted September 8, 2015 Do you know Javascript? If not, you can learn in the W3Schools Javascript tutorial. Quote Link to post Share on other sites
eddirae 1 Posted September 9, 2015 Author Report Share Posted September 9, 2015 I would love to, but I am crunched for time on this. I really need a solution on this, if you have one. Thanks!! Eddi Rae Quote Link to post Share on other sites
eddirae 1 Posted September 12, 2015 Author Report Share Posted September 12, 2015 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 Quote Link to post Share on other sites
Ingolme 1,031 Posted September 12, 2015 Report Share Posted September 12, 2015 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? Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.