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
public void init(final Context context, final OnProgressStart onStart) {
_mTts = new TextToSpeech(context, new OnInitListener() {
// Implements TextToSpeech.OnInitListener.
public void onInit(int status) {
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (status == TextToSpeech.SUCCESS) {
_isInitialized = true;
Services.getTTSS().setLanguage();
LogUtil.logInfo("TTS connected", this);
if(onStart != null)
onStart.onStart();
} else {
// Initialization failed.
Log.e(Constants.LOGTAG, this.getClass().getName()
+ " Could not initialize TextToSpeech.");
}
}
});
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:
init(context, new OnProgressStart() {
public void onStart(String... args) {
startSpeak();
}
});
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.