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

时间:2015-03-21 16:27:38

标签: javascript audio

var shots = Math.ceil(Math.random(0,1)*5);
var snd = new Audio("test.wav");

for (var i = 0; i < shots; i++){
snd.play();
}

现在,根据拍摄的“镜头”数量,我想播放test.wav相同的次数,其间有一个随机的,非常短的延迟,甚至可能“重叠”的声音效果相互影响

我最好怎么做呢? - 谢谢

1 个答案:

答案 0 :(得分:0)

我会这样做...

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
}
相关问题