title: Android text to speech delay
body: <p>I have come across the common issue of the android TTS initialization delay which is significantly high. I am building an application for a smartwatch with Android Nougat where I require 2 TTS services, one to speak a text in one language and another to speak a text in a second language. It turns out that switching between languages within the same instance of the <code>TextToSpeech</code> object introduces the initial delay every time I change the <code>Voice</code> object to change the language even though the <code>onInit</code> method of the <code>OnInitListener</code> had already been called. </p>
  
  <p>I tried to fix the issue by creating 2 instances of the <code>TextToSpeech</code> class with different languages and calling their corresponding <code>speak()</code> methods when needed but the delay is still present. Is there any workaround to the problem for my application?</p>
  
  <p>CODE:</p>
  
  <pre><code>public class TTSSpeaker implements TextToSpeech.OnInitListener{
  
  //TTS object
  private  TextToSpeech tts;
  
  //Locale
  private Locale locale;
  
  //Language code
  private String code;
  
  //Initialized flag
  private  boolean initialized;
  
  //ID
  private String utteranceId;
  
  //Constructor
  public TTSSpeaker(Context context, String languageCode, String id){
      code = languageCode;
      initialized = false;
      utteranceId = id;
      tts = new TextToSpeech(context, this);
  }
  
  @Override
  public void onInit(int status) {
      if(status == TextToSpeech.SUCCESS){
          initialized = true;
          Locale closest = Locale.forLanguageTag(code);
          tts.setLanguage(closest);
          Set&lt;Voice&gt; voices = tts.getVoices();
          int lowestLatency = Voice.LATENCY_VERY_HIGH;
          Voice selected = tts.getDefaultVoice();
          for(Voice voice : voices){
              if(voice.getLatency() &lt;= lowestLatency
                      &amp;&amp; voice.getLocale().getLanguage().equals(code)
                      &amp;&amp; !voice.isNetworkConnectionRequired()){
                  lowestLatency = voice.getLatency();
                  selected = voice;
              }
          }
          tts.setVoice(selected);
      }
  }
  
  public void speakText(String text){
      if(initialized) {
          tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
  
      }
  }
  
  public boolean isInitialized(){return initialized;}
  
  public void finishTTS(){
      if(tts != null){
          tts.stop();
          tts.shutdown();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>How I use this class:</p>
  
  <pre><code>private void speakRecording(){
      if (ttsSpeakerOrigin.isInitialized()){
  
          ttsSpeakerOrigin.speakText(recordedText);
      }
      else
          Log.d("Debug", "Not Initialized");
  }
  
  private void speakTranslation(){
      if (ttsSpeakerTranslation.isInitialized()){
  
          ttsSpeakerTranslation.speakText(translation);
      }
      else
          Log.d("Debug", "Not Initialized");
  }
  </code></pre>
  
  <p>The <code>ttsSpeakerTranslation</code> and <code>ttsSpeakerOrigin</code> are instances of TTSSpeaker and they correspond to TTS services with different languages. The problem is that even though they are initialized, make one speak after the other introduces the same initialization delay that google has with this service</p>