开发者

android TextToSpeech.LANG_AVAILABLE

开发者 https://www.devze.com 2023-01-29 00:44 出处:网络
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.

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 开发者_Go百科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.");
    }
}


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..


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

0

精彩评论

暂无评论...
验证码 换一张
取 消