WebRTC getUserMedia 承诺 api 支持 Chrome

WebRTC getUserMedia promise api support in Chrome

chrome 是否支持基于承诺的 API WebRTC?我无法在 Chrome.

中获得基于 API 承诺的 getUserMedia()
<!DOCTYPE html>
<html>
    <head>
        <title> Mitel WebRTC client </title>
        <script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
        <script src='dist/webrtc.min.js'></script>

        <script type="text/javascript">
            function startUp() {

                var options = {
                    audio: true,
                    video: true
                };
                if (getUserMedia) {
                    getUserMedia(options)
                    .then(function (stream) {
                        console.log("Acquired audio and video!");
                    })
                    .catch(function (err) {
                        console.log(err.name + ": " + err.message);
                    });
                } else {
                    alert("WebRTC not supported on this browser");
                }
            }
        </script>
    </head>

    <body onload="startUp();">
        <h1>WebRTC Promise API Client Application</h1>
    </body>
</html>

在控制台上,我看到以下错误

This appears to be Chrome
adapter-latest.js:32 chrome: {"audio":true,"video":true}
adapter-latest.js:410 Uncaught TypeError: Failed to execute 'webkitGetUserMedia' on 'Navigator': The callback provided as parameter 2 is not a function.

我想使用基于承诺的 API。我错过了什么吗?

它还没有在 Chrome 中实现,但是如果你使用官方的 adapter.js WebRTC polyfill: https://jsfiddle.net/srn9db4h/

var constraints = { video: true, audio: true };

navigator.mediaDevices.getUserMedia(constraints)
  .then(stream => video.srcObject = stream)
  .catch(e => console.error(e));

Firefox 和 Edge 原生支持 FWIW。

更新: Chrome (50) 现在似乎支持这个。而Chrome52甚至支持srcObject.

要访问 navigator.mediaDevices,您必须使用 HTTPS 连接来连接您的站点。无法使用 HTTP 访问此功能。

https://developers.google.com/web/fundamentals/media/capturing-images/

Warning: Direct access to the camera is a powerful feature. It requires consent from the user, and your site MUST be on a secure origin (HTTPS).

虽然不推荐这样做,但您仍然可以尝试通过禁用媒体安全来测试您的项目。

chrome://flags/#unsafely-treat-insecure-origin-as-secure

您可以添加您的 IP,chrome 会将其视为安全的。

如果您运行宁Chrome您的应用程序在 http.如果是这样,您应该 运行 您的应用程序为 https。 Chrome.

仅支持带 http 的本地主机 url
`http://jsfiddle.net/jib1/srn9db4h/ `
// not working

`https://jsfiddle.net/jib1/srn9db4h/`
//working with https