在 MediaRecorder 处于“正在录制"状态时添加 AudioTrack [英] Adding an AudioTrack while the MediaRecorder is in state 'recording'

查看:41
本文介绍了在 MediaRecorder 处于“正在录制"状态时添加 AudioTrack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 MediaRecorder 处于正在录制"状态时,我正在尝试将 mediaTrack 添加到 mediaStream

Im trying to add mediaTrack to the mediaStream while MediaRecorder is in state 'recording'

添加新曲目的代码如下:

The code for adding a new track is the following:

activeStream.addTrack(newAudioTrack)

此后事件 (onstop) 被触发.我怎样才能避免这种情况?

After that the event (onstop) was triggered. How can I avoid this?

推荐答案

您可以使用 AudioContext 来创建固定的 MediaStream,您可以将其传递给 媒体记录器.这允许您在录制时更改输入.

You can use an AudioContext to create a fixed MediaStream that you can pass to the MediaRecorder. This allows you to change the input when recording.

const audioContext = new AudioContext();
const mediaStreamAudioDestinationNode = new MediaStreamAudioDestinationNode(audioContext);
const mediaRecorder = new MediaRecorder(mediaStreamAudioDestinationNode.stream);

假设您有一个 MediaStream称为 initialMediaStream.你可以这样连接:

Let's say you have a MediaStream called initialMediaStream. You could connect it like that:

const mediaStreamAudioSourceNode = new MediaStreamAudioSourceNode(
    audioContext,
    { mediaStream: initialMediaStream }
);

mediaStreamAudioSourceNode.connect(mediaStreamAudioDestinationNode);

然后您可以开始录制initialMediaStream.

mediaRecorder.start();

稍后您可以将 initialMediaStream 替换为 anotherMediaStream.

Later on you can replace the initialMediaStream with anotherMediaStream.

const anotherMediaStreamAudioSourceNode = new MediaStreamAudioSourceNode(
    audioContext,
    { mediaStream: anotherMediaStream }
);

anotherMediaStreamAudioSourceNode.connect(mediaStreamAudioDestinationNode);
mediaStreamAudioSourceNode.disconnect();

如果需要,您甚至可以使用 GainNodes 在两个流之间应用淡入淡出.

You could even use GainNodes to apply a cross-fade between the two streams if that's what you want.

这篇关于在 MediaRecorder 处于“正在录制"状态时添加 AudioTrack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆