用户授予权限后自动获取用户网络摄像头访问权限

时间:2020-09-13 11:56:36

标签: javascript html html5-video

我使用javascript创建了一个带有用户视频div的页面。因此,一旦用户授予许可,它将在网站一角显示用户网络摄像头。因此,当我再次访问任何其他页面时,我面临的问题是我需要征求用户许可。因此,一旦获得用户许可,有什么方法可以使用户自动访问网络摄像头。

<div id="videoContainer">
    <video autoplay="true" id="videoElement"></video>
</div>
<script>
var video = document.querySelector("#videoElement");
var next_button = document.getElementById("button_1");


if (navigator.mediaDevices.getUserMedia) {
  navigator.mediaDevices.getUserMedia({ audio: true,video: true })
    .then(function (stream) {
      video.srcObject = stream;
      next_button.classList.remove("disabled");
      localStorage.setItem("camera_access", "granded");
    })
    .catch(function (error) {
        console.log(error);
        localStorage.setItem("camera_access", "declined");
    });
}
</script>

如果用户授予权限,我尝试的操作将保存在本地存储中。但是使用此值,我找不到访问网络摄像头的方法。请给我一些想法。 我认为我可以使用ajax并加载页面,但这会很困难,因为其他页面正在加载参数和全部。

0 个答案:

没有答案