伙计们,我一直试图使用webrtc进入镀铬前后摄像头,并且完美地工作,我可以在前后摄像头之间切换。但是,默认情况下,Chrome会选择前置摄像头,有什么方法可以让Chrome在浏览器打开时拿起后置摄像头?
var videoSelect = document.querySelector('select#videoSource');
var selectors = [videoSelect];
function gotDevices(deviceInfos) {
// Handles being called several times to update labels. Preserve values.
var values = selectors.map(function(select) {
return select.value;
});
selectors.forEach(function(select) {
while (select.firstChild) {
select.removeChild(select.firstChild);
}
});
for (var i = 0; i !== deviceInfos.length; ++i) {
var deviceInfo = deviceInfos[i];
var option = document.createElement('option');
option.value = deviceInfo.deviceId;
if (deviceInfo.kind === 'videoinput') {
option.text = deviceInfo.label || 'camera ' + (videoSelect.length + 1);
videoSelect.appendChild(option);
} else {
console.log('Some other kind of source/device: ', deviceInfo);
}
}
selectors.forEach(function(select, selectorIndex) {
if (Array.prototype.slice.call(select.childNodes).some(function(n) {
return n.value === values[selectorIndex];
})) {
select.value = values[selectorIndex];
}
});
}
navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);
我尝试按降序访问数组以获得后置摄像头,但没有帮助
for (var i = deviceInfos.length - 1; i >= 0; i--) {
var deviceInfo = deviceInfos[i];
var option = document.createElement('option');
option.value = deviceInfo.deviceId;
....... //code follows
更新:我也尝试将这些行添加到上面的代码中但是同样的响应,默认情况下会打开前置凸轮
for (var i = 0; i !== deviceInfos.length; ++i) {
var deviceInfo = deviceInfos[i];
if (deviceInfo.kind === 'videoinput' && deviceInfo.facing == "environment") {
videoSourceId = deviceInfo.id;
} else {
console.log('Some other kind of source/device: ', deviceInfo);
}
}
//other code
var constraints = {
video: { optional: [{sourceId: videoSourceId}] }
};
navigator.mediaDevices.getUserMedia(constraints).
then(gotStream).then(gotDevices).catch(handleError);
}
PS:我尝试在其他代码中使用了facesMode:“environment”,但这里是否有任何范围?
答案 0 :(得分:1)
如何使用 约束 ,如:
对于后置摄像头:
{audio:false,video:{facingMode:{exact:"environment"}}}
对于FRONT相机:
{audio:false,video:{facingMode:"user"}}