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文章,但是即使使用了example,它也不是很有效!有时我确实听到那只鸟的声音,但是只是一小段时间,那时我再也听不见它的声音了。
最佳答案
您可以使用HTML5的<audio>
标签:http://jsfiddle.net/STTnr/。
var audio = document.getElementById('audio');
setTimeout(function() {
audio.play(); // play it through JavaScript after 3 seconds
}, 3000);
<audio src="something" id="audio"></audio>
#audio {
display: none;
}