Dark Mode
Image

Android Fragments

Android Service

Android AlarmManager

Camera Tutorial

Sensor Tutorial

Android Graphics

Android Animation

Android Web Service

Android MCQ

Android Quiz

TextToSpeech Tutorial

In android, you can convert your text into speech by the help of TextToSpeech class. After completion of the conversion, you can playback or create the sound file.

Constructor of TextToSpeech class

  • TextToSpeech(Context context, TextToSpeech.OnInitListener)

Methods of TextToSpeech class

The commonly used methods of TextToSpeech class are as follows:

Method Description
int speak (String text, int queueMode, HashMap params) converts the text into speech. Queue Mode may be QUEUE_ADD or QUEUE_FLUSH. Request parameters can be null, KEY_PARAM_STREAM, KEY_PARAM_VALUME etc.
int setSpeechRate(float speed) it sets the speed for the speech.
int setPitch(float speed) it sets the pitch for the speech.
int setLanguage (Locale loc) it sets the locale specific language for the speech.
void shutdown() it releases the resource set by TextToSpeech Engine.
int stop() it interrupts the current utterance (whether played or rendered to file) and discards other utterances in the queue.

TextToSpeech.OnInitListener Interface

You need to implement TextToSpeech.OnInitListener interface, for performing event handling on TextToSpeech engine.

Method of TextToSpeech.OnInitListener Interface

There is only one method in this interface.

Method Description
void onInit (int status) Called to signal the completion of the TextToSpeech engine initialization. The status can be SUCCESS or ERROR.

Android TextToSpeech Example

Let's write the code to convert text into voice.

activity_main.xml

Drag one textview, one edittext and one button for the layout. Now the activity_main.xml file will look like this:

File: activity_main.xml

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MainActivity" >  
  
    <EditText  
        android:id="@+id/editText1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="77dp"  
        android:layout_marginTop="42dp"  
        android:ems="10" >  
  
        <requestFocus />  
    </EditText>  
  
    <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/editText1"  
        android:layout_below="@+id/editText1"  
        android:layout_marginLeft="59dp"  
        android:layout_marginTop="39dp"  
        android:text="Speak" />  
  
    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/editText1"  
        android:layout_alignBottom="@+id/editText1"  
        android:layout_alignParentLeft="true"  
        android:text="Enter Text:" />  
  
</RelativeLayout>  

Activity class

Let's see the code to speak the given text.

File: MainActivity.java

package com.example.texttospeech;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
import java.util.Locale;  
  
import android.app.Activity;  
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 MainActivity extends Activity implements  
TextToSpeech.OnInitListener {  
/** Called when the activity is first created. */  
  
private TextToSpeech tts;  
private Button buttonSpeak;  
private EditText editText;  
  
@Override  
public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.activity_main);  
  
tts = new TextToSpeech(this, this);  
buttonSpeak = (Button) findViewById(R.id.button1);  
editText = (EditText) findViewById(R.id.editText1);  
  
buttonSpeak.setOnClickListener(new View.OnClickListener() {  
    @Override  
    public void onClick(View arg0) {  
        speakOut();  
    }  
  
});  
}  
  
@Override  
public void onDestroy() {  
// Don't forget to shutdown tts!  
if (tts != null) {  
    tts.stop();  
    tts.shutdown();  
}  
super.onDestroy();  
}  
  
@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!");  
}  
  
}  
  
private void speakOut() {  
String text = editText.getText().toString();  
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);  
}  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.activity_main, menu);  
        return true;  
    }  
  
}  

You need to run it on the Real Device (e.g. Mobile) to test the application.

Comment / Reply From