Search code examples
androidlocaletext-to-speech

android TextToSpeech.LANG_AVAILABLE


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

Solution

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