9

I have written a small Android Demo to use TTS in different languages. I have a layout with two buttons, Spanish and English. Pressing the button triggers an utterance in the language selected.

However, I can't change the language (setLanguage (Locale locale)). I can do it by hand, using the phone settings and changing the TTS language to US, UK, Italian, German, etc, but my code doesn't seem to work. Could you tell me where the problem is?

Thank you!!

package com.ignacio.SpeakAPP;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import java.util.Locale;

public class SpeakAPPActivity extends Activity implements OnInitListener {
private static final String TAG = "TextToSpeechDemo";
private TextToSpeech mTts;
public boolean Passer = false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

/** Handle the action of the English Button **/
public boolean talknowEN(View v)
{

    mTts = new TextToSpeech (this, this);
    return Passer = false;
}

/** Handle the action of the Spanish Button **/
public boolean talknowES(View v)
{
    mTts = new TextToSpeech (this, this);   
    return Passer = true;
}

/** TTS **/
public void onInit (int status){

    if (status ==TextToSpeech.SUCCESS){

        if(Passer==false){
            //If English Button was activated
            //Initialize speech to text, set language to english and send utterance
            mTts.setLanguage(Locale.US);
            mTts.speak("How may I help you?", TextToSpeech.QUEUE_FLUSH, null);  
        }else{
            //If Spanish Button was activated
            //Initialize speech to text, check if spanish is available, set locale to spanish and send utterance

            Locale loc = new Locale ("es", "ES");
            mTts.setLanguage(loc);
            if (result2==TextToSpeech.LANG_MISSING_DATA||result2==TextToSpeech.LANG_NOT_SUPPORTED){
                Log.e(TAG, "Language is not available");
            }else {
                mTts.speak("Como puedo ayudarte?", TextToSpeech.QUEUE_FLUSH, null);
            }

        }

    }else {
        Log.e(TAG, "Could not initialize TextToSpeech");
    }

}


@Override
protected void onDestroy(){
    super.onDestroy();
    mTts.shutdown();
} 

}

Charles
  • 50,943
  • 13
  • 104
  • 142
Ignacio
  • 135
  • 1
  • 2
  • 8

1 Answers1

5

From https://web.archive.org/web/20120505124037/http://developer.android.com/resources/articles/tts.html, you may want to try this:

Locale loc = new Locale ("spa", "ESP");

Seems odd, but that's what they reference (not es like one would expect).

elixenide
  • 44,308
  • 16
  • 74
  • 100
Femi
  • 64,273
  • 8
  • 118
  • 148
  • Thanks Femi, I tried that too but it doesn't seem to work either :S – Ignacio Jul 12 '11 at 17:55
  • 4
    I finally fixed the problem! I had to manually uncheck the "always use my settings" checkbox under Text To Speech Settings. – Ignacio Aug 16 '11 at 20:17
  • @Ignacio where i can find those settings in samsung galaxy S3 – Shajeel Afzal Sep 16 '13 at 10:52
  • @Ignacio ...Thank you!! I was getting MAD!!!! Now I wonder if I can make it PROGRAMMATICALLY, that would be really nice! – Phantômaxx Aug 05 '14 at 18:42
  • 1
    @Femi, just want to know that if user manually install some language pack (which is not supported by `TTS` ), will `TTS` work for those languages ?. – maddy d Oct 12 '15 at 11:19
  • In the link it says Locale.ITALIAN has some "Pretty interesting results" and I couldn't run it after French in T867 device, but works fine with N950F Samsung devices. – Bay Oct 01 '20 at 22:36