android - What is wrong with my Text-To-Speech in my CountDownTimer? -


hey all, i'm trying put text-to-speech in countdowntimer. "there x seconds left" after amount of time. started using texttospeech , i'm not sure i'm doing..

    package com.android.countdown;   import java.util.locale; import android.app.activity; import android.os.bundle; import android.os.countdowntimer; import android.view.view; import android.speech.tts.texttospeech; import android.widget.button; import android.widget.textview; import android.view.view.onclicklistener;  public class countdown extends activity implements texttospeech.oninitlistener{  countdowntimer counter1;  countdowntimer counter2;  countdowntimer counter3;  int interval = 1;  texttospeech tts;      public string formattime(long millis) {           string output = "0:00";           long seconds = millis / 1000;           long minutes = seconds / 60;            seconds = seconds % 60;           minutes = minutes % 60;             string secondsd = string.valueof(seconds);           string minutesd = string.valueof(minutes);            if (seconds < 10)             secondsd = "0" + seconds;           if (minutes < 10)             minutesd = "0" + minutes;            output = minutesd + " : " + secondsd;           return output;         }     @override      public void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          setcontentview(r.layout.main);     //declare start/stop timer  button btnstart = (button)findviewbyid(r.id.btnstart);  button btnstop = (button)findviewbyid(r.id.btnstop);  //text field show time left  final textview mcounter1textfield=(textview)findviewbyid(r.id.counter1);  final textview mcounter2textfield = (textview)findviewbyid(r.id.counter2);  final textview mcounter3textfield=(textview)findviewbyid(r.id.counter3);    //counter 1 counter1 = new countdowntimer(20000 , interval) { public void ontick(long millisuntilfinished){     mcounter1textfield.settext("seconds left: " + formattime(millisuntilfinished));     if (millisuntilfinished == 10000) {      instantiate();     }   }  public void onfinish() {     counter1.start();   } };  //counter 2 counter2 = new countdowntimer(80000 , interval) {  public void ontick(long millisuntilfinished) {      mcounter2textfield.settext("seconds left: " + formattime(millisuntilfinished));   }   public void onfinish() {      mcounter2textfield.settext("finished!");      counter2.start();  }  };  //counter 3 counter3 = new countdowntimer(3000 , interval) {   public void ontick(long millisuntilfinished) {       mcounter3textfield.settext("seconds left: " + formattime(millisuntilfinished));   }    public void onfinish() {     mcounter3textfield.settext("finished!");     counter3.start();   }   };   //start button btnstart.setonclicklistener(new onclicklistener() {  public void onclick(view v) {   counter1.start();   counter2.start();   counter3.start();    } });  //stop button btnstop.setonclicklistener(new onclicklistener() {  public void onclick(view v) {   counter1.cancel();   counter2.cancel();   counter3.cancel();   if (tts != null) {   tts.stop();         tts.shutdown();   }     } }); }     public void instantiate() {       tts = new texttospeech(this, this);       tts.setlanguage(locale.us);          tts.speak("you have 10 seconds remaining", texttospeech.queue_add, null);     }   @override  public void oninit(int status) {   }      @override     public void ondestroy() {         // don't forget shutdown!         if (tts != null) {             tts.stop();             tts.shutdown();         }          super.ondestroy();     }   } 

your second argument in tts = new texttospeech(this, this) not implement texttospeech.oninitlistener.

you need have countdown or class implement texttospeech.oninitlistener:

public class countdown extends activity implements texttospeech.oninitlistener { 

then implement oninit() in said class:

void oninit(int status){    // implementation  } 

and pass class implements oninitlistener texttospeech constructor:

// second 'this' replaced class if  // decide use class other countdown implement interface. tts = new texttospeech(this, this); 

check out texttospeechactivity.java tutorial full working example.

edit

as nickt mentioned, need add curly braces if statement in ontick:

if (millisuntilfinished == 10000) {     tts = new texttospeech(this, this);     tts.setlanguage(locale.us);     tts.speak("you have 10 seconds remaining", texttospeech.queue_add, null); } 

else you'll execute setlanguage , speak always, give nullpointerexception unless millisuntilfinished == 10000 true.


http://developer.android.com/reference/android/speech/tts/texttospeech.oninitlistener.html


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -