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.
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.
Share Improve this question edited Feb 16, 2021 at 13:18 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Dec 2, 2019 at 0:31 Murali NepalliMurali Nepalli 1,61810 silver badges17 bronze badges 7- Hi, could it be a security issue? getUserMedia only works for secure contexts. It will not work in http unless you are in your localhost. Also try to run the code from this article when the window.onload blog.addpipe./mon-getusermedia-errors – louie kim Commented Dec 2, 2019 at 1:13
- @louiekim I see this problem over https too. It seems the navigator.permissions.query has limitations in firefox and edge. – Murali Nepalli Commented Dec 2, 2019 at 1:15
-
stackoverflow./questions/53147944 Though for your case there might be hack-arounds, e.g
(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. – Kaiido Commented Dec 2, 2019 at 2:47 - @Kaiido I included the use case. – Murali Nepalli Commented Dec 2, 2019 at 5:32
- 1 Always show the message? Or if you really think it's too cumbersome, store a cookie/pref in localStorage so it's shown only once per user. – Kaiido Commented Dec 2, 2019 at 5:40
1 Answer
Reset to default 4 +100It's not possible
The Permissions API is an experimental technology currently under development:
Mozilla believes that the ability to work with user permissions is critical for user agency. There are certain aspects of the API that are not suitable for the permissions model used in Firefox and so we would like to work on improving several aspects of the API. In particular, we think that the way that status of permissions needs to more accurately reflect the different states that exist or could exist. We also think that the interactions with Feature Policy need to be better clarified. We're mitted to fixing this, because permissions has bee critical in making the web a more capable platform and it is important to ensure that we preserve user control over their online experience
Mozilla's position about Permissions API
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745412476a4626597.html
评论列表(0条)