12

I'm making a bot to listen to my voice.
So i did :

this.recognition = new webkitSpeechRecognition();

I can do this to start listen :

this.recognition.start();

And this to stop listen :

this.recognition.stop();

But do you know a function that will return me true if this.recognition is started and false if it's stopped ? Like "isStarted()" ?

Thanks.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
TomSkat
  • 273
  • 4
  • 11

2 Answers2

14

You can do this by raising a flag variable on the onstart and onend events:

var recognition = new webkitSpeechRecognition();
var recognizing = false;

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

recognition.onend = function () {
    recognizing = false;
};

recognition.onerror = function (event) {
    recognizing = false;
};

if (recognizing) {
    // Do stuff
}
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
  • Thanks for your answer, i already tryed it. My code is a little bit complex so sometimes it's crashing with this error : ------ > Uncaught DOMException: Failed to execute 'start' on 'SpeechRecognition': recognition has already started. I can miss a boolean update anywhere but a webkitSpeechRecognition function is a better solution :P – TomSkat May 28 '17 at 11:39
  • @TomSkat There is no boolean value currently, so you would have to implement one like I wrote. Please add the `onerror` event like I edited in my answer, and wrap your code with `catch`, that should solve the case you mentioned as well. – Koby Douek May 28 '17 at 11:41
  • It seem's to be ok :p, cause i don't put the boolean edit in events ;) – TomSkat May 28 '17 at 11:44
  • @TomSkat Wonderful. I am doing this myself and it works great. Hope I helped. – Koby Douek May 28 '17 at 11:46
-1

You can simple check this

if(this.recognition){
    //do something if true
}else{
    // do something else if false
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
MindRoasterMir
  • 324
  • 1
  • 2
  • 18