audio.play() 和多个效果播放

audio.play() and more than one effect playback?

本文关键字:播放 play audio      更新时间:2023-09-26
var shots = Math.ceil(Math.random(0,1)*5);
var snd = new Audio("test.wav");
for (var i = 0; i < shots; i++){
snd.play();
}
现在,根据发射的

"镜头"数量,我想播放相同的次数.wav中间有随机的、非常短的延迟,甚至可能相互"重叠"的声音效果。

我该如何最好地做到这一点?-谢谢

我会这样做...

var shots = Math.ceil(Math.random()*5), // Math.random() --> always 0-1 range
    src = 'http://upload.wikimedia.org/wikipedia/en/f/f9/Beatles_eleanor_rigby.ogg',  // change to your source
    maxStartTime = 1000; 
for(var i=0;i<shots;i++){
    setTimeout(function(){
        (new Audio(src)).play();
    }, 100 + maxStartTime*(Math.random())); // again you can calibrate the timeout value to what suits you best
}