首页 > 解决方案 > 一切看起来都很好,但 getUserMedia 仍然无法正常工作

问题描述

一切看起来都很好。那么为什么我的代码不起作用?它甚至不请求许可。

这是我的代码。

var video = document.getElementById("video")

window.onload = () => {

if(navigator.mediaDevices){
  navigator.mediaDevices.getUserMedia({
    video: {
      height: {
        max: 2460,
        ideal: 1690,
        min:1080
      },
      width: {
        max: 1940,
        ideal: 1260,
        min: 720
      },
      facingMode: 'user'
    },
    audio:false
  })
  .then(stream => {
    
    video.srcObject = stream
  }).catch(err){
    document.write(err.message + "\nPlease allow camera access to use this site...")
  }
}
else{
  alert("Your device doesn't support this feature")
  document.write("Your browser doesn't support this feature")
}
}

我也使用过和不使用这个:

var mediaDevices = mediaDevices || webkitMediaDevices || mozMediaDevices

和这个:

var mediaDevices = navigator.mediaDevices || navigator.webkitMediaDevices || navigator.mozMediaDevices 

仍然无法正常工作

标签: javascriptgetusermedianavigatormediadevices

解决方案


推荐阅读