6

我正在尝试找出一种方法来阻止网络音频脚本处理器节点运行,而无需断开它。

我最初的想法是将“onaudioprocess”设置为“null”以停止它,但是当我这样做时,我听到一个非常短的音频播放循环。我的猜测是音频缓冲区没有被清除或其他东西,它重复播放同一个缓冲区。

我尝试了一些其他技术,例如首先将缓冲区通道数组值全部设置为 0,然后将“onaudioprocess”设置为“null”,这仍然会产生循环的音频片段而不是静音。

我有一些类似下面的代码(咖啡脚本)

context = new webkitAudioContext()
scriptProcessor = context.createScriptProcessor()

scriptProcessor.onaudioprocess = (e)->
  outBufferL = e.outputBuffer.getChannelData(0)
  outBufferR = e.outputBuffer.getChannelData(1)
  i = 0
  while i < bufferSize
    outBufferL[i] = randomNoiseFunc()
    outBufferR[i] = randomNoiseFunc()
    i++
  return null
return null

然后当我想停止它时

stopFunc1: ->
  scriptProcessor.onaudioprocess = null

我还尝试将缓冲区通道数组设置为 0,然后将回调设置为 null

stopFunc2: ->
  scriptProcessor.onaudioprocess = (e)->
    outBufferL = e.outputBuffer.getChannelData(0)
    outBufferR = e.outputBuffer.getChannelData(1)
    i = 0
    while i < bufferSize
      outBufferL[i] = 0
      outBufferR[i] = 0
      i++
    scriptProcessor.onaudioprocess = null
    return null
  return null

这两种技术都会产生一段非常快速循环的音频,而不是没有音频。

有没有办法正确地做到这一点,还是我只是想错了?

非常感谢任何帮助。

4

1 回答 1

4

Maybe I'm misunderstanding...

But if you null out onaudioprocess, it won't stop playback immediately unless you just happen to hit it at the very end of the current buffer.

Let's say your bufferSize is 2048, and you happen to null out onaudioprocess half-way through the current buffer duration of 46ms (2048 / 44100 * 1000). You're still going to have the another 23ms of audio that's already been processed by your ScriptProcessor before you nulled it out.

Best bet is probably to throw a gain node into the path and just mute it on-demand.

于 2013-07-15T17:38:12.817 回答