的WebRTC。没有显示视频

时间:2016-12-13 18:10:05

标签: 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>

1 个答案:

答案 0 :(得分:2)

工作代码:

<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>