【问题标题】:navigator.mediaDevices in microsoft edge mobile ios 13.3.1microsoft edge mobile ios 13.3.1 中的 navigator.mediaDevices
【发布时间】:2020-03-03 13:32:54
【问题描述】:

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

【问题讨论】:

  • 欢迎来到本站!如果您可以为您的代码提供一些上下文,这将非常有帮助,因为它可能会发生其他事情。 Read about it
  • 你可以查看MediaDevices Browser compatibility,看看它是否支持Mobile IOS。此外,正如 tfrascaroli 所说,最好发布相关代码或创建一个 Minimal,Reproducible Example,这样我们更容易重现问题并缩小问题范围。感谢理解。

标签: javascript iphone microsoft-edge mediadevices ios13.3


【解决方案1】:

请检查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);
});

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

【讨论】:

    猜你喜欢
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    相关资源
    最近更新 更多