'how to fully shutdown audio input stream

I have created an audio worklet that performs pitch detection , all works fine but I want to free the microphone once I am done

I get the stream and wire everything up like this

        const AudioContextConstructor =
            window.AudioContext || window.webkitAudioContext;
        this.audioContext = new AudioContextConstructor();

        await this.audioContext.audioWorklet.addModule('js/worklet_pitcher.js');
        this.stream = await navigator.mediaDevices.getUserMedia({ audio: true });

        var mediaStreamSource = this.audioContext.createMediaStreamSource(this.stream);
        this.pitchWorklet = new AudioWorkletNode(this.audioContext, 'pitch-processor');
        mediaStreamSource.connect(this.pitchWorklet);

When I am done I simply do this

stop = (): void => {
     if (this.running) {
        this.audioContext.close();
        this.running = false;
    }
}

this stops the worklet pipeline but the red dot still shows in the browser tab meaning that I still own the mic.

I looked for a stream.close so I could explicitly close the MediaStream returned by getUserMediabut there isnt one



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source