6

I am setting my TextToSpeech to use a particular language (English - UK), using the locale "en_GB". But it always uses my devices default language. Is there no way to set it programmatically? I have downloaded the files required for the language and when I change my TTS's default language to 'English - UK' it works but when the default is different the programmatic approach does not work. I have scoured the web to my best but am unable to resolve this issue.

    String ttsEngine = "com.google.android.tts";
    txt2Speech = new TextToSpeech(this, this, ttsEngine);
    //Locale ttsLocale = new Locale("eng", "GBR");
    txt2Speech.setLanguage(new Locale("en_GB"));

Tried several methods, but none are working. Can I not set my TTS's language programmatically?

Thank You

EDIT: In response to 'A Honey Bustard'

Other Code:

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener

My onInit()

public void onInit(int status) {
    // TODO Auto-generated method stub

}

Also I'm calling .setLanguage() in my onCreate(), as soon as my TextToSpeech is initialized. Is that correct? Also I'm only calling it once. It is not required to call it every time right? Also I'm testing on a GS7

KISHORE_ZE
  • 1,466
  • 3
  • 16
  • 26

2 Answers2

5

You need to set the language once the Text to Speech Engine has initialised correctly.

public void onInit(int status) {

    switch (status) {

        case SUCCESS:
        // Set the language here
        break;
        case ERROR:
         // Something went wrong. You can't set the language
        break;
    }
}

That should do it.

brandall
  • 6,094
  • 4
  • 49
  • 103
3

Try the second Constructor from Locale that takes two Strings like this :

    txt2Speech.setLanguage(new Locale("en", "GB"));

EDIT :

Yes it is usually ok to do instantiate it in onCreate, and it usually only needs and should be done once.

All I can do is show you my working code, I am setting the default language after instantiating in onCreate() :

    textToSpeech = new TextToSpeech(getApplicationContext(), this);

    textToSpeech.setLanguage(Locale.getDefault());

In my app there are buttons in which you can change the language, which trigger this code (case British English) :

  textToSpeech.setLanguage(new Locale("en", "GB"));

Maybe it is not available somehow , there are some checks you can validate if the language and country is available. You might find your error there.

 if (textToSpeech.isLanguageAvailable(new Locale("en", "GB")) == TextToSpeech.LANG_COUNTRY_AVAILABLE
   && textToSpeech.isLanguageAvailable(new Locale("en", "GB")) == TextToSpeech.LANG_AVAILABLE) 

should return true.

A Honey Bustard
  • 3,433
  • 2
  • 22
  • 38
  • 1
    @A Honey Bustard Thanks for the answer. But still not working. Have also tried `"eng", "GBR"`. Dosen't work. Even tried another language like Spanish but still not working. Any Other Method? Thanks – KISHORE_ZE Jul 04 '16 at 18:07
  • Maybe also try the other TTS Constructor that is using the default Engine : text2Speech = new TextToSpeech(getApplicationContext(), this); – A Honey Bustard Jul 04 '16 at 18:15
  • @A Honey Bustard No, it still uses the default language. :( – KISHORE_ZE Jul 04 '16 at 18:24
  • maybe it is something else in your code, or it is device specific, because I tested it and it works. If theres more code where you use texttospeech post it – A Honey Bustard Jul 04 '16 at 18:31
  • @A Honey Bustard I have edited it and posted some relevant code. Let me know if there is an error in that. Thanks – KISHORE_ZE Jul 04 '16 at 18:58
  • 1
    @A Honey Bustard Thank you for your efforts +1. However the issue was that I had to `.setLanguage()` in the `onInit()` method. That solved it. (@brandall 's comment on my answer). Thank you for helping me anyways. :) – KISHORE_ZE Jul 05 '16 at 09:17