The following code sometimes fails and sometimes it works:
navigator.mediaDevices.getUserMedia(
{video : true}
)
.then((stream) => {
video.srcObject = stream;
})
.catch(err => console.error('getUserMedia() failed: ', err));
Error Message when failing: DOMException: Requested device not found
Without changing anything, half an hour later it works. I work on a MacBook Pro (13-inch, M1, 2020), which does have a webcam. I use Chrome, but tested also in Safari and the result is the same. I understand that the error means, that the constraints could not be fulfilled ---> no webcam found. But I do not understand why my webcam sometimes cannot be found and without changing anything 30 minutes later it works. Using any other tools like Teams, Skype, etc. this problem does not arise.
Update: One more thing I recognized: In case of success, my browser asks for the permission to use webcam. In case of failure, my browser does not even ask for that permission.
My Question: Why can my webcam sometimes be found and sometimes not?