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

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

本文介绍了WebRTC 混合本地和远程音频流并记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So far i've found a way only to record either local or remote using MediaRecorder API but is it possible to mix and record both steams and get a blob?

请注意它只是音频流,我不想在服务器端混音/录制.

Please note its audio steam only and i don't want to mix/record in server side.

我有一个 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 中添加多个轨道,但它仍然只提供第一轨道音频.任何帮助或见解将不胜感激!

Even i tried adding multiple tracks in MediaStream API but it still gives only first track audio. Any help or insight 'd be appreciated!

推荐答案

WebAudio API 可以为您进行混音.如果您想记录数组 audioTracks 中的所有音轨,请考虑以下代码:

The WebAudio API can do mixing for you. Consider this code if you want to record all the audio tracks in the array 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 混合本地和远程音频流并记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:35