Javascript Chrome:navigator.mediaDevices.getUserMedia 不是一个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37315361/
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-08-23 20:11:56  来源:igfitidea点击:

Chrome: navigator.mediaDevices.getUserMedia is not a function

javascriptgoogle-chromewebrtc

提问by muninn9

I'm on localhost and trying to use the MediaDevices.getUserMedia method in Chrome. I receive the error as titled. I understand that in Chrome it is only possible to use this function with a secure origin and that localhost is considered a secure origin. Also, this works in Firefox.

我在本地主机上并尝试在 Chrome 中使用 MediaDevices.getUserMedia 方法。我收到标题为的错误。我知道在 Chrome 中只能使用具有安全来源的此功能,并且 localhost 被认为是安全来源。此外,这适用于 Firefox。

This is how I'm using it as shown on the Google Developers website https://developers.google.com/web/updates/2015/10/media-devices?hl=en:

这就是我使用它的方式,如 Google Developers 网站https://developers.google.com/web/updates/2015/10/media-devices?hl=en 所示

var constraints = window.constraints = {
            audio: false,
            video: true
};


navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
            callFactory.broadcastAssembly(stream);
            ...
});

回答by Amrendra

On some latest browsers navigator.getUserMediadoes not perform well. So, try using navigator.mediaDevices.getUserMedia. Or, better you check if navigator.mediaDevices.getUserMediais available for the browser use navigator.mediaDevices.getUserMediaor else use navigator.getUserMedia.

在一些最新的浏览器navigator.getUserMedia上表现不佳。因此,请尝试使用navigator.mediaDevices.getUserMedia。或者,最好检查一下是否navigator.mediaDevices.getUserMedia可用于浏览器navigator.mediaDevices.getUserMedia,否则使用navigator.getUserMedia.

navigator.getWebcam = (navigator.getUserMedia || navigator.webKitGetUserMedia || navigator.moxGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
if (navigator.mediaDevices.getUserMedia) {
    navigator.mediaDevices.getUserMedia({  audio: true, video: true })
    .then(function (stream) {
                  //Display the video stream in the video object
     })
     .catch(function (e) { logError(e.name + ": " + e.message); });
}
else {
navigator.getWebcam({ audio: true, video: true }, 
     function (stream) {
             //Display the video stream in the video object
     }, 
     function () { logError("Web cam is not accessible."); });
}

Hope this will solve your problem.

希望这能解决您的问题。

回答by Simon Malone

Try enabling: chrome://flags/#enable-experimental-web-platform-features

尝试启用:chrome://flags/#enable-experimental-web-platform-features

Worked for me in chromium

在铬为我工作

回答by Rohith S

I too had the same problem in my chrome browser. first check your phone is supported by testing it in https://test.webrtc.org/

我的 chrome 浏览器也遇到了同样的问题。首先通过在https://test.webrtc.org/ 中测试来检查您的手机是否受支持

if your phone passes all the cases, then check step 2

如果您的手机通过了所有情况,请检查第 2 步

step 2: If your hosting a webpage or running a third party webpage,see whether camera permissions are enabled on your phone.

第二步:如果您托管网页或运行第三方网页,请查看您的手机是否启用了摄像头权限。

Also the main issue is WEBRTC is not supported in HTTP site and it is supported only in HTTPS site

此外,主要问题是HTTP 站点不支持 WEBRTC,仅 HTTPS 站点支持

This is the https site which allows webThis is the http site which gives a error

这是允许 web 的 https 站点这是给出错误的 http 站点

回答by brijesh-pant

I got stuck in the same issue. One solution is to follow and download the extension Web Server for Chromeshared in the comment above by @ellerynz, or

我陷入了同样的问题。一种解决方案是关注并下载@ellerynz 在上述评论中共享的 Chrome扩展Web 服务器,或

if you have python installed you could also do

如果你安装了 python,你也可以这样做

python -m SimpleHTTPServer [port]

After you hit enter, you should see the following message:

按 Enter 键后,您应该会看到以下消息:

Serving HTTP on 0.0.0.0 port 8000 ...

Open the browser and put

打开浏览器并输入

http://127.0.0.1:[port]

回答by Philippe Sultan

Have you tried to include adapter.jspolyfill ? Check this page : https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#Browser_compatibility

您是否尝试过包含adapter.jspolyfill ?检查此页面:https: //developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#Browser_compatibility

It looks like this or enabling chrome://flags/#enable-experimental-web-platform-featuresas per @Simon Malone's note, is needed for Chrome.

看起来像这样或chrome://flags/#enable-experimental-web-platform-features按照@Simon Malone's note启用,是 Chrome 需要的。

回答by ellerynz

I was having this problem too and changing flags didn't seem to work. I came across this chrome extension — Web Server for Chromein Google's WebRTC tutorialwhich seemed to do the trick.

我也遇到了这个问题,更改标志似乎不起作用。我在 Google 的WebRTC 教程中遇到了这个 chrome 扩展 — Web Server for Chrome,它似乎可以解决问题。

回答by Adrian Ber

Use navigator.getUserMedia()instead.

使用navigator.getUserMedia()代替。

navigator.getUserMedia(constraints, successCallback, errorCallback);