5

I've implemented the TextToSpeech integration exactly as mentioned in this blog post. After I've added it to my program it's now interfering with my other intents.

For example:

  1. List item
  2. User starts app
  3. User invokes load activity
  4. User picks a file to load, and activity returns fileanme to load in the intent
  5. Main activity starts, and realizes it needs to load a filename so it starts doing so
  6. The check for TTS needs to be done so I launch the ACTION_CHECK_TTS_DATA intent
  7. This pauses the main activity again and the loading process gets interrupted
  8. When the TTS check returns, the loading never happened.

When do I need this TTS check? Can I just do it once on application start up? It's causing my application to load slowly. I would like this load to be performed in a separate thread if possible.

Mike Bailey
  • 12,479
  • 14
  • 66
  • 123
swinefeaster
  • 2,525
  • 3
  • 30
  • 48

1 Answers1

8

Do the check once. Once the data is installed, it's very unlikely that the user will need to ever do it again. Once the data is installed, there's no way for the user to delete it, even if they wanted to.

Also, don't use the ACTION_CHECK_TTS_DATA Intent, that's awkward to use.

Instead, do the following:

  1. Create TextToSpeech
  2. OnInit, check isLanguageAvailable() if it is, your app is all set. if not, send the ACTION_INSTALL_TTS_DATA

Here's some code that initializes a TextToSpeech in the way I suggest. As a bonus, it sets the language as well.

public class DemoCreateTTS
{
    private static final String TAG = "DemoCreateTTS";

    private TextToSpeech tts;

    public void createTextToSpeech(final Context context, 
            final Locale locale)
    {
        tts = new TextToSpeech(context, new OnInitListener()
        {
            @Override
            public void onInit(int status)
            {
                if (status == TextToSpeech.SUCCESS)
                {
                    Locale defaultOrPassedIn = locale;
                    if (locale == null)
                    {
                        defaultOrPassedIn = Locale.getDefault();
                    }
                    // check if language is available
                    switch (tts.isLanguageAvailable(defaultOrPassedIn))
                    {
                        case TextToSpeech.LANG_AVAILABLE:
                        case TextToSpeech.LANG_COUNTRY_AVAILABLE:
                        case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
                            Log.d(TAG, "SUPPORTED");
                            tts.setLanguage(locale);
                            //pass the tts back to the main
                            //activity for use
                            break;
                        case TextToSpeech.LANG_MISSING_DATA:
                            Log.d(TAG, "MISSING_DATA");
                                Log.d(TAG, "require data...");
                                Intent installIntent = new Intent();
                                installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                                context.startActivity(installIntent);
                            break;
                        case TextToSpeech.LANG_NOT_SUPPORTED:
                            Log.d(TAG, "NOT SUPPORTED");
                            break;
                    }
                }
            }
        });
    }
}
gregm
  • 12,019
  • 7
  • 56
  • 78
  • @gregm [Why](http://stackoverflow.com/q/11550746/636571) is the ACTION_CHECK_TTS_DATA Intent “awkward to use”? Thanks +1 for now. – an00b Jul 18 '12 at 21:51
  • Hello, I tested using isLanguageAvailable, but it's not giving the desired result. For example, Korean's voice data isn't installed. However when I called isLanguageAvailable on the Korean's voice data when it's not downloaded, it simply returned LANG_COUNTRY_AVAILABLE, which is also correct. The locale is available for the engine just the voice data not downloaded yet. However, due to this I can never check to see if the voice data is downloaded or not, since I'm never getting LANG_MISSING_DATA. Is the solution no longer applicable? Thanks! – Charles Li Jun 18 '17 at 16:00