到目前为止,我已经找到了一种仅使用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中添加多个轨道,但它仍然只提供第一个轨道音频。任何帮助或见解将不胜感激!
最佳答案
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);
关于WebRTC混合本地和远程音频流并进行录制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42138545/