wavesurfer.js icon indicating copy to clipboard operation
wavesurfer.js copied to clipboard

Fix Failed to execute 'createMediaElementSource' on 'AudioContext'

Open SchmidtWongxn opened this issue 3 years ago • 1 comments

Short description of changes:

It works fine the first time I create the wavesurfer with MediaElementWebAudio backend, but when the component is unmounted and then recreated at a later point, I get the following error message:

Uncaught InvalidStateError: Failed to execute 'createMediaElementSource' on 'AudioContext': HTMLMediaElement already connected previously to a different MediaElementSourceNode.

It's possible to workaround this issue using WeakMap to remember MediaElementSourceNode:

 createMediaElementSource(mediaElement) {
        if (MediaElementMap.has(mediaElement)) {
            this.sourceMediaElement = MediaElementMap.get(mediaElement);
        } else {
            this.sourceMediaElement = this.ac.createMediaElementSource(
                mediaElement
            );
            MediaElementMap.set(mediaElement, this.sourceMediaElement);
        }
        this.sourceMediaElement.connect(this.analyser);
    }

Breaking in the external API:

none

Breaking changes in the internal API:

none

SchmidtWongxn avatar May 24 '22 13:05 SchmidtWongxn

ping @SchmidtWongxn

thijstriemstra avatar Jul 29 '22 20:07 thijstriemstra

no feedback, closing.

thijstriemstra avatar Nov 10 '22 17:11 thijstriemstra