navigator.getusermedia

时间:2012-09-13 13:22:38

标签: html5

我正在玩html5新规格,正是网络摄像头的功能。

following this tutorial。我收到以下错误:

Native web camera streaming (getUserMedia) is not supported in this browser. 
这个if语句采用了

if (navigator.getUserMedia)

现在,我确信我的浏览器启用了navigator.getUserMedia,因为这些示例here完美无缺

所以,我用if修改了if中的代码:

if (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia)

但是现在,我收到了一个javascript错误:

Uncaught TypeError: Object #<Navigator> has no method 'getUserMedia' 

在这一行:

navigator.getUserMedia('video', successCallback, errorCallback);

这真的没有意义!它正在我发布的最后一个链接上工作!

有什么想法吗?

提前致谢。

3 个答案:

答案 0 :(得分:18)

如果您正在测试navigator.getUserMedianavigator.webkitGetUserMedianavigator.mozGetUserMedianavigator.msGetUserMedia,则无法保证navigator.getUserMedia()可用。它可能是其他三个中的任何一个。您可以尝试这样的事情(来自getUserMedia.js):

navigator.getUserMedia_ = (   navigator.getUserMedia
                           || navigator.webkitGetUserMedia 
                           || navigator.mozGetUserMedia 
                           || navigator.msGetUserMedia);

if ( !! navigator.getUserMedia_) {
    navigator.getUserMedia_('video', successCallback, errorCallback);
    //The rest of your code

答案 1 :(得分:3)

这是一项新技术。 Yoy必须有Firefox / Chrome / Opera浏览器,必须更新。然后,试试这个:

function showCamera()  {   var streaming = false,
      video        = window.content.document.createElement("video"),
      cover        = window.content.document.createElement("div"),
      canvas       = window.content.document.createElement("canvas"),
      photo        = window.content.document.createElement("img"),
      startbutton  = window.content.document.createElement("button"),
      width = 320,
      height = 0;

  photo.src = "http://placekitten.com/g/320/261";   window.content.document.body.insertBefore(video, window.content.document.body.firstChild);

  var navigator = window.navigator;
    navigator.getMedia = ( navigator.getUserMedia ||
                         navigator.webkitGetUserMedia ||
                         navigator.mozGetUserMedia ||
                         navigator.msGetUserMedia);
    navigator.getMedia(
    {
      video: true,
      audio: false
    },
    function(stream) {
      if (navigator.mozGetUserMedia) {
        video.mozSrcObject = stream;
      } else {
        var vendorURL = window.URL || window.webkitURL;
        video.src = vendorURL.createObjectURL(stream);
      }
      video.play();
    },
    function(err) {
      console.log("An error occured! " + err);
    }   );
    video.addEventListener('canplay', function(ev){
    if (!streaming) {
      height = video.videoHeight / (video.videoWidth/width);
      video.setAttribute('width', width);
      video.setAttribute('height', height);
      canvas.setAttribute('width', width);
      canvas.setAttribute('height', height);
      streaming = true;
    }   }, false);
    function takepicture() {
    canvas.width = width;
    canvas.height = height;
    canvas.getContext('2d').drawImage(video, 0, 0, width, height);
    var data = canvas.toDataURL('image/png');
    photo.setAttribute('src', data);   }
    startbutton.addEventListener('click', function(ev){
      takepicture();
    ev.preventDefault();   }, false);   }


showCamera();

如果您的浏览器是Firefox但仍无法正常工作,请转到about:config并设置/添加一个名为media.navigator.enabled的真值的布尔变量

来源:https://developer.mozilla.org/en-US/docs/WebRTC/Taking_webcam_photos

P / d:我在Greasemonkey脚本中使用了这段代码并且它有效。我对原始代码的第一行进行了一些更改。

答案 2 :(得分:0)

navigator.getUserMedia()已过时。参见MDN。

https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getUserMedia

改为使用navigator.mediaDevices.getUserMedia(constraints);

https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia