How to know if webkitSpeechRecognition is started?
Asked Answered
B

2

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.

Bearberry answered 28/5, 2017 at 11:30 Comment(0)
L
16

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
}
Letsou answered 28/5, 2017 at 11:33 Comment(4)
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 :PBearberry
@Bearberry 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.Letsou
It seem's to be ok :p, cause i don't put the boolean edit in events ;)Bearberry
@Bearberry Wonderful. I am doing this myself and it works great. Hope I helped.Letsou
H
-2

You can simple check this

if(this.recognition){
    //do something if true
}else{
    // do something else if false
}
Heddy answered 26/8, 2021 at 22:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.