所以我有一个可以说话的服务。我还有一个 Activity 可以启用/禁用服务说话。
问题是,当我在销毁服务后启动服务时,TTS 与引擎断开连接,我该如何重新连接它?
@Override
public void onStart(Intent intent, int startid) {
if (tts == null) {
tts = new TextToSpeech(this, this);
}
}
和 onInit
@Override
public void onInit(int status) {
this.status = status;
if (this.status == TextToSpeech.SUCCESS) {
if (tts == null) {
tts = new TextToSpeech(this, this);
tts.setSpeechRate(0.8f);
}
int result = tts.setLanguage(Locale.UK);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
Intent installIntent = new Intent();
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
销毁
@Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
Log.d("TAG", "TTS Destroyed");
}
super.onDestroy();
}