3

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?

rayfranco
  • 3,630
  • 3
  • 26
  • 38
  • Is there a reason you check the permission before creating? – emesday May 02 '14 at 02:32
  • Not before creating, but before the user accepts, yes, mainly for display concerns. The interface will be bound to this value. I've noticed that the permission can expire without ssl when the user idle. I guess for now the best is using `onstart` and `onstop` events. I'd prefer to have this as a native global since for now it forces me to emit this value out of the scope that deal with the recognition. – rayfranco May 02 '14 at 10:42
  • Have you seen this? : https://www.google.com/intl/en/chrome/demos/speech.html – emesday May 02 '14 at 11:25
  • This handles several situations. Is the solution of your question not in this? – emesday May 02 '14 at 11:28
  • Yeah, they are using the same technique I exposed in my example. So I guess this is the way to go... :) – rayfranco May 02 '14 at 12:38

1 Answers1

1

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();
rayfranco
  • 3,630
  • 3
  • 26
  • 38