Jonathan
Jonathan

Reputation: 3034

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.

Upvotes: 13

Views: 23230

Answers (4)

zahra_oveyedzade
zahra_oveyedzade

Reputation: 1220

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;
}

Upvotes: 1

Anand S
Anand S

Reputation: 1138

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()

Upvotes: 16

jamix
jamix

Reputation: 5638

It looks like the proper way to deal with this issue is to stop your MediaStream, recreate (and reattach) it as an audio-only one and then renegotiate the PeerConnection session. Unfortunately, Firefox currently doesn't support renegotiation mid-session.

The only viable hack is thus to also recreate the PeerConnection with the new MediaStream as suggested here (see "Adding video mid-call").

Upvotes: 0

Sam Dutton
Sam Dutton

Reputation: 15279

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.

Upvotes: 0

Related Questions