We have a feature called TextToSpeech and is available for Android 1.6 and higher. Usind TTS we can build an app that speaks the text of various languages.
Below are the steps to do so:
Simply create an xml layout that includes an input filed with a button to speak that.
Create an Activty that implements TextToSpeech.OnInitListener
Then in the Activity, in the onClick() event of that button, fetch the text from input fieldand pass it to tts.speak() method like:
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
tts above is the object of TextToSpeech, forget not to initilize the same before using it in onCreate() method.
TextToSpeech.OnInitListener has its public method:
@Override
public abstract void onInit (int status)
{
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TextToSpeech", "Language not supported");
} else {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
} else {
Log.e("TextToSpeech", "Unable to initialize");
}
}
In the onDestroy() method of the activity, shut down the tts like:
@Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
Thanks and all the best :)
0 Comment(s)