问题
I'm creating an alert to give the user an additional notification when my site requests access to their microphone. I've read that for Chrome, if the site is served over HTTPS, the user isn't asked for permission again unless they delete the permission. However, I'm noticing that on my non-HTTPS site requesting microphone access, that the permission is still saved. It appears that regardless of permission, calling getUserMedia always makes the Chrome alert appear.
Is there a way for me to determine that the user has already given my site permission? I would only want the alert to appear if the user hasn't given permission or has deleted the permissions.
window.navigator = window.navigator || {};
navigator.getUserMedia = (navigator.getUserMedia
|| navigator.webkitGetUserMedia
|| navigator.mozGetUserMedia
|| navigator.msGetUserMedia
|| null);
if (navigator.getUserMedia /* && "Site not allowed" */) {
// Show alert.
navigator.getUserMedia(
{
audio: true
},
function(stream) {
// Hide alert.
},
function(error) {
switch (error.name) {
case 'PERMISSION_DENIED':
// Permission denied
break;
case 'NOT_SUPPORTED_ERROR':
// Media not supported
break;
case MANDATORY_UNSATISFIED_ERROR:
// No media tracks of the type specified in the constraints are found.
break;
}
}
);
} else {
console.log('Browser doesn't support getUserMedia.');
}
来源:https://stackoverflow.com/questions/23432654/requesting-getusermedia-only-when-permission-is-not-granted