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

发布于 2024-10-14 18:07:50 字数 2833 浏览 2 评论 0原文

大家好,所以我尝试在我的计时器之一达到 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);

在没有错误的情况下将我的内容放在那里。但我不确定为什么演讲不起作用。

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


}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏日浅笑〃 2024-10-21 18:07:50

您需要在活动 onCreate()onResume() 方法中初始化 TTS。另请参阅如何在 Android 上等待 TextToSpeech 初始化

You need to initialize TTS within your activities onCreate() or onResume() method. See also How to wait for TextToSpeech initialization on Android

我乃一代侩神 2024-10-21 18:07:50

好吧,我根本不知道你的计时器是如何启动的。在完成之前你不能开始它!你有:

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

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

如果您正确格式化代码并去掉按钮等冗余内容,将会有很大帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文