How to mute/unmute mic in webrtc
Asked Answered
N

6

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.

Nobe answered 19/2, 2016 at 18:26 Comment(0)
M
38

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://mcmap.net/q/669218/-webrtc-and-google-chrome-app-microphone-volume-adjustment

Madonnamadora answered 20/2, 2016 at 11:41 Comment(0)
M
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

Meshach answered 29/4, 2018 at 0:28 Comment(2)
w3c.github.io/mediacapture-main/#track-muted -- muted is not something you can control -- or should not be able to control.Headmost
@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.Meshach
N
5

Ahhh there is a good way to do this:

mediaStream.getVideoTracks()[0].enabled = !(mediaStream.getVideoTracks()[0].enabled);
Nobe answered 19/2, 2016 at 19:9 Comment(0)
R
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.

Raylenerayless answered 1/4, 2020 at 7:20 Comment(0)
B
0

Full code of get microphone and mute it after 6 second is:

var voiceConstraints = { audio: true, video: false };
var localStream = null;
navigator.getUserMedia(voiceConstraints , callbackUserMediaSuccess, errorHandler);

const callbackUserMediaSuccess = (stream) => {
    console.log("callbackUserMediaSuccess: got media stream");
    localStream = stream;
    setTimeout(function(){localStream.getAudioTracks()[0].enabled = false;},6000);
};

const errorHandler = (error) => {
    console.log(error);
};

As you seen localStream.getAudioTracks()[0].enabled = false; used for mute microphone.

Bennybenoit answered 2/8 at 22:33 Comment(0)
E
-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

Exception answered 31/10, 2020 at 3:9 Comment(5)
He's talking about muting audio/video on his localstream so the remote peer can receive im either on audio or videoVolute
can you explain moreException
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 peerVolute
have you tested this what is happening on remote side.Exception
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 generalVolute

© 2022 - 2024 — McMap. All rights reserved.