Web Audio API延迟后停止

时间:2014-05-16 15:57:48

标签: javascript html5 web-audio

我有这个片段播放音调。我将stop()功能的延迟设置为5秒。它在第一次调用时有效。但任何后续调用的延迟都没有发生 - 它只是在time1到期后立即停止。

有什么建议是什么问题?

function playSound() {
    var mySource = myAudioContext.createOscillator();
    var myGain = myAudioContext.createGainNode();

    mySource.frequency.value = 261.625;
    mySource.connect(myGain);
    myGain.gain.value = 1.0;
    myGain.connect(myAudioContext.destination);

    mySource.start(0);
    setTimeout(function(s) {
               mySource.stop(5);   //stop after 5 sec. only works for the first call
               }, time1, mySource);

}

1 个答案:

答案 0 :(得分:3)

从现在开始n秒后,

stop(n)不生效。“这是一个绝对的时间 - 时间从零开始,所以它第一次出现[*]。

请改用:

mySource.stop( 5 + myAudioContext.currentTime );

[*]这实际上也依赖于Webkit / Blink中的一个错误,我们在创建第一个节点之前不会开始运行时间;它应该在创建AudioContext时启动。