从服务开始文本到语音引擎?

时间:2010-06-23 08:58:23

标签: android

我有一个服务,我试图启动TextToSpeech引擎,但似乎它不起作用,所以是否可以从服务启动tts?

这是我尝试过的:

package com.example.TextSpeaker;

import java.util.Locale;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
 import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.widget.Toast;


 public class SpeakerService extends Service implements OnInitListener{

   public static TextToSpeech mtts;
  @Override
   public IBinder onBind(Intent arg0) {
 // TODO Auto-generated method stub
  return null;
  }

@Override
 public void onCreate(){
Log.d("SpeakerService","Service created successfully!");
mtts = new TextToSpeech(this,this);
mtts.setLanguage(Locale.ENGLISH);


}
@Override
public void onStart(Intent intent,int startid)
  {
Log.d("SpeakerService","Service started successfully!");
 Log.d("SpeakerService","Service started successfully!");
  Log.d("SpeakerService","tspker.mtts = " + TextSpeaker.mtts.toString());
 mtts = new TextToSpeech(this,this);
   mtts.setLanguage(Locale.ENGLISH);
   mtts.speak(Receiver.str, TextToSpeech.QUEUE_FLUSH,null);
}
 @Override
 public void onDestroy(){
 if(mtts!=null)
  {
     mtts.stop();
  Toast.makeText(getApplicationContext(),"The service has been destroyed!", T  oast.LENGTH_SHORT).show();
}

}

 @Override
  public void onInit(int arg0) {
   // TODO Auto-generated method stub

 }

}

1 个答案:

答案 0 :(得分:4)

new TextToSpeech(getApplicationContext(),this);

和我一起工作...... 但是一定要让服务运行,直到发言完毕。