I've made an audio visualizer in React with the AudioContext interface and I want the user able to enable and disable it.
The visualizer works fine and I can disable it as well (I just remove the vis component)
However, when I want to enable it again, it tells me : "InvalidStateError: Failed to execute 'createMediaElementSource' on 'AudioContext': HTMLMediaElement already connected previously to a different MediaElementSourceNode."
I suppose that I can't have 2 ElementSources on a audio element at the same time. But I can't manage to work around this error.
I've tried to return audiocontext.close() in my useEffect hook so I can create a new MediaElementSource then (not sure if it works this way) but it doesn't change anything.
Maybe there is a property on the audio element that can tell me if there is already a MediaElementSource ? (I didn't find anything)
Or maybe the AudioContext interface is a bit too hard for me since I'm only a beginner in React and I've just copy paste an existing visualizer...
Thanks for the help!
Here is some code from my visualizer component :
useEffect(() => {
var context = new AudioContext(); //Some visualiser stuff
var src = context.createMediaElementSource(audio);// The error is here
src.crossOrigin = "anonymous";
var analyser = context.createAnalyser();
src.connect(analyser);
analyser.connect(context.destination);
analyser.fftSize = 1024;
// Some canvas stuff here
//
return () => {
context.close() // doesn't work ?
};
}, [somedeps]);
return <canvas>...</canvas>