在HTML5中从Web浏览器捕获视频

时间:2013-03-10 12:44:04

标签: html5

我正在尝试按照HTML5中从网络摄像头捕获视频的指南

http://www.html5rocks.com/en/tutorials/getusermedia/intro/

我复制并粘贴了以下代码,但Chrome并未要求获得使用相机的许可

<video autoplay></video>

<script>
  var onFailSoHard = function(e) {
    console.log('Reeeejected!', e);
  };

  // Not showing vendor prefixes.
  navigator.getUserMedia({video: true, audio: true}, function(localMediaStream) {
    var video = document.querySelector('video');
    video.src = window.URL.createObjectURL(localMediaStream);

    // Note: onloadedmetadata doesn't fire in Chrome when using it with getUserMedia.
    // See crbug.com/110938.
    video.onloadedmetadata = function(e) {
      // Ready to go. Do some stuff.
    };
  }, onFailSoHard);
</script>

当我点击指南中的“捕捉视频”时,我的网络摄像头显示......

另一个网站有类似的代码,但又一次它不适合我

http://dev.opera.com/articles/view/playing-with-html5-video-and-getusermedia-support/

<!-- HTML code -->
<video id="sourcevid" autoplay>Put your fallback message here.</video>

/* JavaScript code */
window.addEventListener('DOMContentLoaded', function() {
    // Assign the <video> element to a variable
    var video = document.getElementById('sourcevid');

    // Replace the source of the video element with the stream from the camera
    if (navigator.getUserMedia) {
        navigator.getUserMedia('video', successCallback, errorCallback);
        // Below is the latest syntax. Using the old syntax for the time being for backwards compatibility.
        // navigator.getUserMedia({video: true}, successCallback, errorCallback);
        function successCallback(stream) {
            video.src = stream;
        }
        function errorCallback(error) {
            console.error('An error occurred: [CODE ' + error.code + ']');
            return;
        }
    } else {
        console.log('Native web camera streaming (getUserMedia) is not supported in this browser.');
        return;
    }
}, false);

我想知道我是否遗漏了某些东西或者有些变化,因为到目前为止,没有任何示例代码对我有效。

1 个答案:

答案 0 :(得分:1)

了解发生了什么。对于任何想知道的人,在Chrome上,如果从服务器运行,您只能访问网络摄像头。它不仅适用于文件。