Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The microphone icon in Chrome stays open when disconnected #52

Closed
hookedupjoe opened this issue Aug 3, 2023 · 5 comments
Closed

The microphone icon in Chrome stays open when disconnected #52

hookedupjoe opened this issue Aug 3, 2023 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@hookedupjoe
Copy link

After stopping the use of the microphone, in Chrome, the mic icon stays on. This happens in my project and the examples on the websites as well. A solution is to also close the microphone track that is open when disconnecting.

An example solution is to add an option to the disconnect method to optionally close the track. This made the icon go away when used and should be backward compatible for anyone still needing it to not be closed when disconnecting.

/**
	 * Disconnects audio sources from the analyzer
	 *
	 * @param [{object|array}] a connected AudioNode object or an array of such objects; if undefined, all connected nodes are disconnected
	 * @param {boolean} [stopTracks] if true, stops the track when disconnected
	 */
	disconnectInput( sources, stopTracks ) {
		if ( ! sources )
			sources = Array.from( this._sources );
		else if ( ! Array.isArray( sources ) )
			sources = [ sources ];
		for ( const node of sources ) {
			const idx = this._sources.indexOf( node );			
			if (stopTracks && node.mediaStream){
				for ( const ats of node.mediaStream.getAudioTracks() ){
					ats.stop();
				}
			}
			if ( idx >= 0 ) {
				node.disconnect( this._input );
				this._sources.splice( idx, 1 );
			}
		}
	}

Naturally can still be called with default sources:
audioMotion.disconnectInput(false, true);

@hvianna hvianna added the enhancement New feature or request label Aug 3, 2023
@hvianna hvianna self-assigned this Aug 3, 2023
@hvianna
Copy link
Owner

hvianna commented Aug 3, 2023

Thanks for this! Although the user could do that on their side, I think it's an elegant solution.

@hookedupjoe
Copy link
Author

Thanks for this! Although the user could do that on their side, I think it's an elegant solution.

Agreed - I just had to hunt down how to do it and that took a bit of doing so maybe this would help others. Thanks for the killer awesome fantastic .. not enough good words to describe ... library.

@Staijn1
Copy link
Contributor

Staijn1 commented Aug 12, 2023

I've noticed the same behavior of the mic staying open in both Firefox & Chrome in my personal project where I use this library. I figured I did something wrong and since I'm the only person using it.. I could not be bothered to fix it.
I'd love to see the code-snippet merged.

Although, should the track also be stopped when the analyzer is toggled off, using toggleAnalyzer()? (Preventing the icon being shown when the analyzer is toggled off)
Or is it then impossible to resume the same track after toggling the analyzer on again? I'm not familiar enough with using media devices to tell.

If desired I can raise a PR that toggles the track on disconnectInput() & toggleAnalyzer()

@hvianna
Copy link
Owner

hvianna commented Aug 13, 2023

I'll be adding this to v4.2.0

@Staijn1: according to MDN docs the track is permanently ended on stop() and disassociated from the source stream, so it can't be resumed.

@hvianna
Copy link
Owner

hvianna commented Sep 7, 2023

Now available in v4.2.0 🎉

@hvianna hvianna closed this as completed Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants