Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *