Javascript HTML5视频的GetUserMedia在chrome中没有选择面向用户的摄像头
因此,我的任务是使用HTML5视频元素创建一个演示,以获得用户的图片,然后在服务器端进行一些处理。在Surface Pro 4上测试似乎忽略了navigator.mediaDevices.getUserMedia函数中的facingMode:“用户”选项Javascript HTML5视频的GetUserMedia在chrome中没有选择面向用户的摄像头,javascript,html,Javascript,Html,因此,我的任务是使用HTML5视频元素创建一个演示,以获得用户的图片,然后在服务器端进行一些处理。在Surface Pro 4上测试似乎忽略了navigator.mediaDevices.getUserMedia函数中的facingMode:“用户”选项 navigator.mediaDevices.getUserMedia({ audio: false, video: { facingMode: "user" } }) .then(handleSuccess, stopCame
navigator.mediaDevices.getUserMedia({ audio: false, video: { facingMode: "user" } })
.then(handleSuccess, stopCameras);
var handleSuccess = (stream) => {
customer.srcObject = stream;
videotracks = stream.getVideoTracks();
}
handleSuccess调用流上的getVideoTracks,但阵列中只有后向摄像头
如何确保这只针对面向用户的摄像头?您需要首先枚举摄像头的数量,并将其保存在一个变量中,如下所示:
var camcounter; // camera counter
var camera_id = {}; // array of cameras by device id
navigator.mediaDevices.enumerateDevices()
.then(function(myStream) {
myStream.forEach(function(myStream) {
if(myStream.kind === 'videoinput') {
camcounter++; //camera count
camera_id.push(myStream.deviceId); //pushing the device ID to the array
console.log(myStream.kind + ": " + myStream.label + " id = " + myStream.deviceId);
}
});
})
.then(function(myStream) {
if(camcounter === 1) { //building mediaContraints if just one camera found
console.log('one camera profile');
mediaConstraints = {
video: {deviceId: {exact: camera_id[0]}},
audio: {mandatory: {echoCancellation: true, googAutoGainControl: true, googNoiseSuppression: true, googHighpassFilter: true, googTypingNoiseDetection: true}}
};
}
else {
if(camcounter === 2) { //building mediaContraints if two cameras found
mediaConstraints = {
video: {deviceId: {exact: camera_id[1]}},
audio: {mandatory: {echoCancellation: true, googAutoGainControl: true, googNoiseSuppression: true, googHighpassFilter: true, googTypingNoiseDetection: true}}
};
}
else {
console.log('no camera or more than 2 cameras');
}
}
})
.then(function(myStream) {
navigator.mediaDevices.getUserMedia(mediaConstraints)
.then(function(myStream) {
stream = myStream;
usercam1.srcObject = stream;
pc = new PeerConnection(servers);
pc.addStream(stream);
pc.ontrack = function(e) {
usercam2.srcObject = e.streams[0];
};
pc.onicecandidate = function(event) {
if(event.candidate) {
send({type: 'candidate', candidate: event.candidate});
}
};
})
})
.catch(function(error) {
console.log('error switching camera :' + error + ' camecounter is: ' + camcounter);
});
可能的副本必须对此进行检查并报告!