How to make and add a MediaStreamTrack to a media stream

I am wondering how to make and add a MediaStreamTrack to a MediaStream. I cant find a way to add or make one, and so I’m stuck. Please help. Thanks!

>Solution :

Step 1: Generate the tracks.


const userMedia = await navigator.mediaDevices.getUserMedia({audio : true, video : true});

const videoTrack = userMedia.getVideoTracks()[0];
const audioTrack = userMedia.getAudioTracks()[0];

Step 2: Create a MediaStream and add tracks to them.


const stream = new MediaStream();
stream.addTrack(videoTrack);
stream.addTrack(audioTrack);

Now you can use that MediaStream as whatever you want.

Leave a Reply