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
1 Answer
Reset to default 8You 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 GainNode
s to apply a cross-fade between the two streams if that's what you want.