How to mute/unmute mic in webrtc

39.4k Views Asked by At

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.

5

There are 5 best solutions below

0
On

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

0
On

Ahhh there is a good way to do this:

mediaStream.getVideoTracks()[0].enabled = !(mediaStream.getVideoTracks()[0].enabled);
0
On

You should read and set the "enabled" value. The "enabled" value is for 'muting'. The "muted" value is a read-only value to do with whether the stream is currently unable to play.

The enabled property on the MediaStreamTrack interface is a Boolean value which is true if the track is allowed to render the source stream or false if it is not. This can be used to intentionally mute a track. When enabled, a track's data is output from the source to the destination; otherwise, empty frames are output.

In the case of audio, a disabled track generates frames of silence (that is, frames in which every sample's value is 0). For video tracks, every frame is filled entirely with black pixels.

The value of enabled, in essence, represents what a typical user would consider the muting state for a track, whereas the muted property indicates a state in which the track is temporarily unable to output data, such as a scenario in which frames have been lost in transit.

2
On

There are 2 properties enabled and muted. enabled is for setting, and muted is read-only on the remote side (the other person) (I have tried, setting muted does not work, basically, value cannot be changed)

stream.getAudioTracks()[0].enabled = true; // remote one will get muted change

5
On

Step 1) call jquery.min.js

Step 2) use below code ,

A) To Mute

$("video").prop('muted','true');

B) To unmute

$("video").prop('muted','');

single Icon mute and unmute like youtube

function enablemute(thisimag) { 
  if($(thisimag).attr('src')=='images/mute.png')
  {   
    $("video").prop('muted','');
    $(thisimag).prop('src','images/unmute.png');
  }
  else
  {
      //alert('her');
    $("video").prop('muted','true');
    $(thisimag).prop('src','images/mute.png');
  } 
} 

above function enablemute should call from onclick