WebRTC混合本地和远程音频流和记录

问题描述 投票:4回答:1

到目前为止,我已经找到了一种方法,只能使用MediaRecorder API记录本地或远程,但是可以混合并记录两个流并获得blob吗?

请注意它的音频蒸汽,我不想在服务器端混合/记录。

我有一个RTCPeerConnection作为pc

var local_stream = pc.getLocalStreams()[0];
var remote_stream = pc.getRemoteStreams()[0];
var audioChunks = [];
var rec = new MediaRecorder(local_stream);
rec.ondataavailable = e => {
    audioChunks.push(e.data);
    if (rec.state == "inactive") 
        // Play audio using new blob
}
rec.start();

即使我尝试在MediaStream API中添加多个轨道,但它仍然只提供第一个音轨。任何帮助或见解'不胜感激!

webrtc web-audio
1个回答
8
投票

WebAudio API可以为你做混合。如果要记录数组audioTracks中的所有音轨,请考虑以下代码:

const ac = new AudioContext();

// WebAudio MediaStream sources only use the first track.
const sources = audioTracks.map(t => ac.createMediaStreamSource(new MediaStream([t])));

// The destination will output one track of mixed audio.
const dest = ac.createMediaStreamDestination();

// Mixing
sources.forEach(s => s.connect(dest));

// Record 10s of mixed audio as an example
const recorder = new MediaRecorder(dest.stream);
recorder.start();
recorder.ondataavailable = e => console.log("Got data", e.data);
recorder.onstop = () => console.log("stopped");
setTimeout(() => recorder.stop(), 10000);
© www.soinside.com 2019 - 2024. All rights reserved.