Running Android TTS in a Service

拈花ヽ惹草 提交于 2019-12-20 14:21:57

问题


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

}

回答1:


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



回答2:


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

since API Level 14, android has added a default TextToSpeech Service class that does what you want.




回答3:


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

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

TTS doesn't speak from a service whereas it does it from an activity in android



来源:https://stackoverflow.com/questions/5219568/running-android-tts-in-a-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!