0

有没有人尝试在 microsoft edge 移动浏览器上从 iphone 相机捕获视频?它有效吗?navigator.mediaDevices返回我undefined,我想知道该浏览器是否根本不支持 mediaDevices API,或者它只是一个相机访问问题。

4

1 回答 1

0

请查看这篇文章,如果当前文档没有安全加载或者在旧浏览器中使用新的 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);
});

我已经在IOS 13.4版本和使用Microsoft Edge 44.13.7版本上重现了该问题,使用上述polyfill后,此错误消失了。

于 2020-04-07T09:25:55.900 回答