In this blog the TTS is discussed as it is always nice to use TTS in android . we just need to follow few steps.
make a new project with name AndroidTextToSpeech and a main activity with the name AndroidTextToSpeechActivity.java and a layout as main.xml
1) copy the following code to yours AndroidTextToSpeechActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
speakOut();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
// tts.setPitch(5); // set pitch level
// tts.setSpeechRate(2); // set speech speed rate
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "oops it is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
<EditText android:id="@+id/txtText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter your text here"
android:layout_marginTop="20dip"
android:layout_margin="10dip"/>
<Button android:id="@+id/btnSpeak"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text to Speech"
android:layout_margin="10dip"/>
</LinearLayout>
3) run the program at emulator/device and you can see the output