我决定学习WebRTC,但不显示视频。请帮忙。
我究竟做错了什么?使用Chrome
我的代码:

<html>

<head>
    <meta charset="UTF-8">
</head>

<body>
    <script>
        window.onload = function () {
            navigator.webkitGetUserMedia({ video: true }, getStream, noStream);
        };

        function getStream(stream) {
            var url = window.webkitURL.createObjectURL(stream);
            var video = document.getElementById('video');
            video.src = url;
        }

        function noStream(faild) {

        }
    </script>
    <video id="video" autoplay="autoplay" width="400"></video>
</body>

</html>

最佳答案

工作代码:

<head>
    <meta charset="UTF-8"></script>
</head>

<body>
    <script>
        window.onload = function () {
            var constraints = { audio: true, video: { width: 1280, height: 720 } };

navigator.mediaDevices.getUserMedia(constraints)
.then(function(mediaStream) {
  var video = document.querySelector('video');
  video.srcObject = mediaStream;
  video.onloadedmetadata = function(e) {
    video.play();
  };
})
.catch(function(err) { console.log(err.name + ": " + err.message); });
        };
    </script>
    <video id="video" autoplay="autoplay" width="400"></video>
</body>

</html>

关于javascript - WebRTC。不显示视频,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41127828/

10-11 06:55