Android Core

Android Text to Speech (TTS)

This post describes how to use text to speech (TTS) in Android. It isn’t so common to find a post explaining how to use it and it is a part of Android not much covered. In my opinion, TTS is very interesting because it can add some nice features to an app. 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.

In this post, i want to cover how to implement TTS in Android and how we can control some interesting aspects of speech engine. We want to code an app that has a EditText widget so that we write the words that have to be read and some controls to modify the speech engine.

android_text_to_speech

Text to speech Engine

The first thing we have to do to use the TTS in our app is initialise the engine. The class that controls the engine is called TextToSpeech,

engine = new TextToSpeech(this, this);

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 we have:

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

We use the onInit as callback method, and when the engine is ready, we set the default language that the engine used to read our sentence.

Read the words

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

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

where

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

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 on 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 become:

private void speech() {
        engine.setPitch((float) pitch);
        engine.setSpeechRate((float) speed);
        engine.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null, null);
    }
  • Source code available @ github
Reference: Android Text to Speech (TTS) from our JCG partner Francesco Azzola at the Surviving w/ Android blog.

Francesco Azzola

He's a senior software engineer with more than 15 yrs old experience in JEE architecture. He's SCEA certified (Sun Certified Enterprise Architect), SCWCD, SCJP. He is an android enthusiast and he has worked for long time in the mobile development field.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Syed Asim Ashiq
Syed Asim Ashiq
9 years ago

Sir, I tried this code but got this Error when called speak function.
Caused by: java.lang.NoSuchMethodError: android.speech.tts.TextToSpeech.speak
what’s wrong with this? is it depreciated ?

ERROR: Call requires API level 21 Current min is 14.
Please Suggest the solution.

Irfan Khoirul Muhlishin

Set your minimum SDK to 21
or just remove the last parameter (null)
it should be “engine.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);”
but it was deprecated on API level 21 and above

Robert
Robert
8 years ago

Hey Nice Tutorial Man ! Thanks op. also i want to share Coding101 Blog,which is also awesome like Yours !

Back to top button