How to completely turn off camera on mediastream javascript

Viewed 8937

im trying to create a video conference web app..

the problem is im trying to disable my camera on the middle conference, its work but my laptop camera indicator still on (the light is on) but on my web, video show blank screen, is that normal or i miss something?

here what i try

videoAction() {
  navigator.mediaDevices.getUserMedia({
    video: true,
    audio: true
  }).then(stream => {
     this.myStream = stream
  })

  this.myStream.getVideoTracks()[0].enabled = !(this.myStream.getVideoTracks()[0].enabled)
  this.mediaStatus.video = this.myStream.getVideoTracks()[0].enabled
}
3 Answers

There is also a stop() method which should do the trick in Chrome and Safari. Firefox should already mark the camera as unused by setting the enabled property.

this.myStream.getVideoTracks()[0].stop();

Firstly, MediaStreamTrack.enabled is a Boolean, so you can simply assign the value false.
To simplify your code, you might call:

var vidTrack = myStream.getVideoTracks();
vidTrack.forEach(track => track.enabled = false);

When MediaStreamTrack.enabled = false, the track passes empty frames to the stream, which is why black video is sent. The camera/source itself is not stopped-- I believe the webcam light will turn off on Mac devices, but perhaps not on Windows etc.

.stop(), on the other hand, completely stops the track and tells the source it is no longer needed. If the source is only connected to this track, the source itself will completely stop. Calling .stop() will definitely turn off the camera and webcam light, but you won't be able to turn it back on in your stream instance (since its video track was destroyed). Therefore, completely turning off the camera is not what you want to do; just stick to .enabled = false to temporarily disable video and .enabled = true to turn it back on.

We need to assign window.localStream = stream; inside navigator.mediaDevices.getUserMedia method

For Stop Webcam and LED light off.

localStream.getVideoTracks()[0].stop();
video.src = '';

Related