加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:24:24 所属栏目:百科 来源:网络整理
导读:我点击按钮后想在网页上播放一些声音.这是我的代码,但它显示错误. SoundPlayer x = new SoundPlayer();x.SoundLocation = "WindowsBalloon.wav";//x.Play();x.PlaySync(); 错误: Please be sure a sound file exists at the specified location. 但该文件存
我点击按钮后想在网页上播放一些声音.这是我的代码,但它显示错误.
SoundPlayer x = new SoundPlayer();
x.SoundLocation = "WindowsBalloon.wav";
//x.Play();
x.PlaySync();

错误:

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

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

解决方法

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

原因

它将在服务器端而非客户端播放声音.

如下面的链接所述
– Problem With The C# System.Media.SoundPlayer Class On A Web Host
– What is the most “compatible” way of autoplaying sound?

>使用ASP.NET audio control.
>其他SO Answer超过同样的要求.
>使用任何其他基于Flash或Silverlight的插件.
>使用html embed标签或html5音频标签.例子可以在w3schools看到

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

>< embed> tag:< embed> tag定义外部(非HTML)内容的容器. (这是一个HTML5标记,在HTML 4中无效,但适用于所有浏览器).

<embed height="100" width="100" src="horse.mp3" />

>< object> tag:< object> tag还可以为外部(非HTML)内容定义容器.

<object height="100" width="100" data="horse.mp3"></object>

>< audio> tag:< audio> element是一个HTML5元素,但它适用于所有浏览器.

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

请注意基于html5的解决方案的问题,您必须将视频转换为不同的格式. – < audio>元素不会验证为HTML 4和XHTML. – < embed>元素不会验证为HTML 4和XHTML. – < embed>元素无法“后退”以显示错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读