javascriptwebrtcgetusermediaenumerate-devices

How to do so that the browser prompts the user for the camera just once?


When using the WebRTC Javascript API, I noticed that the browser prompts the user for enabling the camera everytime the page is loaded in Firefox and Safari when calling navigator.mediaDevices.getUserMedia().

My objective is that the browser only prompts once the user, as it does in Chrome. How can I do this?

I have tried using navigator.mediaDevices.enumerateDevices(), thinking that I could use the result in getUserMedia to select the camera, but it doesn't keep the user approval after the browser is closed.


Solution

  • You can not force this, the browsers leave this decision to the user. And even Chrome is moving away from permanent permissions as explained in https://developer.chrome.com/blog/one-time-permissions?hl=en

    What you can do is detect whether permission has been given previously using the Permissions API (or using enumerateDevices) and adapt your UX to that.