How to wait for result of speech recognition in android? -
i using recognition listener speech recognition in android. basic construct of class follows:
class speechinput implements recognitionlistener { intent intent; speechrecognizer sr; boolean stop; public void init() { sr = speechrecognizer.createspeechrecognizer(context); sr.setrecognitionlistener(this); intent = new intent(recognizerintent.action_recognize_speech); intent.putextra(recognizerintent.extra_language_model,recognizerintent.language_model_free_form); intent.putextra(recognizerintent.extra_calling_package,context.getpackagename()); intent.putextra(recognizerintent.extra_max_results,3); } ... }
i stuck in situation want run android recognition listener in loop, on lines of:
for(int i=0; i<n; i++) { // processing code sr.startlistening(intent); }
now, want wait output before starts listening again. in order achieve this, tried use locking mechanism follows:
for(int i=0; i<n; i++) { // processing code sr.startlistening(intent); stop = false; new task().execute().get(); }
where task asynctask defined follows:
private class task extends asynctask<void,void,void> { @override protected void doinbackground(void... params) { try { int counter = 0; while(!stop) { counter++; } } catch(exception e) { } return null; } }
the boolean value 'stop' updated in 'onresults' method of recognitionlistener follows:
public void onresults(bundle results) { ... stop = true; }
the problem speech recognition not working @ all. nothing happening, not starting. guess because asynctask taking processor time. can please guide me towards architecture able achieve this? thank you.
restart listening when receive result. no loop needed, no waiting needed.
i'm not expert on speech recognition on android blocking main thread call
new task().execute().get();
so speech recognition never started.
Comments
Post a Comment