Javascript 如何给网页添加背景音乐?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6529645/
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
How to add background music to a web page?
提问by zhuanzhou
How do I add background music to a web page? So that when the visitor opens the page, the music will auto play.
如何在网页中添加背景音乐?这样当访问者打开页面时,音乐会自动播放。
I have tried <object>
<embed>
and <bgsound>
but they are all not working in firefox. Why?
我试过<object>
<embed>
,<bgsound>
但它们都不能在 Firefox 中工作。为什么?
采纳答案by Devin Burke
The <bgsound>
tag is Internet Explorer-specific and will thus not work in other browsers such as FireFox. The <embed>
tag should work in FireFox if you use it correctly. It will use a browser plug-in to play the sound. Below is an example:
该<bgsound>
标签是特定于 Internet Explorer 的,因此无法在其他浏览器(如 FireFox)中使用。<embed>
如果您正确使用该标签,它应该可以在 FireFox 中使用。它将使用浏览器插件来播放声音。下面是一个例子:
<embed loop="true" src="sound.wav" hidden="true" type="video/quicktime"></embed>
loop="true"
specifies to play the sound repeatedly.src="sound.wav"
specifies the relative path of the sound file to play. The variety of formats you can play depends on whattype=
you specify.hidden="true"
indicates to not show the media player's interface. Hide it if you want the user to not be able to pause, stop, or navigate through the sound.type="video/quicktime"
specifies to use a Quicktime component, which means the client must have Quicktime installed. Useapplication/x-mplayer2
for Windows Media Player oraudio/x-pn-realaudio-plugin
for Real Player audio. Quicktime plays more formats and is probably what you will want to use.
loop="true"
指定重复播放声音。src="sound.wav"
指定要播放的声音文件的相对路径。您可以播放的各种格式取决于type=
您指定的内容。hidden="true"
表示不显示媒体播放器的界面。如果您希望用户无法暂停、停止或浏览声音,请隐藏它。type="video/quicktime"
指定使用 Quicktime 组件,这意味着客户端必须安装 Quicktime。使用application/x-mplayer2
Windows媒体播放器或audio/x-pn-realaudio-plugin
用于实时播放音频。Quicktime 播放更多格式,可能是您想要使用的格式。
Alternatively, use <object>
in a very similar way. An example is below:
或者,<object>
以非常相似的方式使用。一个例子如下:
<object data="sound.wav" type="video/quicktime" width="0" height="0">
<param name="filename" value="sound.wav">
<param name="autostart" value="1">
<param name="playcount" value="true">
</object>
Keep in mind that, like the <marquee>
tag, background sound on a web page is generally frowned upon because it is often obtrusive and annoying. Also, as the user switches between pages or causes post-backs, the sound will restart from the beginning. Only use audio formats that are highly compressed, meaning they have small file sizes, or the sound will not play for several seconds while it downloads to the client machine.
请记住,与<marquee>
标签一样,网页上的背景声音通常不受欢迎,因为它通常是突兀和烦人的。此外,当用户在页面之间切换或导致回传时,声音将从头开始重新播放。仅使用高度压缩的音频格式,这意味着它们具有较小的文件大小,否则在下载到客户端计算机时声音将不会播放几秒钟。
回答by XeeMez AsHu
The embedding method places a media playerin your page. Here's the most basic version of the code:
嵌入方法将媒体播放器放置在您的页面中。这是代码的最基本版本:
<audio controls="controls"><source src="SoundFile.mp3" type="audio/mpeg" /></audio>
The embedded player looks like this:
嵌入式播放器如下所示:
If you would prefer not to show the player(and give the user no control), use this code:
如果您不想显示播放器(并且不让用户控制),请使用以下代码:
<audio><source src="SoundFile.mp3" type="audio/mpeg" /></audio>
For more information and options such as autoplay, see HTML5 audio.
有关更多信息和选项(例如自动播放),请参阅HTML5 音频。
回答by Nazren Naz
To play audio and display the standard controls:
播放音频和显示标准控件:
<audio autoplay="autoplay" controls="controls">
<source src="http://play.onet4u.com/nazrenz.mp3" />
</audio>
Or to hide the controls:
或者隐藏控件:
<audio autoplay="autoplay">
<source src="http://play.onet4u.com/nazrenz.mp3" />
</audio>