一次這是我的TTS服務代碼:廣播接收器的呼叫服務僅在循環
public class SMSTTSService extends Service implements TextToSpeech.OnInitListener, OnUtteranceCompletedListener {
private TextToSpeech tts;
private String spokenText;
@Override
public void onCreate() {
tts = new TextToSpeech(this, this);
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.ENGLISH);
// tts.setPitch(5); // set pitch level
// tts.setSpeechRate(2); // set speech speed rate
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language is not supported");
} else {
speakOut(spokenText);
}
/*if (result != TextToSpeech.LANG_MISSING_DATA && result != TextToSpeech.LANG_NOT_SUPPORTED) {
tts.speak(c, TextToSpeech.QUEUE_FLUSH, null);
}*/
} else {
Log.e("TTS", "Initilization Failed");
}
}
private void speakOut(String text) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onUtteranceCompleted(String uttId) {
stopSelf();
}
@Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
spokenText = intent.getStringExtra("spoken_txt");
}
}
和接收我叫服務像
public class Receiver extends BroadcastReceiver {
private static final String TAG = "SMSReceiver";
@Override
public void onReceive(Context context, Intent intent) {
for(int i=0; i<msgs.length; i++) {
....
Intent intentTTS = new Intent(context, SMSTTSService.class);
intentTTS.putExtra("spoken_txt", msgBody);
context.startService(intentTTS);
}
但服務只執行一次即使我有循環。如何讓我有更多的時間內循環語音?我做錯了什麼?
有沒有其他的選擇,然後與服務? – senzacionale 2012-08-14 11:04:24
IntentService是一種服務,使用context.startService(intent)以同樣的方式啓動。你只需要對你的SMSTTSService類進行一些修改就可以擴展IntentService,我想這不是什麼大問題。 – 2012-08-14 11:17:48
感謝您提供更新的代碼,但ExampleIntentService類永遠不會執行 – senzacionale 2012-08-14 20:01:57