tracking.js
tracking.js copied to clipboard
Android Pie unable to use front camera
recommended add camera settings,because mobile devices use Face (camera) need front camera.and the current code does not support Android Pie(9) using the front camera.because Android P use front camera requires getUserMedia
to force use facingMode: { exact: "user" }
or use deviceId
to select,this is my solution for use deviceId in Android Pie(9)
tracking.initUserMedia_ = function(element, opt_options) {
window.navigator.mediaDevices.enumerateDevices().then(function(devices) {
let cfg={
video:{'facingMode':'user'},
audio: (opt_options && opt_options.audio) ? true : false,
};
devices = devices.filter(function(device) { return device.kind === 'videoinput'});
if (navigator.userAgent.toLowerCase().indexOf("android") > 0) {
for (let i = 0; i < devices.length; i++) {
let device = devices[i];
if (device.label) {
if (device.label.split(',')[1]==' facing front'){
cfg={
video:{
deviceId: {'exact':device.deviceId},
},
audio: (opt_options && opt_options.audio) ? true : false ,
};
break;
}
}
}
}
window.navigator.mediaDevices.getUserMedia(cfg).then(function(stream) {
element.srcObject = stream;
}).catch(function(err) {
throw Error('Cannot capture user camera.');
});
});
};