How to know if webkitSpeechRecognition is started?

天涯浪子 提交于 2019-12-08 16:05:19

问题


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.


回答1:


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
}


来源:https://stackoverflow.com/questions/44226827/how-to-know-if-webkitspeechrecognition-is-started

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!