【问题标题】:Play an AudioBufferSourceNode twice?播放 AudioBufferSourceNode 两次?
【发布时间】:2012-03-15 10:11:31
【问题描述】:

我应该能够使用同一个AudioBufferSourceNode 多次播放声音吗?出于某种原因,第二次调用 noteGrainOn 不会播放音频,即使有介入的 noteOff

这段代码只播放一次声音:

var node = audioContext.createBufferSource()
node.buffer = audioBuffer
node.connect(audioContext.destination)

var now = audioContext.currentTime
node.noteGrainOn(now, 0, 2)
node.noteOff(now + 2)
node.noteGrainOn(now + 3, 0, 2)
node.noteOff(now + 5)

【问题讨论】:

    标签: javascript web-audio-api


    【解决方案1】:

    一旦你回放了一个源节点,你就不能再使用它了。您需要使用相同的缓冲区创建另一个 AudioBufferSourceNode。查看Web Audio FAQ 了解更多信息(请参阅 noteOn() 问题)。

    【讨论】:

    • 感谢您的回答和常见问题解答。
    • 正如你所说,这似乎不可能html5rocks.com/en/tutorials/webaudio/intro/#toc-volume 需要重新加载。我也无法使用当前时间重新加载 // play = function() { source.noteOn(now); }, pause = function() { now = audioContext.currentTime; source.noteOff(0); }
    【解决方案2】:

    我有一些技巧可以轻松切换播放/暂停 :) 您可以直接将音频映射到现有的音频标签并使用其上的控件

    <audio id="sound" src="./test.mp3" controls="controls"></audio>
    
    var audioElement = document.getElementById('sound');
    source = audioContext.createMediaElementSource(audioElement);
    /* ... */
    audioElement.pause();
    

    我在这里运行的示例:http://www.b2bweb.fr/bonus/Braincer/ 我目前使用 Chrome build 18.0.xxxx

    其他参考:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多