当我的应用程序首次安装或被销毁后打开时,它的效果很好。如果你退出并返回,那么UtteranceProgressListener中的onStart永远不会在OnInitListner中的onInit之后触发。当我在退出后打开应用程序时,其他一切似乎都在工作。日志显示调用onInit,但它永远不会进入UtteranceProgressListener中的onStart。我怎样才能做到这一点?
在我的onCreate中:
// Implement Text to speech feature
tts = getTts();
// set progress listener to the TTS engine
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Log.d("LOOK AT ME!!!", "ttsUtteranceListener - onStart");
}
@Override
public void onError(String utteranceId) {
Log.d("LOOK AT ME!!!", "ttsUtteranceListener - onError");
}
@Override
public void onDone(String utteranceId) {
Log.d("LOOK AT ME!!!", "ttsUtteranceListener - onDone");
if (processStarted) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("LOOK AT ME!!!",
"ttsUtteranceListener - speech,startListening");
speech.startListening(intent);
}
});
} else {
...
}
processStarted = false;
}
});
这里是“getTts()”:
private TextToSpeech getTts() {
if (tts == null) {
// Implement Text to speech feature
tts = new TextToSpeech(this, new OnInitListener() {
@Override
public void onInit(int status) {
Log.d("LOOK AT ME!!!", "ttsInitListener - onInit");
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.getDefault());
} else {
tts = null;
Toast.makeText(MainActivity.this,
"Failed to initialize TTS engine.",
Toast.LENGTH_SHORT).show();
}
}
});
}
return tts;
}
这是我的生命周期调用(onPause和onResume):
// --- LIFECYCLE ---
@Override
public void onResume() {
super.onResume();
Log.d("LOOK AT ME!!!", "onResume");
if (tts == null) {
tts = getTts();
}
}
@Override
public void onPause() {
// Stop then shut down the TextToSpeech Engine
if (tts != null) {
tts.stop();
tts.shutdown();
tts = null;
}
super.onPause();
}