Currently, I have implemented text to speech (TTS) to read Books. Since TTS only allows up to 4000 chars (and a book is wayyyy more than that) I split the book up and add each part to the TTS queue. I wanna be able to click a button and pause the TTS and then later resume the TTS from where the user left off.
I have tried using synthesizeToFile and pausing the media file object but again you can only synthesize one file at a time which is less then 4000 chars. I don't want to have hundreds of media files stored on the users device just for TTS.
I can make the TTS read the book, I just can't pause without stopping it and then having to start the TTS from the beginning of the book.
In the code below, I have the entire book stored in a string bookText
.
The TTS engine is the tts
variable.
This is how I load the TTS queue:
int position = 0;
int pos = 0;
int sizeOfChar = bookText.length();
String testString = bookText.substring(position,sizeOfChar);
int next = 500;
while(true) {
String temp = "";
try {
temp = testString.substring(pos, next);
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, temp);
tts.speak(temp, TextToSpeech.QUEUE_ADD, params);
pos = pos + 500;
next = next + 500;
}
catch (Exception e) {
temp = testString.substring(pos, testString.length());
tts.speak(temp, TextToSpeech.QUEUE_ADD, null);
break;
}
}
This is how I "stop" the TTS:
tts.speak("Pausing!", TextToSpeech.QUEUE_FLUSH, null);