Speech Recognition - Run continuously

samanime picture samanime · May 2, 2015 · Viewed 8.7k times · Source

I'm trying to create an HTML5-powered voice-controlled editor using the Speech Recognition API. Currently, the problem is when you start recording, it only lasts for a certain amount of time (basically until the user stops talking).

I can set continuous and interimResults to true, but that doesn't keep it recording forever. It still ends.

I can also tell it to start again during the end event, but then it asks for permission every time, which is highly disruptive.

Is there a way to allow it to go continuously while only having to ask a user once?

Answer

Tal Ater picture Tal Ater · May 2, 2015

No matter the settings you'll choose, Google Chrome stops the speech recognition engine after a while... there's no way around it.

The only reliable solution I've found for continuous speech recognition, is to start it again by binding to the onend() event, as you've suggested.

If you try a similar technique, be aware of the following:

  1. If you are not on HTTPS, the user will be prompted to give permission over and over again on each restart. For this, and many other reasons, don't compromise on HTTP when using Speech Recognition.

  2. Make sure you are not restarting the speech recognition immediately onend() without some safeguards to make sure you aren't putting the browser into an endless loop (e.g. two open tabs with onend(function() {restart()}) can crash the browser, as I've detailed in this bug report: https://code.google.com/p/chromium/issues/detail?id=296690) See https://github.com/TalAter/annyang/blob/1ee294e2b6cb9953adb9dcccf4d3fcc7eca24c2c/src/annyang.js#L214 for how I handle this.

  3. Don't autorestart if the reason for it ending is something like service-not-allowed or not-allowed See https://github.com/TalAter/annyang/blob/1ee294e2b6cb9953adb9dcccf4d3fcc7eca24c2c/src/annyang.js#L196

You can see how I handled this in my code - https://github.com/TalAter/annyang/blob/master/src/annyang.js