【发布时间】:2013-02-01 06:22:20
【问题描述】:
在我的对象中,我有一个 AudioBufferSourceNode 对象 (this.bSrc),它连接到一个增益节点 (this.audioDestination),该节点播放已存储在对象 (this.audioBuffer) 中的(有效)AudioBuffer。
如果this.audioDestination 连接到音频上下文最终目的地this.mainContext.destination 并且我在我的AudioBufferSourceNode 对象(this.bSrc) 上调用start(0) 方法,它将正常播放。
相反,如果this.audioDestination尚未连接到音频上下文最终目的地,则缓冲区将不会播放(这没关系),但其播放将延迟到连接完成制作(因此使任何关于 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);
【问题讨论】:
标签: javascript html web-audio-api