import android.app.Activity; import android.speech.tts.*; sbool androidSay_keepEngine = false; // locale = language, e.g. Locale.GERMAN static void androidSay(final S text, final Locale locale) { if (androidSay_keepEngine) { androidSay_keepEngine(text, locale); ret; } final new Flag flag; final TextToSpeech[] tts = new TextToSpeech[1]; tts[0] = new TextToSpeech(androidContext(), new TextToSpeech.OnInitListener() { public void onInit(int status) { try { if (status != TextToSpeech.SUCCESS) fail("Speech engine initialization failed"); int result = tts[0].setLanguage(locale); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) fail("Language is not supported"); result = tts[0].setOnUtteranceProgressListener(new UtteranceProgressListener() { public void onDone(String utteranceId) { print("TTS done"); tts[0].shutdown(); flag.raise(); } public void onError(String utteranceId) { print("TTS error"); tts[0].shutdown(); flag.raise(); } public void onStart(String utteranceId) { } }); if (result != TextToSpeech.SUCCESS) print("Could not set listener"); print(text); new HashMap params; params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, randomID(8)); tts[0].speak(text, TextToSpeech.QUEUE_FLUSH, params); } catch (Throwable e) { e.printStackTrace(); flag.raise(); } } }); flag.waitUntilUp(); }