Hey all, so I'm trying to run Text-to-speech once one of my timers is at 10 seconds. However, it isn't saying anything when the timer reaches 10 seconds.
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 to show time left
final TextView mCounter1TextField=(TextView)findViewById(R.id.counter1);
final TextView mCounter2TextField = (TextView)findViewById(R.id.counter2);
final TextView mCou开发者_JS百科nter3TextField=(TextView)findViewById(R.id.counter3);
//Counter 1
Counter1 = new CountDownTimer(20000 , Interval) {
public void onTick(long millisUntilFinished){
mCounter1TextField.setText("Seconds remaining: " + formatTime(millisUntilFinished));
if (millisUntilFinished == 10000) {
countdown1speech();
}
}
public void onFinish() {
Counter1.start();
}
};
}
public void countdown1speech() {
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 to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}
My onTick() is nested inside an object instantiation for my countdown timer. So I'm not able to put my
tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.US);
tts.speak("You have 10 seconds remaining", TextToSpeech.QUEUE_ADD, null);
in there without getting an error..But I'm not sure why the speech isn't working.
You need to initialize TTS within your activities onCreate()
or onResume()
method. See also How to wait for TextToSpeech initialization on Android
Well, I can't see how your timer even starts in the first place. You don't start it until you've finished! You have:
public void onFinish() {
Counter1.start();
}
The .start(); should be after the closing brace of the instantiation of the CountDownTimer
It would help a lot if you formatted your code properly and got rid of the redundant stuff like the buttons
精彩评论