android - Text to Speech API not calling setOnUtteranceCompletedListener -
what want:
want notified when texttospeech program has done speaking , want perform gui task when finishes speaking task.
what know:
setonutterancecompletedlistener can used call should notify me has findished speaking.it's depriciated in api level 15 still should work.
setonutteranceprogresslistener other , better way receive call backs starting finsishing , error notifications. minimum api level 15 required implement interface. changed minimum sdk version 15 , tried method didn't work me well.
here code
public class speakingnotepad extends appcompatactivity implements texttospeech.onutterancecompletedlistener{ button btnspeak; edittext ettext; texttospeech speaker; speechrecognizer voicerecognizer; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_speaking_notepad); init(); } private void init() { speaker = new texttospeech(this, new texttospeech.oninitlistener() { @override public void oninit(int status) { speaker.setlanguage(locale.english); } }); speaker.setonutterancecompletedlistener(this); btnspeak = (button) findviewbyid(r.id.btnspeak); btnspeak.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(ettext.gettext().length()>0) speaker.speak(ettext.gettext().tostring(),texttospeech.queue_flush,null); } }); ettext = (edittext) findviewbyid(r.id.ettext); } @override public void onutterancecompleted(string utteranceid) { log.d("texttospeech","completed"); toast.maketext(this,"completed",toast.length_long).show(); btnspeak.setbackgroundcolor(color.black); } }
edit : use handler
instead of runonuithread()
method.
i recommend onutteranceprogresslistener
because api has more method listen texttospeech's state changing.
however, if want use onutterancecompleted listener, edit below:
public class speakingnotepad extends appcompatactivity implements texttospeech.onutterancecompletedlistener{ private final string utter_id = "utterance"; private final int tts_done = 99; button btnspeak; edittext ettext; texttospeech speaker; speechrecognizer voicerecognizer; hashmap<string, string> ttsoptions; mainviewhandler g_hnd; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); g_hnd = new mainviewhandler(speakingnotepad.this); init(); } private void init() { speaker = new texttospeech(this, new texttospeech.oninitlistener() { @override public void oninit(int status) { ttsoptions = new hashmap<string, string>(); ttsoptions.put(texttospeech.engine.key_param_utterance_id, utter_id); speaker.setlanguage(locale.english); } }); speaker.setonutterancecompletedlistener(this); btnspeak = (button) findviewbyid(r.id.btnspeak); btnspeak.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(ettext.gettext().length()>0) // speaker.speak(ettext.gettext().tostring(),texttospeech.queue_flush,null); speaker.speak(ettext.gettext().tostring(),texttospeech.queue_flush,ttsoptions); } }); ettext = (edittext) findviewbyid(r.id.ettext); } @override public void onutterancecompleted(string utteranceid) { if (utteranceid.equals(utter_id)) { log.d("texttospeech", "completed"); /* // more common usage, use `handler` instead of `runonuithread()`. // block runonuithread(new runnable() { @override public void run() { toast.maketext(speakingnotepad.this, "completed", toast.length_long).show(); btnspeak.setbackgroundcolor(color.black); } }); */ // then, call `sendmessage()`. g_hnd.sendmessage(tts_done); } } private static class mainviewhandler extends handler { private final weakreference<speakingnotepad> mact; mainviewhandler(speakingnotepad act) { mact = new weakreference<speakingnotepad>(act); } public void handlemessage(message msg) { speakingnotepad act = mact.get(); if (act != null) { act.handlemessage(msg) } } } private void handlemessage(message msg) { switch(msg.what) { case tts_done: toast.maketext(speakingnotepad.this, "completed", toast.length_long).show(); btnspeak.setbackgroundcolor(color.black); break; } } }
Comments
Post a Comment