手机WebRtc调用后置摄像头
按照网上的解决方案,是遍历摄像头设备,然后获取后置摄像头的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,
};
constraints.video = {
optional: [{
sourceId: videoSource
}]
};
constraints.video = { facingMode: "environment"};
navigator.getUserMedia(constraints, function (stream) {
var video = document.querySelector('video');
video.srcObject = stream;
}, function (error) {
console.log("Raised an error when capturing:", error);
});
});
})
.catch(function(err) {
console.log(err.name + ": " + err.message);
});
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
