java - android waiting on something -
i wondering how make program pause , wait event finish before starting event, example have text speech says , right after google voice recognizer should fire, both fire @ same time , makes speech recognizer listen text speech. tried searching on here , found answers not clear me, can me it?
here example code tested out :
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_menu); tts=new texttospeech(getapplicationcontext(), new texttospeech.oninitlistener() { @suppresswarnings("deprecation") @override public void oninit(int status) { if(status != texttospeech.error){ tts.setlanguage(locale.uk); tts.speak("welcome", texttospeech.queue_flush, null); } } }); if(!(tts.isspeaking())){ startvoicerecognitionactivity(); } } private void startvoicerecognitionactivity() { intent intent = new intent(recognizerintent.action_recognize_speech); intent.putextra(recognizerintent.extra_language_model, recognizerintent.language_model_free_form); intent.putextra(recognizerintent.extra_prompt, "speak up"); startactivityforresult(intent, request_code); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == request_code && resultcode == result_ok) { matches = data.getstringarraylistextra( recognizerintent.extra_results); if (matches.contains("list")){ intent gotolist = new intent(menuactivity.this, listactivity.class ); startactivity(gotolist); } } super.onactivityresult(requestcode, resultcode, data); }
welcome wonderful world of asynchronous events.
in order want, approach not "wait" (because freeze), "listen".
when takes long in android, such text speech, have listen event.
if @ docs text speech object, you'll find accepts listener different things: http://developer.android.com/reference/android/speech/tts/texttospeech.html#setonutteranceprogresslistener(android.speech.tts.utteranceprogresslistener) , http://developer.android.com/reference/android/speech/tts/utteranceprogresslistener.html
so you'd (assuming text speech object tts
):
tts.setonutteranceprogresslistener(new utteranceprogresslistener() { public void ondone(string utteranceid) { // text has been read, next action } public void onerror(string utteranceid, int errorcode) { // error reading text code errorcode, can mean lot of things } public void onstart(string utteranceid) { // reading of sentence has started. example print on screen } });
this called "subscribing event", , asynchronous because program can other things , you'll notified when "something" (what have subscribed to) happens.
then example when do
tts.speak ("welcome", ....)
and finishes, method ondone
in listener called. can start voice recognizer there.
Comments
Post a Comment