本文介绍了如何在asp.net网页上播放声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击按钮后,我想在网页上播放一些声音.这是我的代码,但显示错误.

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

错误:

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

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

推荐答案

您不能使用 System.Media.Soundplayer 类!!!

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

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

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

如以下链接中所述
- Web主机上的C#System.Media.SoundPlayer类存在问题
-什么是最兼容"的自动播放声音?

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 ?

  • 其他 SO答复.
  • 使用任何其他基于Flash或Silverlight的插件.
  • 使用html嵌入标签或html5音频标签.可以在 w3schools
  • 上看到示例
  • 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
  • <embed>标记:<embed>标记定义用于外部(非HTML)内容的容器. (这是一个HTML5标记,在HTML 4中无效,但在所有浏览器中均可使用).
  • <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>标签:<object>标签还可以定义用于外部(非HTML)内容的容器.
    • <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>标记:<audio>元素是一个HTML5元素,在HTML 4中无效,但是在所有浏览器中都可以使用.
        • <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>
          

          请注意基于html5的解决方案存在的问题,您必须将视频转换为其他格式.
          -<audio>元素未验证为HTML 4和XHTML.
          -<embed>元素未验证为HTML 4和XHTML.
          -<embed>元素不能后退"以显示错误.

          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.

          这篇关于如何在asp.net网页上播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:25