private int myEngineIndex; // loop counter when initializing TTS engines // Called from onCreate to colled all languages and voices from all TTS engines, initialize the spinners private void getEnginesAndLangs() { myTts = new TextToSpeech(AndyUtil.getAppContext(), null); List<EngineInfo> engines; engines = myTts.getEngines(); // at least we can get the list of engines without initializing myTts object… try { myTts.shutdown(); } catch (Exception e) {}; myTts = null; myEngineIndex = 0; // Initialize the loop iterating through all TTS engines if (engines.size() > 0) { for (EngineInfo ei : engines) allEngines.add(new EngLang(ei)); myTts = new TextToSpeech(AndyUtil.getAppContext(), ttsInit, allEngines.get(myEngineIndex).name()); // DISRUPTION 1: we can’t continue here, must wait until ttsInit callback returns, see below } }
public void onInit(int status) { if (myEngineIndex < allEngines.size()) { if (status == TextToSpeech.SUCCESS) { // Ask a TTS engine which voices it currently has installed EngLang el = allEngines.get(myEngineIndex); Intent in = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); in = in.setPackage(el.ei.name); // set engine package name try { startActivityForResult(in, LANG_REQUEST); // goes to onActivityResult() // DISRUPTION 2: we can’t continue here, must wait for onActivityResult()… } catch (Exception e) { // ActivityNotFoundException, also got SecurityException from com.turboled if (myTts != null) try { myTts.shutdown(); } catch (Exception ee) {} if (++myEngineIndex < allEngines.size()) { // If our loop was not finished and exception happened with one engine, // we need this call here to continue looping… myTts = new TextToSpeech(AndyUtil.getAppContext(), ttsInit, allEngines.get(myEngineIndex).name()); } else { completeSetup(); } } } } else