Search This Blog

Monday, March 26, 2012

Android Text to Speech

TextToSpeech is the great feature of Android. Text to Speech (TTS) which speaks the text in different languages usually selected by the user or you can also put the default user language for the TTS.
First of all make the xml file with the one EditText and Button. And make the following code working in your java file for the TTS. You need to implement the Interface of the TTS TextToSpeech.OnInitListener.

import java.util.Locale;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidTextToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */

private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;

public void onCreate(Bundle savedInstanceState) {

                       tts = new TextToSpeech(this, this);
                       btnSpeak = (Button) findViewById(;
                       txtText = (EditText) findViewById(;

                       // button on click event
                       btnSpeak.setOnClickListener(new View.OnClickListener() {

                                            public void onClick(View arg0) {


           public void onDestroy() {
                     // Don't forget to shutdown tts!
                     if (tts != null) {

            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");


                                Log.e("TTS", "Initilization Failed!");


        private void TTS() {

                   String text = txtText.getText().toString();

                   tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

You can change the language by the oneline code 


You can select the speed of the speaking via putting the setPitch() method.Default value is 1.0 of the pitch rate.You can select the pitch rate higher or lower according to the requirement.

The speed rate can be set using setSpeechRate(). This also will take default of 1.0 value. You can double the speed rate by setting 2.0 or make half the speed level by setting 0.5.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.