How to remove track from MediaStream and "stop" webcam?
Solution 1
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()
Solution 2
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.
Solution 3
for stopping specific media stream, Maybe this help: (Link)
function stopStreamedVideo(videoElem) {
const stream = videoElem.srcObject;
const tracks = stream.getTracks();
tracks.forEach(function(track) {
track.stop();
});
videoElem.srcObject = null;
}
Related videos on Youtube
Comments
-
Jonathan almost 2 years
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.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.
-
Jonathan over 10 yearsNo, not if I only want to stop one track. Each MediaStream have two tracks, audio and video. If I call stop on the MediaStream it will end everything.
-
Sam Dutton over 10 yearsAh -- so you want stop the video, but keep the audio? Not sure if this does what you want, but how about
track.enabled = false
? Light stays on though, i.e. camera is still 'running'. -
Sam Dutton over 10 yearsThe MediaStreamTrack
stop()
method hasn't been implemented in Chrome or Firefox, though it's in the spec: dev.w3.org/2011/webrtc/editor/… -
Jonathan over 10 yearsExactly. That's a bummer, personally I rely quite much on my webcam light when I'm in applications. If its on I'm not convinced that It isn't used. I guess I have to stop the whole stream and then do a new getUserMedia call for now.
-
Jonathan over 10 yearsThanks for the enabled property tip, much better than removing/adding as I did before.
-
jamix over 10 yearsYeah, but as Sam Dutton mentioned,
track.enabled
doesn't make the camera light go off. -
William about 7 yearsI wonder how this design decision was motivated. Is there a discussion somewhere?
-
Pascut almost 4 yearsHave you found a way to make the camera light go off? I can't stop the tracks. I can't disable/remove them.
-
Aronanda almost 2 years