我使用以下JavaScript代码打开了一个网络摄像头:

const stream = await navigator.mediaDevices.getUserMedia({ /* ... */ });

是否有任何JavaScript代码停止或关闭网络摄像头?


当前回答

下面的代码适用于我:

public vidOff() {

      let stream = this.video.nativeElement.srcObject;
      let tracks = stream.getTracks();

      tracks.forEach(function (track) {
          track.stop();
      });

      this.video.nativeElement.srcObject = null;
      this.video.nativeElement.stop();
  }

其他回答

启动和停止网络摄像头,(更新2020 React es6)

开启Web摄像头

stopWebCamera =()=>

       //Start Web Came
      if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
        //use WebCam
        navigator.mediaDevices.getUserMedia({ video: true }).then(stream => {
          this.localStream = stream;
          this.video.srcObject = stream;
          this.video.play();
        });
      }
 }

停止网络摄像头或视频播放一般

stopVideo =()=>
{
        this.video.pause();
        this.video.src = "";
        this.video.srcObject = null;

         // As per new API stop all streams
        if (this.localStream)
          this.localStream.getTracks().forEach(track => track.stop());
}

停止网络摄像头功能,即使视频流:

  this.video.src = this.state.videoToTest;
  this.video.play();

请检查这个:https://jsfiddle.net/wazb1jks/3/

导航器。getUserMedia(mediaConstraints,函数(流){ 窗口。streamReference =流; }, onMediaError);

停止记录

函数stopStream() { if (!window.streamReference)返回; window.streamReference.getAudioTracks () .forEach(函数(跟踪){ track.stop (); }); window.streamReference.getVideoTracks () .forEach(函数(跟踪){ track.stop (); }); 窗口。streamReference = null; }

使用不同的浏览器启动网络摄像头视频

对于Opera 12

window.navigator.getUserMedia(param, function(stream) {
                            video.src =window.URL.createObjectURL(stream);
                        }, videoError );

Firefox夜间18.0

window.navigator.mozGetUserMedia(param, function(stream) {
                            video.mozSrcObject = stream;
                        }, videoError );

针对Chrome 22

window.navigator.webkitGetUserMedia(param, function(stream) {
                            video.src =window.webkitURL.createObjectURL(stream);
                        },  videoError );

使用不同浏览器停止摄像头视频

对于Opera 12

video.pause();
video.src=null;

Firefox夜间18.0

video.pause();
video.mozSrcObject=null;

针对Chrome 22

video.pause();
video.src="";

有了这个,网络摄像头的灯每次都要熄灭……

有溪流形式成功的参考吗

var streamRef;

var handleVideo = function (stream) {
    streamRef = stream;
}

//this will stop video and audio both track
streamRef.getTracks().map(function (val) {
    val.stop();
});

当通过http连接时,在流上使用.stop()在chrome上工作。它在使用ssl (https)时不起作用。