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

WebRTC - How to mute local audio output

本文关键字:音频 输出 WebRTC      更新时间:2023-09-26

我试图只在WebRTC中静音本地音频播放,更具体地说是在getUserMedia()之后和建立任何服务器连接之前。我找到的选项都不起作用;穆阿兹汗的这个失败了:

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

此技术在此处也描述为"有效",但在 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

dr 我不想听到扬声器上麦克风的输入,但我确实想看到我的视频图像。

解决方法

本杰明·特伦特(Benjamin Trent)提出了此解决方法,它通过在视频标签上设置静音属性来使音频静音,如下所示:

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

也有类似的问题,但它是视频,所以不一样

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

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

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