import android.app.Activity; import android.speech.tts.*; static TextToSpeech androidSay_keepEngine_tts; // TODO: better way to sync than synchronizing on main class static synchronized void androidSay_keepEngine(final S text, final Locale locale) { final new Flag flag; if (androidSay_keepEngine_tts == null) androidSay_keepEngine_tts = new TextToSpeech(androidContext(), new TextToSpeech.OnInitListener() { public void onInit(int status) { try { if (status != TextToSpeech.SUCCESS) fail("Speech engine initialization failed"); androidSay_keepEngine_go(text, locale, flag); } catch (Throwable e) { e.printStackTrace(); flag.raise(); } } }); else androidSay_keepEngine_go(text, locale, flag); flag.waitUntilUp(); } static void androidSay_keepEngine_go(S text, Locale locale, final Flag flag) { int result = androidSay_keepEngine_tts.setLanguage(locale); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) fail("German is not supported"); /*if (empty(text)) { // Empty text = Initialize engine only flag.raise(); ret; }*/ result = androidSay_keepEngine_tts.setOnUtteranceProgressListener(new UtteranceProgressListener() { public void onDone(String utteranceId) { print("TTS done"); flag.raise(); } public void onError(String utteranceId) { print("TTS error"); flag.raise(); } public void onStart(String utteranceId) { } }); if (result != TextToSpeech.SUCCESS) print("Could not set listener"); //if (nempty(text)) print("[speech output] " + text); new HashMap params; params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, randomID(8)); androidSay_keepEngine_tts.speak(text, TextToSpeech.QUEUE_FLUSH, params); }