按照网上的解决方案,是遍历摄像头设备,然后获取后置摄像头的ID,但是没效果。但是加入 constraints.video = { facingMode: "environment"}; 后置摄像头调用成功。代码如下:
var audioSource = null;
var videoSource = null;
navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
devices.forEach(function(device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId);
if (device.kind === "videoinput" && device.label.indexOf("back")>=0) {
alert("Camera found:"+ device.label +device.deviceId);
videoSource = device.deviceId;
}
var constraints = {
audio: false,
video: true,
};