如何播放声音与JS? [英] How to play a sound with JS?
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/450033/playing-sound-notifications-using-javascript\">Playing使用Javascript声音通知?
我在JS做一个游戏,我需要一个适当的方式有事时播放声音。你如何做到这一点与JS / HTML5?我看到这的文章,但即使他们的例如,它并没有真正的工作!有时我听到了小鸟,但只有很短的时间,我不能得到它的工作了吧。
I'm making a game in JS, and I need a proper way to play a sound when something happens. How do you do that with JS/HTML5? I saw this article, but even with their example, it doesn't really work! Sometimes I do hear that bird, but only for a short time, and I can't get it to work anymore then.
推荐答案
您可以使用HTML5的&LT;音频&GT;
标签:的http://jsfiddle.net/STTnr/ 。
You can make use of the HTML5 <audio>
tag: http://jsfiddle.net/STTnr/.
var audio = document.getElementById('audio');
setTimeout(function() {
audio.play(); // play it through JavaScript after 3 seconds
}, 3000);
HTML
<audio src="something" id="audio"></audio>
只是隐藏元素本身:
Just hide the element itself:
#audio {
display: none;
}
href=\"http://html5doctor.com/native-audio-in-the-browser/\">没有浏览器支持所有格式请注意,
Do note that no browser supports all formats. This can be frustrating, but you can supply your audio in several formats.
这篇关于如何播放声音与JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!