如何在WebAudioApi中部署MediaElementAudioSourceNode?

时间:2018-01-26 15:50:46

标签: javascript web-audio web-audio-api

我正在使用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,如果检测到,则会断开先前创建的代码。此外,当音频结束时,它将断开连接。但是几分钟后,节点仍然存在:

enter image description here

我想防止内存泄漏。我怎么处理它们? 编辑: 没有功能(我认为) enter image description here

1 个答案:

答案 0 :(得分:1)

目前没有任何方法可以执行此操作:https://github.com/WebAudio/web-audio-api/issues/1202