Reputation: 2520
I have a small programm, which uses TTS in a for-loop. I created a Button that stops the TTS by tts.stop(). Is there a way to pause the TTS, if I click the button. The problem of stopping is, that I can't resume the TTS. I know there isn't a easy pause-method in TTS.
public class MainActivity extends Activity implements OnClickListener, OnInitListener {
public Button btn;
public Button next;
private TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
}
public void onClick(View v) {
tts.speak("Mach dich bereit!", TextToSpeech.QUEUE_FLUSH, null);
if (v == btn) {
for (int i = 1; i < 7; i++) {
String str = String.valueOf(i);
tts.speak(str, TextToSpeech.QUEUE_ADD, null);
tts.playSilence(3000, TextToSpeech.QUEUE_ADD, null);
}
}
@Override
public void onInit(int status) {
// Button init. and ClickListener
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this); }
//my button to stop!
public void verlaufKlick(View view) {
tts.stop();
}
Error:
03-26 23:33:43.266: E/AndroidRuntime(1720): FATAL EXCEPTION: main
03-26 23:33:43.266: E/AndroidRuntime(1720): java.lang.NullPointerException
03-26 23:33:43.266: E/AndroidRuntime(1720): at com.example.halloandroid.MainActivity.onClick(MainActivity.java:73)
03-26 23:33:43.266: E/AndroidRuntime(1720): at android.view.View.performClick(View.java:4240)
03-26 23:33:43.266: E/AndroidRuntime(1720): at android.view.View$PerformClick.run(View.java:17721)
03-26 23:33:43.266: E/AndroidRuntime(1720): at android.os.Handler.handleCallback(Handler.java:730)
03-26 23:33:43.266: E/AndroidRuntime(1720): at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 23:33:43.266: E/AndroidRuntime(1720): at android.os.Looper.loop(Looper.java:137)
03-26 23:33:43.266: E/AndroidRuntime(1720): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-26 23:33:43.266: E/AndroidRuntime(1720): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 23:33:43.266: E/AndroidRuntime(1720): at java.lang.reflect.Method.invoke(Method.java:525)
03-26 23:33:43.266: E/AndroidRuntime(1720): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-26 23:33:43.266: E/AndroidRuntime(1720): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-26 23:33:43.266: E/AndroidRuntime(1720): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 3927
Reputation: 3472
Have you considered playing a silence for the duration of a pause, like you do between utterances? Previous answer: https://stackoverflow.com/a/11079302/597849
Since playSilence() is asynchronous you don't want to just loop a bunch of times since it won't wait for the previous utterance to finish. You can use the tts progress listener to know when to start a new clip (or silence).
Another poster has also suggested writing the TTS output to a file, the playback of which can be paused.
Upvotes: 1