HTML5音频多重播放

问题描述:

我正在编写Javascript游戏,我想多次使用一个声音。我可以将一次声音加载到一个数组。但是我想加载一次声音并将其“复制”到阵列中,以便我可以多次播放它。这是什么方法我现在有:HTML5音频多重播放

this.list = [ 
     "LaserShot", //http://www.freesound.org/samplesViewSingle.php?id=39459 
     "Ding", //http://www.freesound.org/samplesViewSingle.php?id=5212 
     "Rocket" //http://www.freesound.org/samplesViewSingle.php?id=47252 -> http://creativecommons.org/licenses/sampling+/1.0/ 
    ]; 

... 

for (i in this.list) { 
     this.sounds[this.list[i]] = new Array(); 

     for (var j = 0; j < this.channels; j++) { 
      this.sounds[this.list[i]][j] = new Audio("./sounds/"+ this.list[i] + type); 
     } 
    } 

我只是想做到这一点:

for (i in this.list) { 
     this.sounds[this.list[i]] = new Array(); 

var tempAudio = new Audio("./sounds/"+ this.list[i] + type); 

     for (var j = 0; j < this.channels; j++) { 
      this.sounds[this.list[i]][j] = realCopyOfTempAudio; 
     } 
    } 

太谢谢你了。

我的经验:

最好是具有相同源创造更多的音频html标签。我是js的粉丝,但这次最好有html格式的html音频标签。

我做了一个重复的音频标签,我装饰了我的需求。如果您想在一秒钟内多次播放相同的声音,请添加更多克隆。

还可以修复自动播放的bug (而不是EXE_JUST_ONE_TIME可以使用替代click事件,现在并不重要):

<audio controls id="LaserShot" > 
<source src="LaserShot.mp3" type="audio/mpeg"> 
<source src="LaserShot.ogg" type="audio/ogg"> 
</audio> 
<audio controls id="LaserShot_CLONE" > 
<source src="LaserShot.mp3" type="audio/mpeg"> 
<source src="LaserShot.ogg" type="audio/ogg"> 
</audio> 

VAR EXE_JUST_ONE_TIME = FALSE;

document.addEventListener( “点击”,功能(E){

if (EXE_JUST_ONE_TIME == false){ 
EXE_JUST_ONE_TIME = true; 

document.getElementById("LaserShot").play(); 
document.getElementById("LaserShot").pause(); 
// now you can play programmability from js 
document.getElementById("LaserShot_CLONE").play(); 
document.getElementById("LaserShot_CLONE").pause(); 

} 

}

最后一部分(需要处理)这个处理程序仅适用于一个克隆:

var play_SHOT = function(){ 

if (document.getElementById('LaserShot').duration > 0 && !document.getElementById('LaserShot').paused) { 

document.getElementById('LaserShot_CLONE').play(); 

} 
else { 

document.getElementById('LaserShot').play(); 

} 

} 
+0

感谢您回答我五年前的问题,我会在有空的时候尝试一下,并在问题得到解决后将其标记为已接受。 – 2016-02-18 14:39:40