我正在使用MediaElementAudioSourceNode
播放音频文件(refer to this question)并且它按预期工作但我对如何在断开连接后处理节点有疑问。
目前我有这段代码:
//Do we have already an MediaElementSourceNode ?
if (this._sourceNode) {
console.warn("THERE IS AUDIO PLAYING!");
//this._audioContainer.addTextTrack
// disconnect and remove all references.
this._audioContainer.pause();
this._audioContainer = undefined;
this._sourceNode.disconnect();
this._sourceNode = undefined;
console.warn("The _audioContainer was paused and the _sourceNode disconnected");
}
this._audioContainer = new Audio(filePath);
this._audioContainer.oncanplaythrough = () => {
clearTimeout(securityTimeout);
console.log(this._audioContainer.readyState);
console.dir(this._audioContainer);
this.audioPlayerActive = true;
// MediaElementSource are ONE TIME ONLY use kind of Node
this._sourceNode = this._AudioContext.createMediaElementSource(this._audioContainer);
this._sourceNode.connect(this._EqNode);
this._audioContainer.play();
console.log("playAudioFile() END");
};
this._audioContainer.onended = () => {
console.log("__audioContainer.onended()");
this._audioContainer = undefined;
this._sourceNode.disconnect();
this._sourceNode = undefined;
};
使用Web Audio inspector on firefox我可以看到每次运行此代码时(按钮单击),它会创建一个新的MediaElementAudioSourceNode
,如果检测到,则会断开先前创建的代码。此外,当音频结束时,它将断开连接。但是几分钟后,节点仍然存在: