I have a text that is refreshed, and every time this happens.
I call a static method method speak() located in another class with this structure:
public class Voc {
static TextToSpeech mytts;
public static void speak(String myText){
mytts=new TextToSpeech(c, new TextToSpeech.OnInitListener() {
//.........parameters and config not related to issue........
}
mytts.speak();
}
public static off(){
mytts.stop();
mytts.shutdown();
}
}
The problem is that if I call off() after multiple speak() invocations, the tts continues to speak.
This doesn't happens if I invoke speak() only a single time and not multiple time as happens in the refresh method.
This raise me the suspect that off() method doesn't work for all instances despite I have assigned all the new TextToSpeech(...) invocations to same static field in the class Voc.
How could I fix this issue?
Your issue is due to the fact that you instantiate a new TextToSpeech object on each call of speak(), this is not needed. With the updates I provided below, you have a single TTS object that you turn off and back on, not multiple so when you turn it off the single TTS object is stopped and your issue is no longer present.
I have tested this on my phone and it should work exactly as you need.
Please check my updated code below:
Updated Voc Class
Main Activity For Test
Example Layout For Test