text to speech - Deal with multiple shot on TTS service -
i trying optimize code on text speech service. in code, possible multiple shots start tts service @ same time.
i hope service can catch texts these shots tts queue 1 one. please check code follows:
public class voiceservice extends service implements texttospeech.oninitlistener, texttospeech.onutterancecompletedlistener { private final string tag = "voiceservice"; private texttospeech mtts; private string messagetitle; private string messagecontext; @override public void oncreate() { log.i(tag, "oncreate"); try{ log.i(tag, "new texttospeech(this, this);"); mtts = new texttospeech(this, this); mtts.setonutterancecompletedlistener(this); } catch(exception e){ log.i(tag, "exception e : " + e.tostring()); e.printstacktrace(); } } @override public int onstartcommand(intent intent, int flags, int startid){ log.i(tag, "s of onstartcommand @ voiceservice"); messagetitle = ""; messagecontext = ""; // bug, intent totally null if(intent == null) { stopself(); } // action in intent , ask stop service. else if(intent.getaction() != null && intent.getaction().equals("xxx.stop_tts")){ stopself(); } // should start tts else { log.i(tag, "run tts"); if (intent.getstringextra("messagetitle") != null) messagetitle = intent.getstringextra("messagetitle"); if (intent.getstringextra("messagecontext") != null) messagecontext = intent.getstringextra("messagecontext"); } // return super.onstartcommand(intent, flags, startid); return start_sticky; } @override public void oninit(int status) { log.i(tag, "oninit"); if (status == texttospeech.success) { int result = mtts.setlanguage(locale.getdefault()); if (result != texttospeech.lang_missing_data && result != texttospeech.lang_not_supported && messagetitle.length() > 0 ) { log.i(tag, "add speech..."); hashmap<string,string> stringstringhashmap = new hashmap<string, string>(); stringstringhashmap.put(texttospeech.engine.key_param_utterance_id, "utterance_id"); mtts.speak(messagetitle, texttospeech.queue_add, null); mtts.speak(messagecontext, texttospeech.queue_add, stringstringhashmap); } } } @override public void onutterancecompleted(string uttid) { log.i(tag, "onutterancecompleted"); stopself(); } @override public void ondestroy() { // text speech engine attached if (mtts != null) { mtts.stop(); mtts.shutdown(); mtts = null; } super.ondestroy(); } @override public ibinder onbind(intent arg0) { return null; }
}
and result log 2 shots @ same time:
i/voiceservice: oncreate i/voiceservice: new texttospeech(this, this); i/texttospeech: sucessfully bound com.google.android.tts i/voiceservice: s of onstartcommand @ voiceservice i/voiceservice: run tts i/voiceservice: s of onstartcommand @ voiceservice i/voiceservice: run tts i/texttospeech: connected componentinfo{com.google.android.tts/com.google.android.tts.service.googlettsservice} i/texttospeech: set connection componentinfo{com.google.android.tts/com.google.android.tts.service.googlettsservice} i/voiceservice: oninit i/voiceservice: add speech...
as can see, runs "add speech" once went "onstartcommand" twice. means ignored first shot.
i did work around, put add text code in "onstartcommand" , try ignore check (status == texttospeech.success) failed.
the log follows:
i/voiceservice: s of onstartcommand @ voiceservice i/voiceservice: run tts w/texttospeech: setlanguage failed: not bound tts engine i/texttospeech: connected componentinfo{com.google.android.tts/com.google.android.tts.service.googlettsservice} i/voiceservice: s of onstartcommand @ voiceservice i/voiceservice: run tts w/texttospeech: setlanguage failed: tts engine connection not set i/texttospeech: set connection componentinfo{com.google.android.tts/com.google.android.tts.service.googlettsservice} i/voiceservice: oninit
if queue not starts in oninit(), "setlanguage failed" happens. wonder if have idea deal it. return value in onstartcommand() makes difference? i.e. start_sticky, start_flag_redelivery ?
Comments
Post a Comment