How to detect if microphone permissions have been granted in Chrome with JavaScript?

Sometimes, we want to detect if microphone permissions have been granted in Chrome with JavaScript.

In this article, we’ll look at how to detect if microphone permissions have been granted in Chrome with JavaScript.

How to detect if microphone permissions have been granted in Chrome with JavaScript?

To detect if microphone permissions have been granted in Chrome with JavaScript, we can use the navigation.permissions.query method.

For instance, we write:

const checkMic = async () => {
  const permissionStatus = await navigator.permissions.query({
    name: 'microphone'
  })
  console.log(permissionStatus)
}
checkMic()

to define the checkMic function that calls navigation.permissions.query with { name: 'microphone' } to return a promise with the microphone permission data.

As a result, we get something like

{name: 'audio_capture', state: 'denied', onchange: null}

logged.

Conclusion

To detect if microphone permissions have been granted in Chrome with JavaScript, we can use the navigation.permissions.query method.