Is there a way to find out if the user has rejected or allowed permission to the media devices (Eg: Microphone, Camera) in Firefox?. In Chrome, I can check that with navigator.permissions.query but this fails in Firefox with a "TypeError".
navigator.permissions.query({name:"microphone"}).then(function(promise) {
if ( promise && promise.state ) {
console.log(promise.state); //"granted", "prompt" or "rejected"
}
});
//in Firefox, It throws the error "TypeError: 'name' member of PermissionDescriptor '' is not a valid value for enumeration PermissionName"
I could not catch the above error with a try catch block for some reason. So I would like to know why I can't catch this error in a try catch block and If there is an alternative approach.
Use Case
My application has speech recognition feature. I need to show a "Pre permission pop up" before user encounters the actual "System dialog" seeking access to microphone. The idea behind this "pre permission pop up" is to give a context to the user why the application needs the access. If a user has already given/rejected the access then pre permission pop up would not be needed. So I need to check the microphone's permission state and show the pop up if needed.
(await navigator.mediaDevices.enumerateDevices()).filter( ({kind}) => "audioinput" )[0].label !== ""
could tell us if a device's permission has been "granted", though it doesn't seem too reliable from my tests... Also note that depending on user's settings, UA may always ask for permissions, so maybe explaining your use case could be beneficial for the question.