javascript getUserMedia - 如何检测设备是否真的有摄像头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12885896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:20:56  来源:igfitidea点击:

getUserMedia - how to detect if the device actually has a camera

javascriptcameramodernizr

提问by Nathan Russell

I'm playing with the html5/javascript getUserMedia api to write a js app that will use the device's camera if available. I'm using Modernizr to detect the capability (of the browser) like this:

我正在使用 html5/javascript getUserMedia api 编写一个 js 应用程序,如果可用,它将使用设备的相机。我正在使用 Modernizr 来检测(浏览器的)功能,如下所示:

if (Modernizr.getusermedia) {

And within the true block:

在真正的块内:

navigator.getUserMedia(
    {   // we would like to use video but not audio
        // This object is browser API specific! - some implementations require boolean properties, others require strings!
        video: true, 
        audio: false
    },
    function(videoStream) {
        // 'success' callback - user has given permission to use the camera
        // my code to use the camera here ... 
    },
    function() {
        // 'no permission' call back
        console.log("user did not give access to the camera");
    }               
);

This works fine. But what I've found is that the Modernizer.getUserMedia call returns true based on the browser supporting the api, and not whether the device actually has a camera or not.

这工作正常。但我发现 Modernizer.getUserMedia 调用根据支持 api 的浏览器返回 true,而不是设备是否真的有摄像头。

IE. on my MacBook with its iSight camera and a current version of Chrome, Modernizr.getUserMedia returns true, then navigator.getUserMedia(...) prompts for permission to use the camera. Excellent

IE。在我的带有 iSight 摄像头和当前版本的 Chrome 的 MacBook 上,Modernizr.getUserMedia 返回 true,然后 navigator.getUserMedia(...) 提示允许使用摄像头。优秀的

However, on another machine without a camera but with a current version of Chrome, Modernizr.getUserMedia returns true, which means that navigator.getUserMedia(...) prompts for permission to use the camera which the device doesn't have. Not so excellent!

但是,在另一台没有摄像头但使用当前版本的 Chrome 的机器上,Modernizr.getUserMedia 返回 true,这意味着 navigator.getUserMedia(...) 提示允许使用设备没有的摄像头。没那么优秀!

Does anyone know if its possible to detect the existance of a camera? Ideally I don't want to prompt the user for permission to access the camera if they dont have one!

有谁知道是否有可能检测到相机的存在?理想情况下,如果用户没有相机,我不想提示用户获得访问相机的权限!

Cheers

干杯

Nathan

弥敦道

回答by Kristjan Liiva

You can use MediaStreamTrack.getSources. This returns a list of video and audio devices connected to the PC. This does not require user permission.

您可以使用 MediaStreamTrack.getSources。这将返回连接到 PC 的视频和音频设备的列表。这不需要用户许可。

You can then pass the ID to getUserMedia to get the desired media device.

然后,您可以将 ID 传递给 getUserMedia 以获取所需的媒体设备。

回答by Vitaly D

This helped me:

这帮助了我:

function(videoStream) {
    // 'success' callback - user has given permission to use the camera
    if (videoStream.getVideoTracks().length > 0) {
        // my code to use the camera here ... 
    }
}

回答by Victor Canezin de Oliveira

You can use DetectRTC from Muaz Khan webrtc experiments: https://github.com/muaz-khan/WebRTC-Experiment/tree/master/DetectRTC

您可以使用 Muaz Khan webrtc 实验中的 DetectRTC:https: //github.com/muaz-khan/WebRTC-Experiment/tree/master/DetectRTC

Use:

利用:

DetectRTC.audioInputDevices
DetectRTC.audioOutputDevices
DetectRTC.videoInputDevices

to get de devices.

获取设备。

回答by Joseph Adams

The getUserMedia API is still quite fresh out of the press and will have some bugs and things to be improved, like this problem.

getUserMedia API 仍然很新鲜,会有一些错误和需要改进的地方,比如这个问题。

But at the moment I don't see a way for you to check if the computer actually has a camera. Though you could use Flash :-( to detect it I think...

但是目前我看不到您可以检查计算机是否真的有摄像头的方法。虽然你可以使用 Flash :-( 来检测它,我认为......