在AudioContext中处理后,无法听到来自RTCPeerConnection的音频

时间:2019-04-16 08:01:50

标签: javascript google-chrome webrtc web-audio

我有活跃的RTCPeerConnection。我要处理此音频。我先创建new AudioContext(),然后创建新节点:MediaStreamAudioSourceNodeMediaStreamAudioDestinationNode

MediaStreamAudioSourceNode连接到MediaStreamAudioDestinationNode后,我听不到声音。

MediaStreamAudioDestinationNode已连接到Audio元素。

  • 我尝试将MediaStream的{​​{1}}直接连接到RTCPeerConnection,并且有效

  • 我还尝试将Audio连接到MediaStreamAudioSourceNode.mediaStream元素,它也可以工作。

  • 我已经连接了Audio返回的MediaStream,甚至可以与getUserMedia()

  • 一起使用

看来MediaStreamAudioDestinationNode中只有MediaStreamRTCPeerConnection不兼容。

MediaStreamAudioDestinationNode

function play(eventWebRtcPeerConnection) { const audio = new Audio() const ctx = new AudioContext(); let mediaStream = new MediaStream(); eventWebRtcPeerConnection.streams[0].getAudioTracks().forEach(track => mediaStream.addTrack(track)); // create source and destination and connect them const msSource = ctx.createMediaStreamSource(mediaStream); const msDestination = ctx.createMediaStreamDestination(); msSource.connect(msDestination); // play media stream on Audio object audio.srcObject = msDestination.stream; // does not work // audio.srcObject = msSource.mediaStream; // works audio.oncanplay = async () => { audio.muted = false; await audio.play(); } } 连接到MediaStreamAudioSourceNode后,我希望能听到音频。

1 个答案:

答案 0 :(得分:0)

一个令人困惑的答案是,至少Chrome要求您将来自WebRTC连接的MediaStream直接连接到Audio元素。我不确定这是错误还是功能。好消息是您可以将此音频元素静音。您的代码将如下所示:

const mutedAudio = new Audio();
const outputAudio = new Audio();
const context = new AudioContext();
const mediaStream = new MediaStream();

peerConnection.streams[0].getAudioTracks().forEach(track => mediaStream.addTrack(track));

mutedAudio.muted = true;
mutedAudio.srcObject = mediaStream;
mutedAudio.play();

const source = context.createMediaStreamSource(mediaStream);
const destination = context.createMediaStreamDestination();

source.connect(destination);

outputAudio.muted = false;
outputAudio.srcObject = msDestination.stream;
outputAudio.play();

您还可以跳过outputAudio元素,并将source直接连接到destination的{​​{1}}。