2011-03-04 254 views
2

我正在編寫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; 
     } 
    } 

太謝謝你了。

回答

0

我的經驗:

最好是具有相同源創造更多的音頻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