TextToSpeech onUtteranceCompleted bug
When I use an Android TextToSpeech
and use OnUtteranceCompletedListener()
I'm having memory issues开发者_运维技巧.
If you do not use OnUtteranceCompletedListener()
then everything is fine.
Why?
final Runnable finish = new Runnable()
{
public void run()
{
Intent intent = new Intent();
intent.setClass(main.this, main.class);
startActivity(intent);
}
};
public void SAY_TTS()
{
final HashMap<String, String> ttsParams = new HashMap<String, String>();
ttsParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "done");
tts = new TextToSpeech(this, new OnInitListener() {
public void onInit(int status)
{
if (status == TextToSpeech.SUCCESS)
{
tts.setLanguage(Locale.ENGLISH);
tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
public void onUtteranceCompleted(String uttID)
{
tts.stop();
tts.shutdown();
handler_finish.post(finish);
}
});
tts.speak("this is a test", TextToSpeech.QUEUE_ADD, ttsParams);
}
}
});
}
If anyone else sees this problem, I think the problem is actually in the case when you're not using the OnUtteranceCompletedListener
. If you don't use it, you're never calling tts.shutdown()
, which means the TextToSpeech
resources are never released, which causes Android to complain when your Activity
ends (because it sees that you haven't cleaned up after yourself).
精彩评论