【问题标题】:Accessing the Device Camera with getUserMedia使用 getUserMedia 访问设备摄像头
【发布时间】:2016-10-28 07:41:44
【问题描述】:

在我添加了一些额外的代码之前,以下代码一直有效,因为我试图让用户知道他们的相机是否准备好/可用。我的 html 看起来像:

<div id="notes">
</div>
<div id="video-container">
<video id="camera-stream" width="500" autoplay></video>
</div>

这里是 .js

<script type="text/javascript">
    window.onload = function() {
    var statusHTML = '<ul>';
  navigator.getUserMedia = (navigator.getUserMedia ||
                            navigator.webkitGetUserMedia ||
                            navigator.mozGetUserMedia || 
                            navigator.msGetUserMedia);
if (navigator.getUserMedia) {
  navigator.getUserMedia(
    {
      video: true,
      audio: true
    },
    function(localMediaStream) {
statusHTML += '<li>You have a camera!</li>';
var vid = document.getElementById('camera-stream');
vid.src = window.URL.createObjectURL(localMediaStream);
    },
    function(err) {
  statusHTML += '<li>You dont have a camera!</li>';
      console.log('The following error occurred when trying to use getUserMedia: ' + err);
    }
  );
  statusHTML += '</ul>';
  document.getElementById('notes').innerHTML = statusHTML;
} else {
  alert('Sorry, your browser does not support getUserMedia');
}
}
</script>

我添加了这 4 个不起作用的代码,我不确定我是否做得对:

  var statusHTML = '<ul>';
  statusHTML += '<li>You have a camera!</li>';
  statusHTML += '<li>You dont have a camera!</li>';
  statusHTML += '</ul>';
  document.getElementById('notes').innerHTML = statusHTML;

总而言之,我试图在用户加载页面后立即向页面打印一条消息,它应该说他们是否有可用的相机。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript html getusermedia


    【解决方案1】:

    尝试直接更改innerHTML,而不是在带有变量的函数之后。我试过了,效果很好:

    window.onload = function() {
        navigator.getUserMedia = (navigator.getUserMedia ||
                                navigator.webkitGetUserMedia ||
                                navigator.mozGetUserMedia || 
                                navigator.msGetUserMedia);
        if (navigator.getUserMedia) {
            navigator.getUserMedia(
            {
              video: true,
              audio: true
            },
            function(localMediaStream) {
              document.getElementById('notes').innerHTML = '<ul><li>You have a camera!</li></ul>';
              var vid = document.getElementById('camera-stream');
              vid.src = window.URL.createObjectURL(localMediaStream);
            },
            function(err) {
              document.getElementById('notes').innerHTML = '<ul><li>You dont have a camera!</li></ul>';
              alert('The following error occurred when trying to use getUserMedia: ' + err);
            }
            );
        } else {
          alert('Sorry, your browser does not support getUserMedia');
        }
    }
    

    【讨论】:

    • 非常感谢您的宝贵时间。这与我正在寻找的非常接近。您认为是否可以在通知之前显示消息(想要使用您的麦克风和摄像头的网站)。
    • 你的意思是你想在其他所有事情之前提醒你的消息:'想要使用你的麦克风和摄像头的网站'?
    • 很简单,在onload函数的第一行,加上alert('site that wants to use your microphone and camera');
    猜你喜欢
    • 2020-11-26
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    相关资源
    最近更新 更多