18

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 Answers5

36

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

Community
  • 1
  • 1
Adrian Ber
  • 20,474
  • 12
  • 67
  • 117
6

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

Xin
  • 33,823
  • 14
  • 84
  • 85
  • https://w3c.github.io/mediacapture-main/#track-muted -- muted is not something you can control -- or should not be able to control. – Philipp Hancke Apr 29 '18 at 12:56
  • @PhilippHancke Yes, you are right, I have tried `muted` it is read only, and I have updated my answer as well. Thank you very much for letting me know. – Xin Apr 30 '18 at 02:34
4

Ahhh there is a good way to do this:

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

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

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

MAYUR SANCHETI
  • 420
  • 3
  • 10
  • He's talking about muting audio/video on his localstream so the remote peer can receive im either on audio or video – ndotie Nov 23 '21 at 09:09
  • can you explain more – MAYUR SANCHETI Jan 03 '22 at 05:43
  • So, we're sending stream of video/audio through the wire, Muting audio or video you just do it on the media object obtained on ur browser... and your webrtc peer connection object receives already audio or video muted stream to send over to the other peer – ndotie Jan 05 '22 at 21:54
  • have you tested this what is happening on remote side. – MAYUR SANCHETI Jan 06 '22 at 04:20
  • Yes, i have tested, but when this done incorrectly... the remote side nothing gets muted or unmuted..... When we're muting audio stream it means remote side doesnt receive audio stream at all... and its not that remote can receive and choose not to play it no, that would be bad use of bandwidth and resources in general – ndotie Jan 08 '22 at 18:29