java - Simple TextToSpeech code -
i want make simple texttospeech
code. code here:
texttospeech tts = new texttospeech(this, this); tts.setlanguage(locale.us); tts.speak("this alert application", texttospeech.queue_add,null,null);
but getting error:
error:(100, 28) error: no suitable constructor found texttospeech(mainactivity,mainactivity) constructor texttospeech.texttospeech(context,oninitlistener,string) not applicable (actual , formal argument lists differ in length) constructor texttospeech.texttospeech(context,oninitlistener) not applicable (actual argument mainactivity cannot converted oninitlistener method invocation conversion)
what missing? need put in code?
if allow of thread sleep , after sleep calling tts.speak(). if so, @ point looking @ code, tts not seem initialized , null crash exception.
this code should prevent exception, if initialization of tts engine takes long, won't loading. also, guessing 5 second (which long time btw) sleep allow initialized?
code
public class mainj extends activity implements oninitlistener { private texttospeech mytts; // status check code private int my_data_check_code = 0; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.loadscreen); intent checkttsintent = new intent(); checkttsintent .setaction(texttospeech.engine.action_check_tts_data); startactivityforresult(checkttsintent, my_data_check_code); thread logotimer = new thread() { public void run() { try { try { sleep(5000); speakwords("loading"); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } //waiting sleep splash_wait() } { finish(); } } }; logotimer.start(); } // speak user text private void speakwords(string speech) { // speak straight away if(mytts != null) { mytts.speak(speech, texttospeech.queue_flush, null); } } // act on result of tts data check protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == my_data_check_code) { if (resultcode == texttospeech.engine.check_voice_data_pass) { // user has necessary data - create tts mytts = new texttospeech(this, this); } else { // no data - install intent installttsintent = new intent(); installttsintent .setaction(texttospeech.engine.action_install_tts_data); startactivity(installttsintent); } } } // setup tts public void oninit(int initstatus) { // check successful instantiation if (initstatus == texttospeech.success) { if (mytts.islanguageavailable(locale.us) == texttospeech.lang_available) mytts.setlanguage(locale.us); } else if (initstatus == texttospeech.error) { toast.maketext(this, "sorry! text speech failed...", toast.length_long).show(); } private void splash_wait() { new handler().postdelayed(new runnable() { @override public void run() { intent = new intent(splashactivity.this, loginactivity.class); startactivity(i); finish(); } }, splash_time_out); } }
Comments
Post a Comment