0

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.

Here's the code for the Service itself:

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 && x > 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 && 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);
    }
}
}

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.

EDIT: Just found out that speech rarely starts up, and only for a split second...

Here's the log if anything:

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

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.

UPDATE: I fiddled around with the code and realised that onInit was executed after onCreate() and onStartCommand, which is the reason why the speech didn't execute. Here's my question again - how do you make onInit run first or make the addQueue code run after it has initialized?

4
  • When does the code at onInit executes?
    – TDG
    Jul 17, 2015 at 17:50
  • @TDG There are no logs from onInit, yet isLoaded isn't set to true, which is strange and the reason I don't understand what's happening. I'll keep on trying and finding out what's happening... Jul 18, 2015 at 0:59
  • @TDG I just fiddled around with the code... and made some new discoveries. Apparently onInit was called after onCreate and onStartCommand, I'll rephrase my question now... Jul 18, 2015 at 1:19
  • Why don't you try to move the onInit code to onCreate?
    – TDG
    Jul 18, 2015 at 6:45

2 Answers 2

1

I kinda solved my own problem by implementing a short delay before the speech. This is only a temporary method but my final app will allow enough time for the tts to initialize, since it will not start speech up immediately.

0

You cannot "make OnInit run first" the whole point is that it's a callback for a sync process and onCreate and onStartCommand are lifecycle callbacks, so none are within your control about when or the order they execute in.

What you need to do to make your code work correctly is in onStartCommand to check is the TTS has completed initialising correctly and if it hasn't yet (most likely) then queue up your speak() calls to run later once it has - ie. when the onInit () is actually executed with a status of SUCCESS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.