scope androidSay_keepEngine. import android.app.Activity; import android.speech.tts.*; sbool #verbose, #inCall; static TextToSpeech #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 (tts == null) { if (verbose) print("Initialising TTS"); tts = new TextToSpeech(androidContext(), new TextToSpeech.OnInitListener() { public void onInit(int status) { try { if (status != TextToSpeech.SUCCESS) fail("Speech engine initialization failed"); go(text, locale, flag); } catch (Throwable e) { e.printStackTrace(); flag.raise(); } } }); if (verbose) print("TTS object created"); } else androidSay_keepEngine_go(text, locale, flag); flag.waitUntilUp(); } static void #go(S text, Locale locale, final Flag flag) { if (verbose) print("TTS go: " + text); int result = 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 = 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 (verbose) print("TTS started"); } }); 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)); if (inCall) params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, str(AudioManager.STREAM_VOICE_CALL)); tts.speak(text, TextToSpeech.QUEUE_FLUSH, params); } end scope