在我的对象中,我有一个AudioBufferSourceNode
对象this.bSrc
(this.audioDestination
AudioBuffer
this.audioBuffer
如果this.audioDestination
连接到音频上下文最终目的地并且我在我的对象 ( ) 上this.mainContext.destination
调用该start(0)
方法,它将正常播放。AudioBufferSourceNode
this.bSrc
相反,如果this.audioDestination
尚未连接到 Audio Context 最终目的地,则缓冲区将不会播放(这没关系),但其播放将延迟到建立连接(从而使任何关于 AudioBufferSourceNode 何时完成播放的假设无效) .
我目前正在实现一个插件架构,其中每个插件都有其 audioDestination 增益节点,并且不(也不能)知道它是否间接连接到最终的音频上下文目标。在我看来合乎逻辑的是 AudioBufferSourceNode 应该立即“播放”并完成,即使它没有连接到最终的音频上下文目标。但它似乎不像这样工作。我是对的还是我弄错了?有没有办法改变这种行为?
代码:
/* Create the audio Context. */
this.mainContext = new webkitAudioContext;
/* Create an audio gain node */
this.audioDestination = this.mainContext.createGainNode();
/* [...] An AudioBuffer gets decoded and stored into this.audioBuffer*/
/* Create an AudioBufferSourceNode and try to play it immediately */
this.bSrc = this.audioContext.createBufferSource();
this.bSrc.connect (this.audioDestination);
this.bSrc.buffer = this.audioBuffer;
this.bSrc.start(0);
/* Create a callback for when the AudioBufferSourceNode finishes playing */
if (!this.bSrc.loop) {
var pbTimer = setTimeout(function() {
this.playFinishedCallback();
}.bind(this), this.audioBuffer.duration * 1000 / this.bSrc.playbackRate.value);
}
/* Connect this.audioDestination to the final AudioContext destination */
/* If this statement is executed after the previous ones, playback will start NOW */
this.audioDestination.connect(this.mainContext.destination);