I'm trying to stop TextToSpeech
, when back button pressed. But speech not stopping, even if I close my App. Only when I clear cache, speech stopping. How can I solve this? Please, help me to understand.
private boolean mShouldSpeak = true;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cat);
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
tts.setEngineByPackageName(enginePackageName);
tts.setLanguage(Locale.getDefault());
tts.setPitch(0);
tts.setSpeechRate(1);
speak();
}
}
});
}
private void speak() {
if (mShouldSpeak == true)
{
tts.speak("Автор: " +getResources().getString(R.string.catAuthor), TextToSpeech.QUEUE_ADD, null);
tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
tts.speak(getResources().getString(R.string.catName), TextToSpeech.QUEUE_ADD, null);
tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
tts.speak(getResources().getString(R.string.catDesc), TextToSpeech.QUEUE_ADD, null);
tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
}
}
@Override
protected void onDestroy() {
if (tts != null)
{
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onBackPressed() {
onDestroy();
super.onBackPressed();
}