Create a simple app which reads out what you write
In this post you will be guided to create a simple android application which reads out loud the text you write. This application uses Text To Speech (TTS) class. This is a basic application which has 1 EditText field and a single button. The EditText field is basically takes input and once you tap that button, it will read the entered text. This program uses your default TTS output voice.
Demo:
Implementing TTS Class:
Overriding TTS onInit method:
Overriding TTS onDestroy method:
The speakOut method method:
Alternatively you can download the whole project here.
Found any bugs, feel free to report them, we will be glad to fix them! :)
Demo:
When tapped on Speak, It reads loudly "Hello I'm Sid".
Implementing TTS Class:
public class MainActivity extends Activity implements TextToSpeech.OnInitListener
Overriding TTS onInit method:
@Override public 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("TTS", "This Language is not supported"); } else { buttonSpeak.setEnabled(true); speakOut(); } } else { Log.e("TTS", "Initilization Failed!"); }
Overriding TTS onDestroy method:
@Override public void onDestroy() { // Don't forget to shutdown tts! if (tts != null) { tts.stop(); tts.shutdown(); } super.onDestroy(); }
The speakOut method method:
private void speakOut() { String text = editText.getText().toString(); tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); }
Alternatively you can download the whole project here.
Found any bugs, feel free to report them, we will be glad to fix them! :)
Comments
Post a Comment