camera-viewer
camera-viewer copied to clipboard
Doesn't wait for prompt
Hey there.. love this little bit of code, super handy. One thing I noticed is that the code doesn't really wait for the getUserMedia()
call to complete before calling enumerateDevices()
.. so my phone wasn't enabling both cameras because I hadn't accepted the use prior to the looping over the enumeration.
I would have created a patch but was messing around so much that the code looks kinda horrible.. instead here's the relevant parts:
function successCallback(stream) {
currentStream = stream;
video.srcObject = stream;
video.play();
if (location.href.includes('&debug')) {
console.log(`stream: ${stream}`);
}
// PATCH: return the enumerate here as a promise..
return navigator.mediaDevices.enumerateDevices();
}
function errorCallback(error) {
window.alert('Error: ', error);
}
navigator.mediaDevices.getUserMedia({audio: false, video: true})
.then(successCallback)
// PATCH: so we can then after the prompt, enumerate the devices
.then(media_devices => {
media_devices.forEach(media_device => {
if (location.href.includes('&debug')) {
console.log(media_device);
}
if (media_device.kind === 'videoinput') {
cameras = cameras.concat(media_device.deviceId);
}
})
})
.catch(errorCallback);
Obviously you can close this 'issue' but just in case anyone else uses your great sample, they know why the rear camera doesn't seem to work.
Hey @matthewdfleming I've added your patch into my fork repo, it works great now! (https://trevcan.github.io/camera-viewer) Thanks