Running Android TTS in a service

I try to run Android TTS inside a service, but I have no idea why it does not work, it compiles, does not crash, but it just does not work.

Toast notification really works.

package alarm.test;

import android.app.Service;
import com.google.tts.TextToSpeechBeta;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyAlarmService extends Service {

    private TextToSpeechBeta myTts;
    private TextToSpeechBeta.OnInitListener ttsInitListener = new TextToSpeechBeta.OnInitListener() {
        public void onInit( int arg0, int arg1 ) {
            myTts.speak("", 0, null);
        }
    };

@Override
public void onCreate() {
 // TODO Auto-generated method stub
    myTts = new TextToSpeechBeta( this,
            ttsInitListener );

 Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}

@Override
public IBinder onBind(Intent intent) {
 // TODO Auto-generated method stub
    myTts.speak("something is working", TextToSpeechBeta.QUEUE_FLUSH, null);
 Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
 return null;
}

@Override
public void onDestroy() {
 // TODO Auto-generated method stub
 super.onDestroy();
 Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startId) {
 // TODO Auto-generated method stub
 super.onStart(intent, startId);
 Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
}

@Override
public boolean onUnbind(Intent intent) {
 // TODO Auto-generated method stub
 Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
 return super.onUnbind(intent);
}

}
+5
source share
3 answers

You can do as below: This works for me. You must create an action to start this service, for example: this.startService (intent)

public class TTSService extends Service implements TextToSpeech.OnInitListener{

private String str;
private TextToSpeech mTts;
private static final String TAG="TTSService";

@Override

public IBinder onBind(Intent arg0) {

    return null;
}


@Override
public void onCreate() {

      mTts = new TextToSpeech(this,
                this  // OnInitListener
                );
      mTts.setSpeechRate(0.5f);
      Log.v(TAG, "oncreate_service");
     str ="turn left please ";
    super.onCreate();
}


@Override
public void onDestroy() {
    // TODO Auto-generated method stub
     if (mTts != null) {
            mTts.stop();
            mTts.shutdown();
        }
        super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {


    sayHello(str);

    Log.v(TAG, "onstart_service");
    super.onStart(intent, startId);
}

@Override
public void onInit(int status) {
    Log.v(TAG, "oninit");
     if (status == TextToSpeech.SUCCESS) {
            int result = mTts.setLanguage(Locale.US);
            if (result == TextToSpeech.LANG_MISSING_DATA ||
                result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.v(TAG, "Language is not available.");
            } else {

                sayHello(str);

            }
        } else {
            Log.v(TAG, "Could not initialize TextToSpeech.");
        }
}
private void sayHello(String str) {
      mTts.speak(str,
                TextToSpeech.QUEUE_FLUSH, 
                null);
}
}
+15
source

https://developer.android.com/reference/android/speech/tts/TextToSpeechService.html

since API level 14, android adds the standard TextToSpeech Service class that does what you want.

+1

Well, this question gave me a better answer than I could find with Google.

Go to the answer to this question and change it according to your application.

TTS does not speak from the service, whereas it does it due to activity in android

0
source

All Articles