如何在asp.net网页中播放声音?

时间:2021-09-22 15:16:55

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.

但该文件存在于我的项目中,我确信该地址是正确的。

8 个解决方案

#1


10  

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

您无法使用System.Media.Soundplayer类在网页上播放文件!

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 ?

如下面链接所述 - Web主机上的C#System.Media.SoundPlayer类问题 - 自动播放声音的最“兼容”方式是什么?

Solution

  • Use ASP.NET audio control.
  • 使用ASP.NET音频控件。

  • Other SO Answer over this same requirements.
  • 其他SO回答同样的要求。

  • Use Any other Flash or Silverlight based plugins.
  • 使用任何其他基于Flash或Silverlight的插件。

  • Use html embed tag or html5 audio tag. Examples can be seen on w3schools
  • 使用html embed标签或html5音频标签。在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).
  • 标签:标签定义外部(非HTML)内容的容器。 (它是HTML5标记,在HTML 4中无效,但适用于所有浏览器)。

<embed height="100" width="100" src="horse.mp3" />
  • <object> tag: The <object> tag can also define a container for external (non-HTML) content.
  • 标记:标记还可以为外部(非HTML)内容定义容器。

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

请注意基于html5的解决方案的问题,您必须将视频转换为不同的格式。 -

#2


5  

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

您需要使用 html标记。

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

Or HTML5 tag

或者HTML5标签

<audio src="WindowsBalloon.wav">
  <p>Your browser does not support the audio element.</p>
</audio>

#3


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.

返回与Web服务器上指定虚拟路径对应的物理文件路径。

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

参数:path:Web服务器的虚拟路径。返回:与路径对应的物理文件路径。

SoundPlayer s = new SoundPlayer();<br>
s.SoundLocation = **Server.MapPath("WindowsBalloon.wav");**<br>
s.PlaySync();

#4


0  

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

给定完整路径,即c:\ wavfiles \ WindowsBalloon.wav

'wavfiles' above is a user privileged folder.

上面的'wavfiles'是用户特权文件夹。

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

#5


0  

This works in HTML5 :

这适用于HTML5:

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("<embed height='0' width='0' src='Sound.wav' />");
}

#6


0  

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

如果您需要以编程方式播放ALARM声音,可以这样做:

<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):

代码背后(visual basic):

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

Code behind (C#):

代码背后(C#):

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

#7


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

尝试将驱动器号添加到路径,例如“C:/WindowsBalloon.wav”。但这不会在客户端播放。我建议客户端尝试HTML5。

#8


-3  

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

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

#1


10  

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

您无法使用System.Media.Soundplayer类在网页上播放文件!

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 ?

如下面链接所述 - Web主机上的C#System.Media.SoundPlayer类问题 - 自动播放声音的最“兼容”方式是什么?

Solution

  • Use ASP.NET audio control.
  • 使用ASP.NET音频控件。

  • Other SO Answer over this same requirements.
  • 其他SO回答同样的要求。

  • Use Any other Flash or Silverlight based plugins.
  • 使用任何其他基于Flash或Silverlight的插件。

  • Use html embed tag or html5 audio tag. Examples can be seen on w3schools
  • 使用html embed标签或html5音频标签。在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).
  • 标签:标签定义外部(非HTML)内容的容器。 (它是HTML5标记,在HTML 4中无效,但适用于所有浏览器)。

<embed height="100" width="100" src="horse.mp3" />
  • <object> tag: The <object> tag can also define a container for external (non-HTML) content.
  • 标记:标记还可以为外部(非HTML)内容定义容器。

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

请注意基于html5的解决方案的问题,您必须将视频转换为不同的格式。 -

#2


5  

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

您需要使用 html标记。

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

Or HTML5 tag

或者HTML5标签

<audio src="WindowsBalloon.wav">
  <p>Your browser does not support the audio element.</p>
</audio>

#3


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.

返回与Web服务器上指定虚拟路径对应的物理文件路径。

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

参数:path:Web服务器的虚拟路径。返回:与路径对应的物理文件路径。

SoundPlayer s = new SoundPlayer();<br>
s.SoundLocation = **Server.MapPath("WindowsBalloon.wav");**<br>
s.PlaySync();

#4


0  

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

给定完整路径,即c:\ wavfiles \ WindowsBalloon.wav

'wavfiles' above is a user privileged folder.

上面的'wavfiles'是用户特权文件夹。

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

#5


0  

This works in HTML5 :

这适用于HTML5:

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("<embed height='0' width='0' src='Sound.wav' />");
}

#6


0  

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

如果您需要以编程方式播放ALARM声音,可以这样做:

<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):

代码背后(visual basic):

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

Code behind (C#):

代码背后(C#):

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

#7


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

尝试将驱动器号添加到路径,例如“C:/WindowsBalloon.wav”。但这不会在客户端播放。我建议客户端尝试HTML5。

#8


-3  

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

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