我有活跃的RTCPeerConnection
。我要处理此音频。我先创建new AudioContext()
,然后创建新节点:MediaStreamAudioSourceNode
至MediaStreamAudioDestinationNode
将MediaStreamAudioSourceNode
连接到MediaStreamAudioDestinationNode
后,我听不到声音。
MediaStreamAudioDestinationNode
已连接到Audio
元素。
我尝试将MediaStream
的{{1}}直接连接到RTCPeerConnection
,并且有效
我还尝试将Audio
连接到MediaStreamAudioSourceNode.mediaStream
元素,它也可以工作。
我已经连接了Audio
返回的MediaStream
,甚至可以与getUserMedia()
看来MediaStreamAudioDestinationNode
中只有MediaStream
与RTCPeerConnection
不兼容。
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
后,我希望能听到音频。
答案 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}}。