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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12329191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:54:04  来源:igfitidea点击:

How do I play a sound in an asp.net web page?

c#asp.net

提问by Hadi Nemati

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.

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

回答by adatapost

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

您需要使用<object><embed>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>

回答by hagensoft

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。

回答by Harsh Baid

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

您不能使用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

解决方案

  • Other SO Answerover 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
  • 针对相同要求的其他SO 答案
  • 使用任何其他基于 Flash 或 Silverlight 的插件。
  • 使用 html 嵌入标签或 html5 音频标签。例子可以在w3schools上看到

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

基于 Html5 的音频解决方案(仅适用于现代浏览器)

  • <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>标签:<embed>标签定义了外部(非 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.
  • <object>标签:<object>标签还可以为外部(非 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>tag:该<audio>元素是一个 HTML5 元素,在 HTML 4 中无效,但在所有浏览器中都有效。
<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 的解决方案的问题,您必须将视频转换为不同的格式。
- 该<audio>元素未验证为 HTML 4 和 XHTML。
- 该<embed>元素未验证为 HTML 4 和 XHTML。
-<embed>元素不能“回退”以显示错误。

回答by Ishwar Singh

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

回答by reza

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

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

回答by reza

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 服务器的虚拟路径。
返回: path 对应的物理文件路径。

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

回答by mrbengi

This works in HTML5:

这适用于HTML5

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

回答by Bruno Picardi

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

代码隐藏(视觉基础):

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");