用 JavaScript 播放声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3402859/
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
Playing sound with JavaScript
提问by FaNIX
Doesn't matter what I do, I simply can't get this to play a sound in Firefox or IE, or Chrome for that matter.
无论我做什么,我都无法让它在 Firefox 或 IE 或 Chrome 中播放声音。
<html>
<head>
<script type="text/javascript">
function play()
{
var embed = document.createElement('object');
embed.setAttribute('src', 'c:\test.wav');
embed.setAttribute('hidden', true);
embed.setAttribute('autostart', true);
embed.setAttribute('enablejavascript', true);
document.childNodes[0].appendChild(embed);
}
// -->
</script>
</head>
<body onload="play();">
</body>
</html>
回答by Kranu
Try using this revised version of the function play()
尝试使用此函数 play() 的修订版
function play()
{
var embed=document.createElement('object');
embed.setAttribute('type','audio/wav');
embed.setAttribute('data', 'c:\test.wav');
embed.setAttribute('autostart', true);
document.getElementsByTagName('body')[0].appendChild(embed);
}
The problem with your code was you were using the src attribute, which is for the <embed> tag. Instead, use the data attribute for the <object> tag.
If you are trying to get the most compatibility out of this, you should also consider adding the embed tag as an alternate for the object tag. The way it works is like this:
您的代码的问题在于您使用了 src 属性,该属性用于 <embed> 标记。相反,使用 <object> 标记的 data 属性。
如果您想从中获得最大的兼容性,您还应该考虑添加 embed 标签作为 object 标签的替代品。它的工作方式是这样的:
<object data="test.wav" type="audio/wav" autostart="true">
<embed src="test.wav" autostart="true" alt="Could not load audio" />
</object>
This works similar to the noscript tag, where older browsers that don't support the object tag resort to the embed tag.
这类似于 noscript 标签,其中不支持 object 标签的旧浏览器求助于 embed 标签。
回答by Nosakhare Belvi
Try This Method, to play sound in all your browsers :
试试这个方法,在所有浏览器中播放声音:
function playSound(soundfile_ogg, soundfile_mp, soundfile_ma) {
if ("Audio" in window) {
var a = new Audio();
if (!!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"')
.replace(/no/, '')))
a.src = soundfile_ogg;
else if (!!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/,
'')))
a.src = soundfile_mp;
else if (!!(a.canPlayType && a.canPlayType(
'audio/mp4; codecs="mp4a.40.2"').replace(/no/, '')))
a.src = soundfile_ma;
else
a.src = soundfile_mp;
a.autoplay = true;
return;
} else {
alert("Time almost up");
}
}
to play the sound, do something like this:
要播放声音,请执行以下操作:
playSound("/file/horse.wav", "/file/horse.mp3","/file/horse.m4a");
回答by thdoan
FYI using the <embed>tag will invalidate your code. At the moment you have two choices if you want to play sounds on your web page: [1]use the method which works on most browsers today, but which will invalidate your HTML (using <embed>), or [2]use the methods which will only work in some of the latest browsers, but which will be the way to go in the future and also is considered valid HTML (using <audio>or <object>).
仅供参考,使用该<embed>标签将使您的代码无效。目前,如果您想在网页上播放声音,您有两种选择:[1]使用目前适用于大多数浏览器但会使您的 HTML 无效的方法(使用<embed>),或[2]使用将仅适用于某些最新的浏览器,但这将是未来的发展方向,并且也被认为是有效的 HTML(使用<audio>或<object>)。

