Ahmad Habib
Ahmad Habib

Reputation: 2382

stop WebRTC media stream

I have started using navigator.mediaDevices.getUserMedia({audio: true, video:false}) a media stream and it is working fine. Now i want it to stop on a button click but i couldn't find any workable solutions. As per my research WebRTC has depreciated stream.stop() but i also found some alternatives like in Stop/Close webcam which is opened by navigator.getUserMedia but solutions here are not working either.

Is there any workable solution?

Upvotes: 1

Views: 836

Answers (1)

Laphaze
Laphaze

Reputation: 386

Instanciate stream :

    navigator.mediaDevices.getUserMedia({ audio: true, video: false})
    .then(stream => {
        // attach this stream to window object so you can reuse it later
        window.localStream = stream;
        // Your code to use the stream
    })
    .catch((err) =>{
        console.log(err);
    });

Remove stream :

localStream.getTracks().forEach((track) => {
    track.stop();
});

Upvotes: 1

Related Questions