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

17,809

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;
}
Share:
17,809

Related videos on Youtube

Jonathan
Author by

Jonathan

Updated on June 04, 2022

Comments

  • Jonathan
    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.

    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.

  • Jonathan
    Jonathan over 10 years
    No, 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
    Sam Dutton over 10 years
    Ah -- 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
    Sam Dutton over 10 years
    The MediaStreamTrack stop() method hasn't been implemented in Chrome or Firefox, though it's in the spec: dev.w3.org/2011/webrtc/editor/…
  • Jonathan
    Jonathan over 10 years
    Exactly. 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
    Jonathan over 10 years
    Thanks for the enabled property tip, much better than removing/adding as I did before.
  • jamix
    jamix over 10 years
    Yeah, but as Sam Dutton mentioned, track.enabled doesn't make the camera light go off.
  • William
    William about 7 years
    I wonder how this design decision was motivated. Is there a discussion somewhere?
  • Pascut
    Pascut almost 4 years
    Have you found a way to make the camera light go off? I can't stop the tracks. I can't disable/remove them.
  • Aronanda
    Aronanda almost 2 years