Questions: webrtc using the front camera by default in Google chrome 54
Developers! i have been testing to get access to both front and back camera for google chrome using webrtc and its absolutely working fine , We are able to change between both front and back cameras. Even so, by default google chrome picks up the front camera, is there any way i can make chrome pick up the back camera when the browser opens up?
Also Read
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);
We tried accessing the array in Desc order to get the rear camera first, but could not help
for (var i = deviceInfos.length - 1; i >= 0; i--) {
var deviceInfo = deviceInfos[i];
var option = document.createElement('option');
option.value = deviceInfo.deviceId;
....... //code follows
Update: We also tried inserting these lines into the above given code but again the like response, open up the front camera by default
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: We tried using facingMode: “environment” in the another code, but is there any extent for that here ?
Total Answers 2
Answers 1 :How to use webrtc front camera by default in Google chrome
How about using constraints ?
For the BACK camera:
{audio:false,video:{facingMode:{exact:"environment"}}}
For the FRONT camera:
{audio:false,video:{facingMode:"user"}}
Answers 2 :How to use webrtc front camera by default in Google chrome
Check for the facing in device Info label and set the default value.
if(deviceInfo.label.indexOf('back') > 0) { option.selected = true; }
0 Comments