下面是我的代码,可以在带有前置摄像头的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有关此问题的更新。

09-25 16:57