javascript - Adding an AudioTrack while the MediaRecorder is in state 'recording' - Stack Overflow

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

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)

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

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)

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

Share Improve this question asked Feb 22, 2021 at 14:48 Andrew MedvedevAndrew Medvedev 651 silver badge8 bronze badges 2
  • Hi Andrew, does the answer over here solve your question as well? stackoverflow./questions/57838283/… – chrisguttandin Commented Feb 22, 2021 at 17:26
  • Thank you for your link, but I still have don`t understand how i can replace the track during the recording. Tracks merging with AudioContext it's OK, but before Recording, is not it? – Andrew Medvedev Commented Feb 22, 2021 at 19:05
Add a ment  | 

1 Answer 1

Reset to default 8

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);

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);

You can then start recording the initialMediaStream.

mediaRecorder.start();

Later on you can replace the initialMediaStream with anotherMediaStream.

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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745557680a4632917.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信