Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove track from MediaStream and "stop" webcam?

I'm trying to remove a track from a MediaStream. MediaStream.removeTrack() removes the track from the stream, but the camera light is left on indicating that the camera is still active.

https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack?redirectlocale=en-US&redirectslug=DOM%2FMediaStreamTrack

This references a stop() method which I suppose would stop the camera completely, In chrome however I get "Object MediaStreamTrack has no method 'stop'"

Is there a way around this or do I have to stop the whole stream and then recreate it with the tracks I don't want gone? As an example, I want to remove the video track while the audiotrack is still there.

like image 516
Jonathan Avatar asked Aug 11 '13 16:08

Jonathan


People also ask

How do I stop streaming video in HTML?

Stopping a video stream This works by obtaining the video element's stream from its srcObject property. Then the stream's track list is obtained by calling its getTracks() method. From there, all that remains to do is to iterate over the track list using forEach() and calling each track's stop() method.

What is MediaStreamTrack?

The MediaStreamTrack interface represents a single media track within a stream; typically, these are audio or video tracks, but other track types may exist as well.


2 Answers

MediaStreamTrack.stop() is now added to the Chrome.

MediaStream.stop() is deprecated in Chrome 45.

You should use MediaStream.getVideoTracks() to get video tracks and stop the track using MediaStreamTrack.stop()

like image 104
Anand S Avatar answered Oct 05 '22 10:10

Anand S


You need to call stop() on the MediaStream, not a MediaStreamTrack.

Take a look at simpl.info/gum. From the console, call stream.stop(): recording stops and the video camera light goes off.

like image 23
Sam Dutton Avatar answered Oct 05 '22 09:10

Sam Dutton