javascript - Webrtc 后置摄像头在 iOS 11 中无法工作

标签 javascript jquery ios webrtc getusermedia

下面是我的代码,在带有前置摄像头的 android 和 ios 11 中运行良好,而当我使用后置摄像头时,它会出现错误

以下作品

video: { width: 400, height: 200, facingMode: "user"  }

以下不起作用

video: { width: 400, height: 200, facingMode: "environment"  }

完整代码

**

var constraints = window.constraints = {
  audio: false,
  video: { width: 400, height: 200, facingMode: "environment"  }
};
function handleSuccess(stream) {
  var videoTracks = stream.getVideoTracks();
  console.log('Got stream with constraints:', constraints);
  console.log('Using video device: ' + videoTracks[0].label);
  stream.oninactive = function() {
    console.log('Stream inactive');
  };
  window.stream = stream; // make variable available to browser console
  video.srcObject = stream;
}
function handleError(error) {
  if (error.name === 'ConstraintNotSatisfiedError') {
    errorMsg('The resolution ' + constraints.video.width.exact + 'x' +
        constraints.video.width.exact + ' px is not supported by your device.');
  } else if (error.name === 'PermissionDeniedError') {
    errorMsg('Permissions have not been granted to use your camera and ' +
      'microphone, you need to allow the page access to your devices in ' +
      'order for the demo to work.');
  }
  errorMsg('getUserMedia error: ' + error.name, error);
}
function errorMsg(msg, error) {
  errorElement.innerHTML += '<p>' + msg + '</p>';
  if (typeof error !== 'undefined') {
    console.error(error);
  }
}
navigator.mediaDevices.getUserMedia(constraints).
    then(handleSuccess).catch(handleError);

**

最佳答案

目前,Apple iOS11 下的getUserMedia不支持widthheight约束。

使用后置摄像头的唯一方法是满足以下限制:

var constraints = window.constraints = {
   audio: false,
   video: { facingMode: 'environment' }
 };

我为此在 Apple 中打开了一个错误,但它作为重复项被关闭。 请密切关注 Apple 关于此问题的更新。

关于javascript - Webrtc 后置摄像头在 iOS 11 中无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49206667/

相关文章:

.net - 使用 Jquery Mobile 使用 WCF 服务返回 400 错误请求

ios - 如何为 WebRTC iOS 框架启用 Bitcode?

mysql - iOS 应用程序开发 - 数据库

javascript - 两个ajax表单不能同时工作

javascript - 下拉字段的 onClick 远程验证

javascript - Rails xhr.send( ( options.hasContent && options.data ) || null );错误

jquery - 将一个 50 字段的表单提交到多个表;常规 POST、AJAX POST 或其他?

javascript - 仅将 jquery 应用于单击的子元素

Swift 中的 iOS 简单 SQLite Select 语句

javascript - 如何仅在特定屏幕尺寸之前和之后运行此 jquery 代码?