JCAguilera
JCAguilera

Reputation: 976

Detecting microphone permissions using DetectRTC

I'm using a javascript library, DetectRTC, to detect if the browser can use microphone and other stuff.

if(DetectRTC.isWebsiteHasMicrophonePermissions){
    //Is ok
}else{
    //Can't use microphone
}

The site has the permissions to use the microphone, but DetectRTC.isWebsiteHasMicrophonePermissions is still false. So I tried to print the object on the console and I get that isWebsiteHasMicrophonePermissions is set to true. But when I print the variable alone, it changes to false again.

console.log(DetectRTC); //isWebsiteHasMicrophonePermissions: true
console.log(DetectRTC.isWebsiteHasMicrophonePermissions) //false

Is this a bug or something? How can I fix it?

Upvotes: 4

Views: 1203

Answers (1)

Etheryte
Etheryte

Reputation: 25328

As covered in the docs, you need to use DetectRTC.load() to wait for detecting audio/video input/output devices.
See this part of the docs for further info.

// This is too early
console.log(DetectRTC.hasMicrophone);

DetectRTC.load(() => {
  // This is reliable
  console.log(DetectRTC.hasMicrophone);
});
<script src="https://cdn.rawgit.com/muaz-khan/DetectRTC/master/DetectRTC.js"></script>

Upvotes: 5

Related Questions