How to mute/unmute mic in webrtc

user526206 picture user526206 · Feb 19, 2016 · Viewed 20.7k times · Source

I have read from here that how i can mute/unmute mic for a localstream in webrtc:WebRTC Tips & Tricks

When i start my localstream mic is enable at that time by default so when i set audioTracks[0].enabled=false it muted a mic in my local stream but when i set it back true it enable to unmute. Here is my code mute/unmute for a localstream:

 getLocalStream(function (stream,enable) {
        if (stream) {
            for (var i = 0; i < stream.getTracks().length; i++) {
                var track = stream.getAudioTracks()[0];
                if (track)
                    track.enabled = enable;
                //track.stop();
            }
        }
    });

Can someone suggest me how i can unmute mic back in a localstream.

Answer

Adrian Ber picture Adrian Ber · Feb 20, 2016

I assume that your method getLocalStream is actually calling navigator.getUserMedia. In this case when you do this you'll get another stream, not the original one. Using the orignal stream you should do

mediaStream.getAudioTracks()[0].enabled = true; // or false to mute it.

Alternatively you can check https://stackoverflow.com/a/35363284/1210071