Video and audio permissions in JavaScript

Viewed 336

I need to tell if a person dismisses the media popup or blocks the media popup

const [permissions, setPermissions] = useState(false)

const handleClick = () => {
        setPermissions('pending');

        navigator.getUserMedia({ video: true, audio: true }, stream => {
            stream.getTracks().forEach(track => {
                return setPermissions(track.enabled);
            });
        }, (error) => {
            if(error ==='DOMException: Permission denied'){
                setPermissions('denied')
            }
            if(error === 'DOMException: Permission dismissed'){
                setPermissions('dismissed')
            }
            });
    };

I tried using the error string that I got back but it is not working. I need to be able to tell the difference between the errors. Does anyone have a good way to do this? Thanks!

1 Answers

You can't tell the difference in any exact way. Browsers are very uncooperative with Javascript code that tries to access media devices but doesn't get permission from the user. Because cybercreeps.

I've had a bit of success with this to see if the user denied access:

Try again. If you get an immediate (within a couple of seconds) error they probably have already denied access to the devices. If it takes longer, they probably are looking at a repeat of the permission dialog.

Not a great situation. Especially if you want to explain to the user how to go back and un-deny permission. But it's a necessary privacy feature in the surveillance era.

Related