Text To Speech
De WikiCode
| Tweet |
Objetivo
Uso del api TextToSpeech
Código
Muestra como trabajar con TextToSpeech, como inicializarlo, seleccionar el idioma en el que se desea leer el texto, y por supuesto como decirle al sistema que lo lea.
Public class SimpleWeatherForecast2Activity extends Activity implements TextToSpeech.OnInitListener{ TextToSpeech tts; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Iniciazamos TextToSpeech tts = new TextToSpeech(this, this); textToSpeak("Hola mundo"); } //Inicializa TextToSpeech public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { //Cargamos el idioma por defecto en el dispositivo int result = tts.setLanguage(Locale.getDefault()); //En caso de que no este soportado o no encuentre, mostramos un error if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { Toast msg = Toast.makeText(getApplicationContext(), "This Language is not supported", Toast.LENGTH_SHORT); msg.show(); Log.e("TTS", "This Language is not supported"); } else { //Inicializacion correcta Log.e("TTS", "Initilization Failed!"); } } } @Override public void onDestroy() { // NO OLVIDARSE de parar el motor de TextToSpeech if (tts != null) { tts.stop(); tts.shutdown(); } super.onDestroy(); } //Lee el texto que se le envia. private void textToSpeak(String textToRead) { tts.speak(textToRead, TextToSpeech.QUEUE_FLUSH, null); }