gpt4 book ai didi

javascript - chrome 54 中默认使用前置摄像头的 webrtc

转载 作者:行者123 更新时间:2023-12-03 05:33:49 26 4
gpt4 key购买 nike

伙计们,我一直在尝试使用 webrtc 访问 chrome 的前置和后置摄像头,它工作得很好,我能够在前置和后置摄像头之间切换。但是,默认情况下 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:我尝试在其他代码中使用faceingMode:“environment”,但是这里有任何范围吗?

最佳答案

如何使用约束

对于后置摄像头:

{audio:false,video:{facingMode:{exact:"environment"}}}

对于前置摄像头:

{audio:false,video:{facingMode:"user"}}

关于javascript - chrome 54 中默认使用前置摄像头的 webrtc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40817341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com