我的 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();
}
}
Hey all, I'm trying to put text-to-speech in my CountDownTimer. I would like it to say "There are x seconds left" after a certain amount of time. I just started using TextToSpeech and I'm not really sure what 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 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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tts = new TextToSpeech(this, this)
中的第二个参数未实现TextToSpeech.OnInitListener
。您需要让
countdown
或其他类实现TextToSpeech.OnInitListener
:然后在该类中实现 onInit():
最后将实现 OnInitListener 的类传递到
TextToSpeech
构造函数:查看 TextToSpeechActivity.java 教程,了解完整的工作示例。
编辑
正如 NickT 提到的,您还需要在 onTick 中的 if 语句中添加大括号:
否则您将执行
setLanguage
和总是说话
,除非millisUntilFinished == 10000
为 true,否则会抛出NullPointerException
异常。http://developer.android.com/reference/android/speech /tts/TextToSpeech.OnInitListener.html
Your second argument in
tts = new TextToSpeech(this, this)
does not implementTextToSpeech.OnInitListener
.You need to have
countdown
or another class implementTextToSpeech.OnInitListener
:Then implement onInit() in said class:
And finally pass the class that implements OnInitListener into the
TextToSpeech
constructor:Check out the TextToSpeechActivity.java tutorial for a full working example.
EDIT
As NickT mentioned, you also need to add curly braces to your if statement in onTick:
Else you'll execute
setLanguage
andspeak
always, which will give you aNullPointerException
unlessmillisUntilFinished == 10000
is true.http://developer.android.com/reference/android/speech/tts/TextToSpeech.OnInitListener.html
另一种不需要在您的活动中实现 TextToSpeech.OnInitListener 的方法(在我看来)意味着更清晰的代码是:
如果您想“询问”tts 数据。我这样做:
几乎与
TextToSpeech.isLanguageAvailable()
相同。我将在不久的将来改变它。Another approach that doen't need to implement
TextToSpeech.OnInitListener
in your activity, meaning a cleaner code (In my opinion), is:If you want to "ask" for tts data. I do it this way:
Is almost the same as
TextToSpeech.isLanguageAvailable()
. I'm going to change it near the future.