javascript – Fix weird NotAllowedError microphone error

one of the users reported me a weird issue :

in the remote debugging I’ve done, first the apps runs this function:

navigator.permissions.query({name: 'microphone'})
  .then(async (permission) => {

    alert(permission.state); // it returns prompt
    const success = await check(permission.state);

  }).catch(e => {

    resolve();

  });

The permission.state in the above code returns prompt Not denied, ok?

then the check function checks the mic permissions:

function check() {
  return new Promise((resolve, reject) => {
    navigator.mediaDevices.getUserMedia({
      audio: true
    }).then(stream => {
     
      resolve();
    }).catch(err => {
      console.log(err.name) //NotAllowedError
      resolve();
    });
  });
}

Here the console.log(err.name) returns the NotAllowedError

if the navigator.permissions.query({name: 'microphone'}) returns prompt how the navigator.mediaDevices.getUserMedia returns the NotAllowedError it doesn’t make sense to me ?

What is your suggestion ? How to fix this issue?

Read more here: Source link