Poschi
Poschi

Reputation: 21

Reusing TTS-Object on multiple, different activitys in my Android App

I'm program an Android-App for blind people. So I'm using TTS on every Activity in my App.

I don't want to initialize TTS on every activity again because it takes time and resources, so I'm creating a TTS-Object in my application class and use it on the activitys. It works fine.

But documentation says you have to something like this:

protected void onDestroy() {
    super.onDestroy();
    if (mTts != null) {
        mTts.shutdown();
    }
}

But I can't do this because i don't want to shutdown my shared object (each time a activity is destroyed) but if my programm get's distroyed it i should shutdown it. So: Where to call .shutdown() ? Or does anybody know a better way to ensure that TTS is loading quickly?

Upvotes: 2

Views: 1960

Answers (3)

iHearGeoff
iHearGeoff

Reputation: 141

I highly recommend using TTS from within a Service, rather than an Activity. Also, make sure that you wait for the TTS service to finish init before making any calls to speak or synthesize files. You can make a static singleton reference to your TTS object in the service, that you can get from any activity after the service is started. I can post some code snippets later if needed.

Upvotes: 1

Ribbons Almark
Ribbons Almark

Reputation: 234

FYI, TTS is not inheritable between classes. Its been documented on the Android Google Forum. I see your on destroy. Are you also closing onPause?

Upvotes: 0

Poschi
Poschi

Reputation: 21

Or does anybody know a better way to ensure that TTS is loading quickly?

I've found a solution that works for me:

The global TTS-engine does the shutdown only if there is no more link to it. So I'm "using" TTS in the mainactivity of my app and all other activitys are related to it. So there is at any time a reference and the engine doesn't shutdown.

btw: If you doen't make the .shutdown() you get this error:

ERROR/ActivityThread(619): Activity com.your.app has leaked ServiceConnection android.speech.tts.TextToSpeech$1@4377a800 that was originally bound here
ERROR/ActivityThread(619): android.app.ServiceConnectionLeaked: Activity com.your.app has leaked ServiceConnection android.speech.tts.TextToSpeech$1@4377a800 that was originally bound here

Upvotes: 0

Related Questions