为什么我不能使用HTML5音频标签多次播放声音? [英] Why can't I play sounds more than once using HTML5 audio tag?
问题描述
这就是存储声音的方式:
This is how the sound is "stored":
<audio id = "hammer" src="res/sounds/Building/hammer.wav"></audio>
在实际游戏中(我使用的是声音),我用它来播放声音:
In the actual game (which I use sounds for), I use this to play the sound:
function playSound(soundid)
{
document.getElementById(soundid).currentTime = 0;
document.getElementById(soundid).play();
}
但声音只是第一次播放,而且再也不会播放!
我尝试将控制台中的currentTime重置为0,但我确实得到了这个:
But the sound plays only the first time, and never again! I tried resetting the "currentTime" in the console to 0, but I literally get this:
>document.getElementById("hammer").currentTime = 0
0
>document.getElementById("hammer").currentTime
0.340...
为什么会发生这种情况,如何解决?
Why is this happening, how do I fix it?
推荐答案
请参阅此幻灯片以及 Evan Wallace和Justin Ardini关于html5游戏开发的演示。
为了制作一些精彩游戏的所有资源,他们的一部分话题是: http://madebyevan.com/gamedevclass/
For all the resources to make some awesome games, part of their talk: http://madebyevan.com/gamedevclass/
音频仍然不能始终如一地工作l浏览器,截至目前:
Audio still doesn't work consistently across all browsers, as of right now:
- 元素必须在Chrome中重新加载,否则只会播放一次
- 不得在Firefox中重新加载元素,否则会有延迟
function playSoundEvent() {
if (window.chrome) elems[index].load()
elems[index].play()
index = (index + 1) % elems.length
}
这篇关于为什么我不能使用HTML5音频标签多次播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!