SpeechToText and running the ACTION_CHECK_TTS_DATA intent

后端 未结 1 1622
清歌不尽
清歌不尽 2021-01-06 01:50

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

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 02:11

    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;
                        }
                    }
                }
            });
        }
    }
    

    0 讨论(0)
提交回复
热议问题