In Android providing a best feature (from Android 1.6) called Text to Speech (TTS) which speaks the text. This example shows how to work with android text to speech.
UI Layout(androidtexttospeech.xml)
Place this xml file in resource/layout folder
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dip"
android:text="Android Text To Speech"
android:textSize="25dip"
android:textStyle="italic" />
<EditText
android:id="@+id/inputText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:layout_marginTop="20dip"
android:hint="Please enter some text to speak" />
<Button
android:id="@+id/speakButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:text="Speak Out Now" />
</LinearLayout>
Android Activity class(TexttoSpeechActivity.class)
package com.androidsurya.texttospeechexample;
import com.androidhive.texttospeech.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class TexttoSpeechActivity extends Activity implements OnInitListener {
private EditText inputTextEv;
private Button speakButton;
private int MY_DATA_CHECK_CODE = 99;
private TextToSpeech texttospeech;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.androidtexttospeech);
inputTextEv = (EditText) findViewById(R.id.inputText);
speakButton = (Button) findViewById(R.id.speakButton);
speakButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String text = inputTextEv.getText().toString();
if (text != null && text.length() > 0) {
Toast.makeText(TexttoSpeechActivity.this,
"TTS Saying: " + text, Toast.LENGTH_LONG).show();
texttospeech.speak(text, TextToSpeech.QUEUE_ADD, null);
}
}
});
// Check TTS Engine in your mobile or not.
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// TTS in your mobile,so create the TTS object
texttospeech = new TextToSpeech(this, this);
} else {
// TTS in not in your mobile,so install TTS in your mobile
Intent installIntent = new Intent();
installIntent
.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
Toast.makeText(TexttoSpeechActivity.this,
"Text-To-Speech engine is instaled in your mobile",
Toast.LENGTH_LONG).show();
} else if (status == TextToSpeech.ERROR) {
Toast.makeText(
TexttoSpeechActivity.this,
"Error occurred while installing Text-To-Speech engine in your mobile",
Toast.LENGTH_LONG).show();
}
}
}
Register the Activity in Android Manifest file
<activity
android:name="com.androidsurya.texttospeechexample.TexttoSpeechActivity"
android:label="@string/app_name" >
Output Screenshot:
Some of other methods are in TextToSpeech Class
setPitch (float pitch)
Sets the speech pitch for the TextToSpeech engine. This has no effect on any pre-recorded speech.
setSpeechRate (float speechRate)
Sets the speech rate. This has no effect on any pre-recorded speech.
setLanguage(Locale loc)
Sets the text-to-speech language. The TTS engine will try to use the closest match to the specified language as represented by the Locale, but there is no guarantee that the exact same Locale will be used. Use isLanguageAvailable(Locale) to check the level of support before choosing the language to use for the next utterances.
isSpeeking()
Checks whether the TTS engine is busy speaking.
synthesizeToFile (String text, HashMap<String, String> params, String filename)
Synthesizes the given text to a file using the specified parameters.
getLanguage ()
Returns a Locale instance describing the language currently being used by the TextToSpeech engine.
For More information about Text to Speech : Android Developers site