1

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?

Ahmad Habib
  • 2,053
  • 1
  • 13
  • 28
  • Does this answer your question? [Stop / kill WebRTC media stream](https://stackoverflow.com/questions/34966809/stop-kill-webrtc-media-stream) – jib Dec 13 '19 at 00:54
  • https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/stop – G-Man Dec 16 '19 at 14:41

1 Answers1

1

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();
});
Laphaze
  • 278
  • 1
  • 6