WebRTC。不显示视频

Vik*_*bas 2 javascript video webrtc

我决定学习 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>
Run Code Online (Sandbox Code Playgroud)

Vik*_*bas 7

工作代码:

<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>
Run Code Online (Sandbox Code Playgroud)