title: How to Pause Text to Speech on Android Java
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.  </p>
  
  <p>In the code below, I have the entire book stored in a string <code>bookText</code>.
  The TTS engine is the <code>tts</code> variable.</p>
  
  <p>This is how I load the TTS queue:</p>
  
  <pre><code>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&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
          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;
      }
  }
  </code></pre>
  
  <p>This is how I "stop" the TTS:</p>
  
  <pre><code>tts.speak("Pausing!", TextToSpeech.QUEUE_FLUSH, null);
  </code></pre>