Question

What is the best way to know if the user has allowed microphone access after creating an instance of webkitSpeechRecognition?

The first idea that came to my mind was using the webkitSpeechRecognition:onstart method to update a local status reference:

var permission  = false;
var recognition = new webkitSpeechRecognition();
recognition.continuous     = true;
recognition.interimResults = true;

recognition.onstart = function() { permission = true; }

But this seems redundant, since a global readonly value may be already set by the browser.

Any thoughts?

Was it helpful?

Solution

Based on the Google example, it seems that there is no browser wide property that state user's permission.

The right solution (as for now) is to listen for onstart and onend events to set the property in the scope of your speech recognition logic

var permission  = false;
var recognition = new webkitSpeechRecognition();
recognition.continuous     = true;
recognition.interimResults = true;

// Start event is fired when user accept
recognition.onstart = function() { 
  permission = true; 
}

// End event is fired when the permission expire
recognition.onend   = function() { 
  permission = false; 
}

recognition.start();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top