I am writing an activity that speaks to the user and I'd really like to block on TextToSpeech initialization - or else time out. How can I get my thread to wait?
I tried:
while (! mIsTtsReady || i>limit) try { Thread.sleep(100); i++; ... };
along with:
@Override
public void OnInit() { mIsTtsReady = true; } // TextToSpeech.OnInitListener
But OnInit() never runs. It seems that OnInit executes within my thread (via a message to my activities Looper?), which is in a tight sleep() loop.
It seems wrong to put the bulk of my code (the "after init" stuff) into OnInit itself. Moving it into a Runnable, then start()ing it, and sleeping as above within that runnable works. But now my code is in a new thread and needs explicit syncing with the UI etc, and it all gets messy really quickly.
What is the right way - or at least one that works :) - to do this?
Thanks!
You need to initialize the TTS system within e.g. the activities onCreate() method, so that you can use it later when the user e.g. clicks a button.
See e.g. https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/OneTweetActivity.java#L62 where setupspeak() is called and then later speak() ( https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/OneTweetActivity.java#L344 ) which is then called when the user clicks the 'speak' button.
I also used the thread sleep, but it seems it doesn't work anymore and actually there is a better way handling this. Just pass a callback using simple interface like: