KISHORE_ZE KISHORE_ZE - 5 months ago 10
Java Question

TextToSpeech setLanguage not working?

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

Answer

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.