在iOS Safari上借助getUsermedia访问麦克风

Joe*_*ers 5 javascript safari ios

我试图在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);
  ..........
});
Run Code Online (Sandbox Code Playgroud)

然而,承诺总是会出现错误,更具体地说是OverConstraintError.

{message: "Invalid constraint", constraint: ""}
Run Code Online (Sandbox Code Playgroud)

这种行为对于iOS Safari来说是独一无二的,在所有其他浏览器(Chrome,Firefox,Safari osX)上,它的运行没有任何问题.实际上我的问题很像这一个很重要=> 如何解决iOS 11 Safari getUserMedia"无效约束"问题,但我并没有尝试使用相机.我唯一感兴趣的是麦克风.

我正在测试一个真正的iPhone(5和X,都更新到最新版本),所以它没有链接到iPhone模拟器.

授予对麦克风的访问权限,并且还显示请求权限的弹出窗口,因此它不是权限问题.

Ran*_*urn 0

此问题可能与此错误报告相关,标题为“当找不到设备时,getUserMedia 失败并出现 OverConstrainedError”

https://bugs.webkit.org/show_bug.cgi?id=177126

这是您在 Codepen 中运行的代码。正如您所说,音频已启用并且可以工作。请注意,该enumerateDevices()调用返回一个空数组。正如错误所述,这会导致您的问题出现错误:

.catch(function(error) {
  navigator.mediaDevices.enumerateDevices().then(devices=>{console.log(devices)});
  console.log('error: ',error);
});
Run Code Online (Sandbox Code Playgroud)

https://codepen.io/anon/pen/rQWRyZ?editors=0011