文本到语音在CountDownTimer中不起作用

时间:2011-01-26 19:51:38

标签: android eclipse text-to-speech

嘿所有,所以当我的一个计时器在10秒钟时,我正试图运行文本转语音。但是,当计时器达到10秒时,它没有说什么。

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


}

我的onTick()嵌套在倒数计时器的对象实例中。所以我不能把我的

tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.US);
tts.speak("You have 10 seconds remaining", TextToSpeech.QUEUE_ADD, null);

在那里没有收到错误..但我不确定为什么演讲不起作用。

2 个答案:

答案 0 :(得分:1)

您需要在活动onCreate()onResume()方法中初始化TTS。另请参阅How to wait for TextToSpeech initialization on Android

答案 1 :(得分:0)

好吧,我看不出你的计时器是如何开始的。你完成之前不要启动它!你有:

public void onFinish() {
    Counter1.start();  
}

.start();应该在CountDownTimer实例化的右括号之后

如果您正确格式化代码并摆脱了诸如按钮

之类的冗余内容,那将会有很大帮助