2011-07-11 16 views
8

He escrito una pequeña demostración de Android para usar TTS en diferentes idiomas. Tengo un diseño con dos botones, español e inglés. Al presionar el botón, se activa un enunciado en el idioma seleccionado.¿Configurando el lenguaje para TTS programáticamente?

Sin embargo, no puedo cambiar el idioma (setLanguage (Locale locale)). Puedo hacerlo a mano, usando la configuración del teléfono y cambiando el idioma de TTS a EE. UU., RU, italiano, alemán, etc., pero parece que mi código no funciona. ¿Podría decirme dónde está el problema?

Gracias!

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(); 
} 

}

Respuesta

5

De https://web.archive.org/web/20120505124037/http://developer.android.com/resources/articles/tts.html, es posible que desee probar esto:

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

parece extraño, pero eso es lo que hacen referencia (no es como cabría esperar).

+0

Gracias Femi, lo intenté también pero parece que tampoco funciona: S – Ignacio

+4

¡Finalmente solucioné el problema! Tuve que desactivar manualmente la casilla de verificación "Usar siempre mi configuración" en Configuración de texto a voz. – Ignacio

+0

@Ignacio donde puedo encontrar esas configuraciones en samsung galaxy S3 –

Cuestiones relacionadas