SpeechToText和运行ACTION_CHECK_TTS_DATA意图意图、SpeechToText、ACTION_CHECK_TTS_DATA

2023-09-07 11:56:55 作者:烟酒疗心伤

我已经完全实现中所提到的这个博客帖子。之后,我已经把它添加到我的程序它现在与我的其他意图干扰秒。

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.

例如:

项目 在用户启动应用程序 在用户调用负载活性 在用户选择要加载的文件和活动的回报fileanme在意向来加载 主要活动开始,并实现它需要所以它开始这样​​做加载一个文件名 在需要检查TTS做,所以我启动 ACTION_CHECK_TTS_DATA 意图 在此暂停主要活动的再次的和装载过程被中断 当TTS检查返回,装载从来没有发生过。 List item User starts app User invokes load activity User picks a file to load, and activity returns fileanme to load in the intent Main activity starts, and realizes it needs to load a filename so it starts doing so The check for TTS needs to be done so I launch the ACTION_CHECK_TTS_DATA intent This pauses the main activity again and the loading process gets interrupted When the TTS check returns, the loading never happened.

当我需要这个TTS检查?我可以做一次应用程序启动?它使我的应用程序加载速度慢。我希望在可能的话一个独立的线程执行此负载。

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.

推荐答案

做检查一次。一旦数据被安装了,这是非常不可能的用户将需要以后再去做。数据一旦被安装,有没有办法让用户删除,即使他们想。

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.

另外,不要使用ACTION_CHECK_TTS_DATA意图,这是尴尬使用。

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

相反,请执行以下操作:

Instead, do the following:

创建TextToSpeech 的OnInit,检查isLanguageAvailable() 如果是,您的应用程序的所有设置。 如果不是,发送ACTION_INSTALL_TTS_DATA Create TextToSpeech OnInit, check isLanguageAvailable() if it is, your app is all set. if not, send the ACTION_INSTALL_TTS_DATA

下面是一些code初始化中,我建议的方式TextToSpeech。作为奖励,它设置了语言。

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