android TextToSpeech.LANG_AVAILABLE

发布于 2024-10-06 04:19:18 字数 2193 浏览 7 评论 0原文

我还在为我的 tts 苦苦挣扎。在我的模拟器中一切都运行良好,但由于我的手机是瑞典的,我需要检查 Locale.US(我的文本是英文)并安装它。 而且..这就是我认为我所做的,但区域设置不可用并返回-1?我做错了什么吗,我以为英语总是可用的?

   public void onInit(int status) {
    // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
    if (status == TextToSpeech.SUCCESS) {
        // Set preferred language to US english.
        // Note that a language may not be available, and the result will indicate this.
        int result = mtts.setLanguage(Locale.US);

        if (result == TextToSpeech.LANG_MISSING_DATA ||
            result == TextToSpeech.LANG_NOT_SUPPORTED) {
           // Lanuage data is missing or the language is not supported.
            Log.e(TAG, "Language is not available.");
           //install it?

            result = mtts.isLanguageAvailable(Locale.US);
            if (result == TextToSpeech.LANG_AVAILABLE) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Language missing, install English speech?")
                   .setCancelable(false)
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {

                           //installera
                           Intent installIntent = new Intent();
                           installIntent.setAction(
                               TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                           startActivity(installIntent);


                       }
                   })
                   .setNegativeButton("No", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                       }
                   });
            AlertDialog alert = builder.create();
            } 
           //not installable
            Log.e(TAG, "Language not installable");

        } else {

            // The TTS engine has been successfully initialized.
            speak();
        }
    } else {
        // Initialization failed.
        Log.e(TAG, "Could not initialize TextToSpeech.");
    }
}

I'm still struggling with my tts. Everything works nicely in my emulator, but since my phone is swedish I need to check for the Locale.US (my texts are in english) and install it.
And.. that's what I thought I did, but the Locale is unavailable and returns -1? Am I doing something wrong, I thought the english languages were always available?

   public void onInit(int status) {
    // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
    if (status == TextToSpeech.SUCCESS) {
        // Set preferred language to US english.
        // Note that a language may not be available, and the result will indicate this.
        int result = mtts.setLanguage(Locale.US);

        if (result == TextToSpeech.LANG_MISSING_DATA ||
            result == TextToSpeech.LANG_NOT_SUPPORTED) {
           // Lanuage data is missing or the language is not supported.
            Log.e(TAG, "Language is not available.");
           //install it?

            result = mtts.isLanguageAvailable(Locale.US);
            if (result == TextToSpeech.LANG_AVAILABLE) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Language missing, install English speech?")
                   .setCancelable(false)
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {

                           //installera
                           Intent installIntent = new Intent();
                           installIntent.setAction(
                               TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                           startActivity(installIntent);


                       }
                   })
                   .setNegativeButton("No", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                       }
                   });
            AlertDialog alert = builder.create();
            } 
           //not installable
            Log.e(TAG, "Language not installable");

        } else {

            // The TTS engine has been successfully initialized.
            speak();
        }
    } else {
        // Initialization failed.
        Log.e(TAG, "Could not initialize TextToSpeech.");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一枫情书 2024-10-13 04:19:18

似乎可以工作..如果手机未连接到计算机。对此一无所知,但是当我断开连接时,我再次运行我的应用程序,它获取了语言包并安装了它,现在它可以流利地讲英语了。

Seem to work.. if the phone isn't connected to the computer. Had no clue about that, but when i disconnected I ran my app again, and it fetched the language pack and installed it and now it speaks english fluently..

っ左 2024-10-13 04:19:18

你有一个小错误。
而不是 :

if (result == TextToSpeech.LANG_AVAILABLE) 

你应该有:

if (result != TextToSpeech.LANG_AVAILABLE) 

然后对话框将显示

you had a small bug.
instead of :

if (result == TextToSpeech.LANG_AVAILABLE) 

you should have:

if (result != TextToSpeech.LANG_AVAILABLE) 

then the dialog will show

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文