嘿所有,我正试图在我的CountDownTimer中添加文本转语音。我想在一段时间后说“剩下x秒”。我刚开始使用TextToSpeech而且我不确定我在做什么......
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 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 to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}
答案 0 :(得分:1)
tts = new TextToSpeech(this, this)
中的第二个参数未实现TextToSpeech.OnInitListener
。
您需要countdown
或其他类工具TextToSpeech.OnInitListener
:
public class countdown extends Activity implements TextToSpeech.OnInitListener {
然后在所述类中实现onInit():
void onInit(int status){
// implementation
}
最后将实现OnInitListener的类传递给TextToSpeech
构造函数:
// The second 'this' will be replaced with another class if you
// decide to use a class other than countdown to implement the interface.
tts = new TextToSpeech(this, this);
查看TextToSpeechActivity.java教程以获取完整的工作示例。
修改强>
如 NickT 所述,您还需要在onTick中的if语句中添加花括号:
if (millisUntilFinished == 10000) {
tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.US);
tts.speak("You have 10 seconds remaining", TextToSpeech.QUEUE_ADD, null);
}
否则,您将始终执行setLanguage
和speak
,除非NullPointerException
为真,否则会为您millisUntilFinished == 10000
。
http://developer.android.com/reference/android/speech/tts/TextToSpeech.OnInitListener.html
答案 1 :(得分:1)
另一种不需要在您的活动中实施TextToSpeech.OnInitListener
的方法,意思是更清晰的代码(在我看来),是:
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status){
if(status == TextToSpeech.SUCCESS) {
Log.d("myapp", "TextToSpeech enabled");
}
}
});
如果你想“询问”tts数据。我是这样做的:
// I have this code inside onCreate(), but you can call it elsewhere.
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, 6);
// Then, in the activity add this code:
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
if (requestCode == 6) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status){
if(status == TextToSpeech.SUCCESS) {
Log.d("myapp", "TextToSpeech prepared");
}
}
});
}
}
}
与TextToSpeech.isLanguageAvailable()
几乎相同。我将在未来改变它。