Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Android TTS in a Service

I'm trying to get Android's TTS to run inside a service, but I have no idea why it isn't working, it compiles, doesn't crash, but it just doesn't work.

The Toast notification do work though.

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

}
like image 481
Randy Davis Avatar asked Mar 07 '11 12:03

Randy Davis


1 Answers

You can do like below: It's working for me. You have to create an activity to start this service, like this: 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);
}
}
like image 95
coastline Avatar answered Sep 28 '22 01:09

coastline