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

发布于 2024-10-14 05:56:58 字数 3524 浏览 2 评论 0原文

大家好,我正在尝试将文本转语音到我的倒计时器中。我想让它在一定时间后说“还剩 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 技术交流群。

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

发布评论

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

评论(2

隱形的亼 2024-10-21 05:56:58

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总是说话,除非 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 implement TextToSpeech.OnInitListener.

You need to have countdown or another class implement TextToSpeech.OnInitListener:

public class countdown extends Activity implements TextToSpeech.OnInitListener {

Then implement onInit() in said class:

void onInit(int status){
   // implementation 
}

And finally pass the class that implements OnInitListener into the TextToSpeech constructor:

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

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:

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

Else you'll execute setLanguage and speak always, which will give you a NullPointerException unless millisUntilFinished == 10000 is true.


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

沉鱼一梦 2024-10-21 05:56:58

另一种不需要在您的活动中实现 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()相同。我将在不久的将来改变它。

Another approach that doen't need to implement TextToSpeech.OnInitListener in your activity, meaning a cleaner code (In my opinion), is:

tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int status){
        if(status == TextToSpeech.SUCCESS) {
            Log.d("myapp", "TextToSpeech enabled");
        }
    }
});

If you want to "ask" for tts data. I do it this way:

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

Is almost the same as TextToSpeech.isLanguageAvailable(). I'm going to change it near the future.

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