Android Text to Speech Tutorial: Step by step Text To Speech Android example

This post describes how to use Android text to speech (TTS) in your Android app. Using this step by step guide we will build a Text To speech Android example

I’m sure you asked yourself how do I use text to speech on Android or how do you get text messages read by your Android smartphone. This tutorial will respond to these question showing how to use text to speech on Android step by step.

Android text to speech is very interesting because it can add some nice features to an android app. There are several scenarios where the text to speech Android feature is useful.

What is text to speech? Text to Speech is a feature of Android platform that can be used to “read” the words and make the app talking, or more in detail to synthesize text.
This post covers how to implement TTS in Android and how to control some interesting properties of the speech engine. This Text to Speach Android example will be very easy to focus our attention on how to use text to speech in Android.

Let us a built the Android App.

Android Text to speech example app UI

The Text to Speech Android example app has an EditText widget where we write the sentence that the Android app will read. Moreover, the Android app has some controls to control the speech engine:

  • The pitch
  • The speed

If you are interested in the Android app source code you can download it using the button below:


The final result of the Android app is shown here.

android text to speech

Text to speech Engine

The first thing we have to do is initializing the TTS engine. The class that controls the engine is called TextToSpeech

[java]engine = new TextToSpeech(this, this);[/java]

where the first parameter is the Context and the other one is the listener. The listener is used to inform our app that the engine is ready to be used. In order to be notified we have to implement TextToSpeech.OnInitListener, so the Android app becomes:

[java]public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
….
@Override
public void onInit(int status) {
Log.d("Speech", "OnInit – Status ["+status+"]");

if (status == TextToSpeech.SUCCESS) {
Log.d("Speech", "Success!");
engine.setLanguage(Locale.UK);
}
}
}[/java]

The callback method named onInit is invoked when the initialization process completes. In this method,  we set the default language that the engine uses to read the sentence.

You can use Android Voice feature in several scenarios.  Learn how to use Android Voice to control a device

TTS Android example: How to read the sentence

Now our engine is ready to use, we need simply pass the string we want to read. To this purpose, we use an EditText so that the user can edit his sentece and when he clicks on the microphone the app starts reading. Without detailing too much the code because is trivial we focus our attention when a user clicks on the microphone button:

[java]speechButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speech();
}
});[/java]

where

[java] private void speech() {
engine.speak(editText.getText().toString(),
TextToSpeech.QUEUE_FLUSH, null, null);
}[/java]

we simply have to call the speak method to make our app reading the text!

Control Text to Speech Engine parameters

We can have more control over how the engine read the sentence. We can modify for example the pitch and the speech rate.
In the app, for example, we used two seek bars to control the pitch and rate. If we want to set the voice pitch we can use setPitch passing a float value.
On the other hand, if we want to change the speech rate we can use setSpeechRate.
In our app, we read this value from two seek bars and the speech method becomes:

[java] private void speech() {
engine.setPitch((float) pitch);
engine.setSpeechRate((float) speed);
engine.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null, null);
}[/java]

 

At the end of this article, you learned how to develop an Android app that uses Text to Speech in Android. We have developed a Text to Speech Android example app that we can use to read sentences.

    1. John Smith February 26, 2015
    2. Francesco Azzola February 26, 2015
    3. Kalyan Kumar July 17, 2017

    Add Your Comment