1

我正在尝试按照本指南以 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);

我想知道我是否遗漏了什么或有什么改变,因为到目前为止没有一个示例代码对我有用。

4

1 回答 1

1

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

于 2013-03-11T09:12:10.360 回答