WebRTC - 如何使本地音频输出静音

WebRTC - How to mute local audio output

我试图在 WebRTC 中仅将本地音频播放静音,更具体地说是在 getUserMedia() 之后和建立任何服务器连接之前。 None 个我找到的选项; Muaz Khan 的这个失败了:

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

source

此技术也 described here 为 "working",但在 Chrome 版本 39.0.2171.95(64 位)(Ubuntu 14.04)上失败。

据说通过使用音量增益起作用的其他方法:

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 我不想在我的扬声器上听到麦克风的输入,但我确实想看到我的视频图像。

解决方法

此解决方法是由 Benjamin Trent 建议的,它通过在视频标签上设置静音属性来使音频静音,如下所示:

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

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

将此添加为答案,因为它是事实上的正确答案:

许多主要的 WebRTC 视频平台都在使用您所说的解决方法:

navigator.mediaDevices.getUserMedia({ video: true, audio: true })
  .then(stream => {
    const vid = document.getElementById('html5vid');
    vid.autoplay = true;
    vid.muted = true;
    vid.srcObject = stream;
  });