22

I'm trying to mute only the local audio playback in WebRTC, more specifically after getUserMedia() and prior to any server connection being made. None of the options I've found work; this one from Muaz Khan fails:

var audioTracks = localMediaStream.getAudioTracks();
// if MediaStream has reference to microphone
if (audioTracks[0]) {
    audioTracks[0].enabled = false;
}

source

This technique is also described here as "working", but fails here on Chrome Version 39.0.2171.95 (64-bit) (Ubuntu 14.04).

Additional method that is said to work by using volume gain:

window.AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext = new AudioContext();
var source = audioContext.createMediaStreamSource(clientStream);
var volume = audioContext.createGain();
source.connect(volume);
volume.connect(audioContext.destination);
volume.gain.value = 0;  //turn off the speakers

tl;dr I don't want to hear the input from my microphone on my speakers, but I do want to see my video image.

Workaround

This workaround was suggested by Benjamin Trent and it mutes the audio by setting the muted attribute on the video tag like so:

document.getElementById("html5vid").muted = true;

Also similar question, but its for video, so not the same

Community
  • 1
  • 1
Paul Gregoire
  • 9,715
  • 11
  • 67
  • 131
  • 1
    Why not set the html video tag to muted? – Benjamin Trent Jan 12 '15 at 18:56
  • 1
    That's not ideal in my case, but is audio "only" mute supported on the tag? – Paul Gregoire Jan 12 '15 at 19:43
  • 1
    muting the video html tag mutes the audio associated with the video. The video will continue to play and both audio and video tracks in the stream will still be accessible and able to be sent to another peer. It sounds like, though, you found a bug in that Chrome release on ubuntu that does need to be addressed. – Benjamin Trent Jan 12 '15 at 20:18

1 Answers1

22

Adding this as the answer because it is the de facto correct answer:

What you stated as a workaround is what's used by many major WebRTC Video platforms:

navigator.mediaDevices.getUserMedia({ video: true, audio: true })
  .then(stream => {
    const vid = document.getElementById('html5vid');
    vid.autoplay = true;
    vid.muted = true;
    vid.srcObject = stream;
  });
xdumaine
  • 10,096
  • 6
  • 62
  • 103
  • What about audio recording only, like { video: false, audio: true }? – headkit Aug 02 '19 at 15:32
  • 1
    @headkit just don't attach your own audio to the DOM at all. To indicate to the user that audio is being captured you can show a waveform or speaking indicator. – xdumaine Aug 06 '19 at 16:36
  • Hi @xdumaine, what I did was, I instantiate the `new Audio()` on javascript. Is it still possible to mute while streaming the data being played from that? – sshanzel Feb 08 '21 at 07:48