Web音频MediaElementSource节点未被垃圾回收

时间:2022-10-29 23:55:29

In web audio, is there a trick to getting MediaElementSource nodes to be garbage collected?

在网络音频中,有一个技巧可以让MediaElementSource节点被垃圾收集吗?

I've set up a page that adds two nodes: a MediaElementSource and an Oscillator. When I disconnect both of them, the Oscillator is garbage collected soon after, but the MediaElementSource stays permanently. (This is according to Firefox web audio dev tools which visualize the audio graph.)

我已经设置了一个添加两个节点的页面:MediaElementSource和Oscillator。当我断开它们时,振荡器很快被垃圾收集,但MediaElementSource永久保留。 (这是根据Firefox网络音频开发工具,可视化音频图。)

I'm experiencing this in both Chrome and Firefox on Mac (Yosemite).

我在Mac(优胜美地)的Chrome和Firefox上都遇到过这种情况。

Why is the MediaElementSource node lingering permanently and is there any way to get rid of it without reloading the page or killing the audio context?

为什么MediaElementSource节点永久延迟,有没有办法摆脱它而不重新加载页面或杀死音频上下文?

<audio src="gam2.mp3" id="audio" controls></audio>
<button onclick="disc()">disconnect</button>

<script>

    var actx = new AudioContext()

    var audio = document.getElementById("audio")
    var stream = actx.createMediaElementSource(audio)
    stream.connect(actx.destination)

    var sine = actx.createOscillator()
    sine.connect(actx.destination)

    function disc() {
        audio.remove()
        stream.disconnect()
        sine.disconnect()
    }

</script>

1 个解决方案

#1


0  

Try nulling your var's so that the GC knows that they are eligible for garbage collection.

尝试清空var,以便GC知道它们有资格进行垃圾回收。

function disc() {
    audio.remove()
    stream.disconnect()
    stream = null
    sine.disconnect()
    sine = null //doing this here for consistency
}

#1


0  

Try nulling your var's so that the GC knows that they are eligible for garbage collection.

尝试清空var,以便GC知道它们有资格进行垃圾回收。

function disc() {
    audio.remove()
    stream.disconnect()
    stream = null
    sine.disconnect()
    sine = null //doing this here for consistency
}