title: Android - Text to Speech in android service not working
body: <p>I'm developing an app in android that uses Text to Speech in a Service, and apparently it isn't working. I've checked around Google and other answers but I still don't exactly understand the problem I am having. I have done the standard procedures, but I'm still relatively new to android so there may be some things I've missed out.</p>
  
  <p>Here's the code for the Service itself:</p>
  
  <pre><code>package com.wordpress.chsiction.timesound;
  
  import android.app.Service;
  import android.content.Intent;
  import android.os.IBinder;
  import android.speech.tts.TextToSpeech;
  import android.util.Log;
  import android.widget.Toast;
  
  import java.util.Locale;
  
  
  public class TimeSoundService extends Service implements TextToSpeech.OnInitListener {
  
  private TextToSpeech tts;
  private boolean isLoaded;
  
  @Override
  public void onCreate() {
      super.onCreate();
      tts = new TextToSpeech(getApplicationContext(), this);
      Log.d("TimeSoundService", "onCreate() ended");
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, R.string.toast_startService, Toast.LENGTH_SHORT).show();
  
      Log.d("TimeSoundService", "Service started");
  
          String s = "String tts";
  
      Log.d("TimeSoundService", "String s = " + s);
  
      int x = 5;
  
      while (isLoaded &amp;&amp; x &gt; 0) {
          speak("test");
          Log.d("TimeSoundService", "Queue added");
          x--;
      }
  
      return Service.START_NOT_STICKY;
  }
  
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  @Override
  public void onDestroy () {
      super.onDestroy();
      if (tts != null) {
          tts.stop();
          tts.shutdown();
      }
      Log.d("TimeSoundService", "Service stopped");
      Toast.makeText(this, R.string.toast_stopService, Toast.LENGTH_SHORT).show();
  }
  
  @Override
  public void onInit(int status) {
      if (status == TextToSpeech.SUCCESS) {
          int result = tts.setLanguage(Locale.US);
          if (result != TextToSpeech.LANG_MISSING_DATA &amp;&amp; result != TextToSpeech.LANG_NOT_SUPPORTED) {
              isLoaded = true;
          } else {
              Log.d("TimeSoundService", "Language has missing data or is not supported");
              Toast.makeText(this, "Missing data", Toast.LENGTH_SHORT).show();
          }
      } else {
          Log.d("TimeSoundService", "Status unsuccessful");
          Toast.makeText(this, "Status unsuccessful", Toast.LENGTH_SHORT).show();
      }
  }
  
  private void speak(String s) {
      if (tts != null) {
          tts.speak(s, TextToSpeech.QUEUE_FLUSH, null);
      }
  }
  }
  </code></pre>
  
  <p>The problem is this - I don't think tts has started up. isLoaded is still false after running it, and there are no error logs from the OnInit. OnCreate ran, meaning that tts should've started up too, but nothing's happening. Also I noticed that the logs in onStartCommand did not appear sometimes. </p>
  
  <p><strong>EDIT:</strong> Just found out that speech rarely starts up, and only for a split second...</p>
  
  <p>Here's the log if anything:</p>
  
  <pre><code>07-17 16:55:15.430    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
  07-17 16:55:30.190    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
  07-17 16:55:34.950    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
  07-17 16:55:48.750    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
  07-17 16:55:49.660    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
  07-17 16:55:49.670    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service started
  07-17 16:55:49.670    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ android.speech.tts.TextToSpeech@3ba062d9
  07-17 16:55:49.670    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ String s = Speech is not delivered
  07-17 17:16:57.090    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service started
  07-17 17:16:57.090    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ android.speech.tts.TextToSpeech@3ba062d9
  07-17 17:16:57.090    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ String s = Speech is not delivered
  07-17 17:16:57.110    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Queue added
  07-17 17:16:59.470    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service started
  07-17 17:16:59.470    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ android.speech.tts.TextToSpeech@3ba062d9
  07-17 17:16:59.470    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ String s = Speech is not delivered
  07-17 17:16:59.490    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Queue added
  07-17 17:17:01.530    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
  07-17 17:17:02.930    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
  07-17 17:17:16.860    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
  07-17 17:17:23.030    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
  07-17 17:17:28.220    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
  07-17 17:17:34.120    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
  07-17 17:17:37.420    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
  07-17 17:17:37.430    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service started
  07-17 17:17:37.430    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ android.speech.tts.TextToSpeech@39d9f6bd
  07-17 17:17:37.430    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ String s = Speech is not delivered
  07-17 17:17:42.180    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
  07-17 17:17:57.110    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ onCreate() ended
  07-17 17:18:04.080    3640-3640/com.wordpress.chsiction.timesound D/TimeSoundService﹕ Service stopped
  </code></pre>
  
  <p>MainActivity have buttons for starting up and stopping the service, and that's all it does. This problem is quite annoying, I've implemented the service and things like that, but maybe the tts service is immediately shut down (?). Hopefully you kind sirs can help me out, thanks in advance.</p>
  
  <p><strong>UPDATE:</strong> I fiddled around with the code and realised that <code>onInit</code> was executed after <code>onCreate()</code> and <code>onStartCommand</code>, which is the reason why the speech didn't execute. Here's my question again - how do you make <code>onInit</code> run first or make the <code>addQueue</code> code run after it has initialized?</p>