android - SpeechRecognizer throws onError on the first listening -


in android 5 faced strange problem. first call startlistening of speechrecognizer results onerror error code 7 (error_no_match).

i made test app following code:

if (speechrecognizer == null) {      speechrecognizer = speechrecognizer.createspeechrecognizer(this);     speechrecognizer.setrecognitionlistener(new recognitionlistener() {         @override         public void onreadyforspeech(bundle bundle) {             log.d(tag, "onreadyforspeech");         }          @override         public void onbeginningofspeech() {             log.d(tag, "onbeginningofspeech");         }          @override         public void onrmschanged(float v) {             log.d(tag, "onrmschanged");         }          @override         public void onbufferreceived(byte[] bytes) {             log.d(tag, "onbufferreceived");         }          @override         public void onendofspeech() {             log.d(tag, "onendofspeech");         }          @override         public void onerror(int i) {             log.d(tag, "onerror " + i);         }          @override         public void onresults(bundle bundle) {             log.d(tag, "onresults");         }          @override         public void onpartialresults(bundle bundle) {             log.d(tag, "onpartialresults");         }          @override         public void onevent(int i, bundle bundle) {             log.d(tag, "onevent");         }     }); }  final intent sttintent = new intent(recognizerintent.action_recognize_speech); sttintent.putextra(recognizerintent.extra_language_model,         recognizerintent.language_model_free_form); sttintent.putextra(recognizerintent.extra_language, "en"); sttintent.putextra(recognizerintent.extra_language_preference, "en");  speechrecognizer.startlistening(sttintent); 

and have log messages after first startlistening call:

onerror 7 onreadyforspeech onbeginningofspeech onendofspeech onresults 

and following messages after startlistening calls:

onrmschanged ... onrmschanged onreadyforspeech onrmschanged ... onrmschanged onbeginningofspeech onrmschanged ... onrmschanged onendofspeech onrmschanged onrmschanged onrmschanged onresults 

so, reason of error , how fix it?

i had same problem couldn't find workaround, ended calling return inside onerror if time between startlistening , onerror unreasonably short.

protected long mspeechrecognizerstartlisteningtime = 0;  protected synchronized void speechrecognizerstartlistening(intent intent) {     if (mspeechrecognizer != null) {         this.mspeechrecognizerstartlisteningtime = system.currenttimemillis();         rlog.d(this, "speechrecognizerstartlistening");         this.mspeechrecognizer.startlistening(intent);     } } ... @override public synchronized void onerror(int error) {     rlog.i(this, this.hashcode() + " - onerror:" + error);  // sometime onerror called after onresults keep boolean ignore error     if (msuccess) {         rlog.w(this, "already success, ignoring error");         return;     }      long duration = system.currenttimemillis() - mspeechrecognizerstartlisteningtime;     if (duration < 500 && error == speechrecognizer.error_no_match) {         rlog.w(this, "doesn't seem system tried listen @ all. duration = " + duration + "ms. might bug onerror , startlistening methods of speechrecognizer");         rlog.w(this, "going ignore error");         return;     }  // -- actual error handing code goes here. } 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -