12

I'm using the web speech API but once a bit of time passes by (a minute or 2) without any vocal input, it stops listening entirely. I know this because I have it log its parsed text to the console, however, it stops doing this when I do not talk for a minute or two.

Is there any way to fix this?

Bryan
  • 2,870
  • 24
  • 39
  • 44
Russell C.
  • 588
  • 6
  • 25
  • The top answer serves to fix this specific question, but in the latest version of Chrome the user will have to allow recording every time. – Johnnie Regalado Dec 15 '18 at 01:55

3 Answers3

10

You can listen to the end event and then restart the recognition on the SpeechRecognition object.

You should use a boolean flag for deciding (in the onend event handler), when to restart the recognition (and when not to restart).

You could use the other recognition-related events for this.

E.g. Chrome triggers the following event handlers when recognition is started:

1. onstart
2. onaudiostart

   (only if sound / speech is detected)
3. onsoundstart
4. onspeechstart

If no sound speech is detected, only the first 2 will be triggered, and then, after some timeout, the corresponding end events (in reverse order).

russa
  • 399
  • 3
  • 7
3

A simple solution for this could probably be to listen for the end event and restart the recognition

recognition.addEventListener('end', recognition.start);
Anup
  • 31
  • 1
  • 1
    I think it is safer to have `recognition.addEventListener('end', () => recognition.start())`; in order to keep `this` context of `.start` method. – ardatan Mar 13 '21 at 22:45
1

recognition.addEventListener('end', () => recognition.start()) works but Chrome browser seems to obstrruct the continuity by generating a pop-up to Allow or Block the microphone every 5-7 seconds.

Ishaan_B
  • 9
  • 6