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
The AppRTCDemo application has a microphone mute button and it is mapped to the setEnabled API on your local audio track.
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().
getAudioTracks()[0]. enabled = false will mute your mic.
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; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With