gpt4 book ai didi

android - 我的 CountDownTimer 中的文本转语音有什么问题?

转载 作者:行者123 更新时间:2023-11-29 22:29:19 26 4
gpt4 key购买 nike

大家好,我正在尝试将文本转语音功能放入我的 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();
}


}

最佳答案

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);
}

否则您将始终执行setLanguagespeak,这会给您一个NullPointerException 除非millisUntilFinished == 10000 为真。


http://developer.android.com/reference/android/speech/tts/TextToSpeech.OnInitListener.html

关于android - 我的 CountDownTimer 中的文本转语音有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4798189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com