How to mute microphone after recording using javascript
Asked Answered
P

2

7

I have using an application to record audio which uses the javaScript code but after recording its mic will never disable.

So please suggest me that how to mute/disable microphone using javaScript?

Plate answered 28/3, 2014 at 11:24 Comment(2)
Read this #17123763Piassava
use the muted property - set it to off.Headmost
J
8

You can stop stream created on MediaRecorder:

navigator.mediaDevices.getUserMedia({audio:true,video:false}).then(function(stream)
{
    recorder = new MediaRecorder(stream);
});
recorder.stream.getAudioTracks().forEach(function(track){track.stop();});
Jemina answered 23/3, 2017 at 20:31 Comment(4)
I've edited your question to use multi-line code formatting. I noticed that you use recorder outside of where you call new MediaRecorder. Is that correct?Gibe
It's a little example. You can set a global variable outsid that function and using it for others method.Matland
This does not actually mute. This stops the stream. This could be a problem is some scenarios, for example when you want to unmute. Check the @darma answer for detailsHunker
This is useful if you need to 100% stop mic usage, in some cases it's what you needSusurrus
M
8

The previous answer is not ideal because the tracks are actually ended and cannot recover after you called track.stop();, so you cannot unmute for example. Better:

Mute

stream.getAudioTracks().forEach(function(track) {
    track.enabled = false;
});

Unmute

stream.getAudioTracks().forEach(function(track) {
    track.enabled = true;
});
Murky answered 23/1, 2022 at 12:47 Comment(1)
Great. Will this work for video tracks as well? Can we turn off/on the camera after the video/screen capturing has started?Phalangeal

© 2022 - 2024 — McMap. All rights reserved.