How to pause android.speech.tts.TextToSpeech?
The TTS SDK doesn't have any pause functionality that I know of. But you could use synthesizeToFile()
to create an audio file that contains the TTS output. Then, you would use a MediaPlayer object to play, pause, and stop playing the file. Depending on how long the text string is, it might take a little longer for audio to be produced because the synthesizeToFile()
function would have to complete the entire file before you could play it, but this delay should be acceptable for most applications.
Pause in TTS android
There is a way to pause. Just call TextToSpeech.playSilence()
see the code below from here.
It speaks some actual text along with some silence.
private void playScript()
{
Log.d(TAG, "started script");
// setup
// id to send back when saying the last phrase
// so the app can re-enable the "speak" button
HashMap<String, String> lastSpokenWord = new HashMap<String, String>();
lastSpokenWord.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
LAST_SPOKEN);
// add earcon
final String EARCON_NAME = "[tone]";
tts.addEarcon(EARCON_NAME, "root.gast.playground", R.raw.tone);
// add prerecorded speech
final String CLOSING = "[Thank you]";
tts.addSpeech(CLOSING, "root.gast.playground",
R.raw.enjoytestapplication);
// pass in null to most of these because we do not want a callback to
// onDone
tts.playEarcon(EARCON_NAME, TextToSpeech.QUEUE_ADD, null);
tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
tts.speak("Attention readers: Use the try button to experiment with"
+ " Text to Speech. Use the diagnostics button to see "
+ "detailed Text to Speech engine information.",
TextToSpeech.QUEUE_ADD, null);
tts.playSilence(500, TextToSpeech.QUEUE_ADD, null);
tts.speak(CLOSING, TextToSpeech.QUEUE_ADD, lastSpokenWord);
}
How to Pause Text to Speech on Android Java
Since the TextToSpeech class doesn't have pause/resume methods, I would suggest doing the following:
1) Divide the book into sentences instead of 500 character blocks. (You could parse using "." as the delimiter).
2) Introduce a "master index" counter, X, that tracks progress as: we are at sentence # X / total sentences.
3) When user clicks pause, just use the stop() method.
4) When the user clicks resume, resume speech at the beginning of the sentence that was interrupted.
This will lead to better user comprehension (of the book) than literally pausing and resuming mid-sentence, anyway.
TTS pause button, I only can stop it complete, but not to resume
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.
Text-To-Speech STOP Issue
It is hard to tell without looking at your code what you are doing wrong, but you should be able to call TextToSpeech.stop() or TextToSpeech.shutdown() in your onPause
and make that work. It is possible the stop fails for any number of reasons, and if it does then you're just out of luck. This works for me consistently on 6 different models of Android device (mTts
is my TextToSpeech
instance):
@Override
protected void onStop()
{
super.onStop();
if(mTts != null){
mTts.shutdown();
}
}
Related Topics
How to Play an Audio File on a Voice Call in Android
Android, How Do How to Get a List of All Files in a Folder
Check If a Vpn Connection Is Active in Android
Android: Radio Button in Custom List View
Draw a Semicircle in The Background of a View
Import an Eclipse Android Project with Version Control System into Android Studio
How to Generate The Aac Adts Elementary Stream with Android Mediacodec
Ble Scan Is Not Working When Screen Is Off on Android 8.1.0
Android - Setting Layoutparams Programmatically
Difference Between Getstring() and Getresources.Getstring()
Android Edittext Listener for Cursor Position Change
Creating Animation on Imageview While Changing Image Resource
Android Httpsurlconnection Eofexception
Multiple Notifications to The Same Activity
Is Google-Services.JSON Safe from Hackers
How to Deal with Deprecated Classes in Android to Keep Compatibility