Requesting getUserMedia only when permission is not granted

橙三吉。 提交于 2019-12-07 13:41:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!