Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRTC - How to mute local audio output

Tags:

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

like image 712
Paul Gregoire Avatar asked Jan 12 '15 18:01

Paul Gregoire


People also ask

How do I mute audio in Webrtc?

The AppRTCDemo application has a microphone mute button and it is mapped to the setEnabled API on your local audio track.

How do I mute and unmute in Webrtc?

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().

How do I mute my mic in Javascript?

getAudioTracks()[0]. enabled = false will mute your mic.


1 Answers

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;   }); 
like image 53
xdumaine Avatar answered Sep 16 '22 14:09

xdumaine