我正在尝试在getUserMedia的帮助下访问iOS Safari上的麦克风。下面你可以找到我的代码片段。
if (navigator.mediaDevices === undefined) {
navigator.mediaDevices = {};
}
if (navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = function(constraints) {
// First get ahold of the legacy getUserMedia, if present
let getUserMedia = navigator.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
}).then(function(stream) {
successCallBack(.......);
}).catch(function(error) {
debug.log(error);
..........
});
然而,promise总是捕获错误,更具体地说,是OverConstraintError。
{message: "Invalid constraint", constraint: ""}这种行为对于iOS Safari来说是独一无二的,在所有其他浏览器(Chrome,火狐,Safari osX)上它都可以正常工作。实际上,我的问题很像这个=> How to resolve iOS 11 Safari getUserMedia "Invalid constraint" issue,但我并没有尝试使用相机。我唯一感兴趣的是麦克风。
我测试的是一个真实的iPhone (5和X,都更新到了最新版本),所以它没有链接到iPhone模拟器。
将授予对麦克风的访问权限,并且还会显示请求权限的弹出窗口,因此这不是权限问题。
发布于 2018-11-13 09:26:04
此问题可能与标题为getUserMedia fails an OverConstrainedError when no devices are found的错误报告有关
https://bugs.webkit.org/show_bug.cgi?id=177126
这是在Codepen中运行的代码。正如您所说的,音频已启用并正常工作。注意,enumerateDevices()调用返回一个空数组。正如bug所述,这会在您的问题中导致错误:
.catch(function(error) {
navigator.mediaDevices.enumerateDevices().then(devices=>{console.log(devices)});
console.log('error: ',error);
});发布于 2018-11-14 06:02:34
web或getusermedia有一些问题,它并不像你预期的那样在所有平台上都能工作-在摄像头检测方面也有同样的问题,比如在三星s5中,同样的代码工作得很好,但在较新的设备上它就失败了。
我的建议是使用webrtc适配器js。尝试简单地包含以下脚本:
<script src="https://cdnjs.cloudflare.com/ajax/libs/webrtc-adapter/6.4.0/adapter.js" type="text/javascript"></script>在使用getusermedia api之前。我认为99%的问题都会消失。
https://stackoverflow.com/questions/53150556
复制相似问题