navigator.mediaDevices 在 Microsoft Edge 移动版中 ios 13.3.1

navigator.mediaDevices in microsoft edge mobile ios 13.3.1

有没有人试过在 Microsoft Edge 移动浏览器上从 iphone 摄像头捕捉视频?它有效吗? navigator.mediaDevices returns 我 undefined 我想知道该浏览器是否根本不支持 mediaDevices API,或者这只是相机访问问题。

请检查 this article,如果当前文档未安全加载,或者如果在旧版浏览器中使用新的 MediaDevices API,则 navigator.mediaDevices 可能未定义。因此,尝试检查浏览器版本并清除浏览器数据,然后重新测试代码。

此外,在使用navigator.mediaDevices之前,您可以尝试添加以下polyfill:

// Older browsers might not implement mediaDevices at all, so we set an empty object first
if (navigator.mediaDevices === undefined) {
  navigator.mediaDevices = {};
}

// Some browsers partially implement mediaDevices. We can't just assign an object
// with getUserMedia as it would overwrite existing properties.
// Here, we will just add the getUserMedia property if it's missing.
if (navigator.mediaDevices.getUserMedia === undefined) {
  navigator.mediaDevices.getUserMedia = function(constraints) {

    // First get ahold of the legacy getUserMedia, if present
    var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

    // Some browsers just don't implement it - return a rejected promise with an error
    // to keep a consistent interface
    if (!getUserMedia) {
      return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
    }

    // Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
    return new Promise(function(resolve, reject) {
      getUserMedia.call(navigator, constraints, resolve, reject);
    });
  }
}

navigator.mediaDevices.getUserMedia({ audio: true, video: true })
.then(function(stream) {
  var video = document.querySelector('video');
  // Older browsers may not have srcObject
  if ("srcObject" in video) {
    video.srcObject = stream;
  } else {
    // Avoid using this in new browsers, as it is going away.
    video.src = window.URL.createObjectURL(stream);
  }
  video.onloadedmetadata = function(e) {
    video.play();
  };
})
.catch(function(err) {
  console.log(err.name + ": " + err.message);
});

我在IOS13.4版本上重现了这个问题,使用Microsoft Edge 44.13.7版本,使用上面的polyfill后,这个错误就消失了。