8

I want to play some sounds in my web page once I click a button. This is my code but it shows an error.

SoundPlayer x = new SoundPlayer();
x.SoundLocation = "WindowsBalloon.wav";
//x.Play();
x.PlaySync();

error:

Please be sure a sound file exists at the specified location.

but the file exists in my project and I'm sure that the address is correct.

Draken
  • 3,134
  • 13
  • 34
  • 54
Hadi Nemati
  • 547
  • 3
  • 11
  • 23
  • 2
    `"WindowsBalloon.wav"` is a relative path, make it absolute `"c:\\WindowsBalloon.wav"`.where do you expect to hear the sound? on the server or the client.your example will play the sound on the server. – mo. Sep 08 '12 at 08:29

8 Answers8

12

You cannot play a file on a web page using the System.Media.Soundplayer class !!!

Reason

It will play sound on server-side not client-side.

As mentioned as in below links
- Problem With The C# System.Media.SoundPlayer Class On A Web Host
- What is the most “compatible” way of autoplaying sound ?

Solution

  • Other SO Answer over this same requirements.
  • Use Any other Flash or Silverlight based plugins.
  • Use html embed tag or html5 audio tag. Examples can be seen on w3schools

Html5-based audio solutions (works on modern browsers only)

  • <embed> tag: The <embed> tag defines a container for external (non-HTML) content. (It is an HTML5 tag, invalid in HTML 4, but works in all browsers).
<embed height="100" width="100" src="horse.mp3" />
  • <object> tag: The <object> tag can also define a container for external (non-HTML) content.
<object height="100" width="100" data="horse.mp3"></object>
  • <audio> tag: The <audio> element is an HTML5 element, invalid in HTML 4, but it works in all browsers.
<audio controls="controls" height="100" width="100">
  <source src="horse.mp3" type="audio/mp3" />
  <source src="horse.ogg" type="audio/ogg" />
  <embed height="100" width="100" src="horse.mp3" />
</audio>

Please note the problems with html5-based solutions you must convert your videos to different formats.
- The <audio> element does not validate as HTML 4 and XHTML.
- The <embed> element does not validate as HTML 4 and XHTML.
- The <embed> element cannot "fall-back" to display an error.

Ryan Taite
  • 789
  • 12
  • 37
Harsh Baid
  • 7,199
  • 5
  • 48
  • 92
5

You need to use <object> or <embed> html tags.

<object data="WindowsBalloon.wav"></object>

Or HTML5 tag

<audio src="WindowsBalloon.wav">
  <p>Your browser does not support the audio element.</p>
</audio>
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • 1
    What about non HTML5 browsers like IE8? is flash the only option then? –  Sep 08 '12 at 08:57
2

This works in HTML5 :

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("<embed height='0' width='0' src='Sound.wav' />");
}
Bengi Besçeli
  • 3,638
  • 12
  • 53
  • 87
1

This is what I think you want:

Server.MapPath(string path);

Returns the physical file path that corresponds to the specified virtual path on the Web server.

Parameters: path: The virtual path of the Web server.
Returns: The physical file path that corresponds to path.

SoundPlayer s = new SoundPlayer();<br>
s.SoundLocation = **Server.MapPath("WindowsBalloon.wav");**<br>
s.PlaySync();
gipinani
  • 14,038
  • 12
  • 56
  • 85
reza
  • 11
  • 1
0

Given full path i.e. c:\wavfiles\WindowsBalloon.wav

'wavfiles' above is a user privileged folder.

use x.PlayLooping()

function if you want to play sound file continuously

BE CAREFUL!

use one button to exit loop else sound file will run continuously. I suggest you to exit the loop: -

Code

 Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        x.Stop()
    End Sub
doptimusprime
  • 9,115
  • 6
  • 52
  • 90
0

If you need to play an ALARM sound programmatically you can do it this way:

<asp:Panel runat="server" ID="panBuzz" style="visibility:hidden">
   <audio runat="server" id="Buzz"  src="http://.....mp3" type="audio/mp3"/>
</asp:Panel>

Code behind (visual basic):

Dim cBuzz As HtmlControl = DirectCast(panBuzz.FindControl("Buzz"), HtmlControl)
cBuzz.Attributes.Add("autoplay", "autoplay")

Code behind (C#):

HtmlControl cBuzz = (HtmlControl)panBuzz.FindControl("Buzz");
cBuzz.Attributes.Add("autoplay", "autoplay");
-1

try adding the drive letter to the path, such as "C:/WindowsBalloon.wav". But this would not play it on the client side. I would recomend trying HTML5 for the client side.

hagensoft
  • 1,497
  • 13
  • 13
-4

SoundPlayer s = new SoundPlayer();
s.SoundLocation = Server.MapPath("WindowsBalloon.wav");
s.PlaySync();

reza
  • 1