停止/关闭由navigator.mediaDevices.getUserMedia打开的网络摄像头流 [英] Stop/Close webcam stream which is opened by navigator.mediaDevices.getUserMedia

查看:2220
本文介绍了停止/关闭由navigator.mediaDevices.getUserMedia打开的网络摄像头流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I opened a webcam by using the following JavaScript code:

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

是否有任何JavaScript代码可以停止或关闭网络摄像头?谢谢大家.

Is there any JavaScript code to stop or close the webcam? Thanks everyone.

推荐答案

编辑

由于最初发布了此答案,因此浏览器API已更改. .stop()在传递给回调的流上不再可用. 开发人员将必须访问组成流(音频或视频)的轨道,并分别停止每个轨道.

Since this answer has been originally posted the browser API has changed. .stop() is no longer available on the stream that gets passed to the callback. The developer will have to access the tracks that make up the stream (audio or video) and stop each of them individually.

此处的更多信息:示例(来自上面的链接):

Example (from the link above):

stream.getTracks().forEach(function(track) {
  track.stop();
});

浏览器支持可能会有所不同.

Browser support may differ.

原始答案

navigator.getUserMedia在成功回调中为您提供了一个流,您可以在该流上调用.stop()停止记录(至少在Chrome中,似乎FF不喜欢它)

navigator.getUserMedia provides you with a stream in the success callback, you can call .stop() on that stream to stop the recording (at least in Chrome, seems FF doesn't like it)

这篇关于停止/关闭由navigator.mediaDevices.getUserMedia打开的网络摄像头流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆