The TextToSpeech constructor looks like it’s designed to be ‘owned’ by an Activity. I’m producing an app with multiple different Activities, and I don’t want to have to initialise a new TextToSpeech instance for each – I want the speech to carry on smoothly even if the Activity is changing.
My idea is to have a static TextToSpeech object accessed by all activities, initialised by the first one.
- Does anyone know if the TextToSpeech implementation is thread-safe? I’m guessing not, but someone out there might know.
- If I initialise it with the Context of my default Activity, will the TextToSpeech instance stop working when the Activity is destroyed?
Thanks to those that told me to pass the ApplicationContext. Turned out that was the easy bit… The hard bit was whether the TextToSpeech object is guaranteed thread-safe.
Thanks for answers telling me how to make something thread-safe / assuming that it is, but the question was about whether the object already is. I probably should have said, I’m fine with implementing thread-safety, but I wanted to know whether I need to bother. And I don’t want to assume thread-safety without being certain.
I ran the following and it seemed to work. So I assume the Android SDK TTS is thread-safe, but can’t find any documentation saying that it’s safe to assume this across all devices, so I’ll be wrapping my TTS instance for the time being!