在服务中运行Android TTS

时间:2011-03-07 12:12:24

标签: android text-to-speech android-service

我正在尝试让Android的TTS在服务中运行,但我不知道为什么它不能正常工作,它编译,不会崩溃,但它只是不起作用。

Toast通知确实有效。

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

}

3 个答案:

答案 0 :(得分:15)

你可以这样做:它对我有用。 您必须创建一个活动来启动此服务,如下所示: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);
}
}

答案 1 :(得分:1)

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

自API Level 14以来,android已经添加了一个默认的TextToSpeech Service类,可以满足您的需求。

答案 2 :(得分:0)

好吧,提出这个问题给了我一个比谷歌更好的答案。

转到此问题的答案,并将其更改为适合您的应用程序。

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