【问题标题】: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,或者它只是一个摄像头访问问题。
【问题讨论】:
标签:
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后,该错误消失了。