Android Text-To-Speech Application

Posted: February 8, 2012 in Java Posts, Random Posts
Tags: , , , , ,

One of the many features that Android provides out of the box is the one of “speech synthesis”. This is also known as “Text-To-Speech” (TTS) and is mainly the capability of the device to “speak” text of different languages. This feature was introduced in version 1.6 of the Android platform and you can find an introductory article at the official Android-Developers blog.

In this tutorial I am going to show you how to quickly introduce TTS capabilities into your application.

Let’s get started by creating a new Eclipse project under the name “AndroidTextToSpeechProject” as shown in the following image:

Note that Android 1.6 was used as the build target and that for the minimum SDK version I used the value 4, since this functionality is not provided by the previous versions.

The first step to use the TTS API is to check if it is actually supported by the device. For that purpose there is a special action named ACTION_CHECK_TTS_DATA which is included in the TextToSpeech.Engine. As the Javadoc states, this is used by an Intent to “verify the proper installation and availability of the resource files on the system”. In case the resourses are not available, another action named ACTION_INSTALL_TTS_DATA is used in order to trigger their installation. Note that the SDK’s emulator supports TTS with no configuration needed.

Before using the TTS engine, we have to be sure that it has properly been initialized. In order to get informed on whether this has happened, we can implement an interface called OnInitListener. The method onInit will be invoked when the engine initialization has completed with the accompanying status.

After initialization, we can use the TextToSpeech class to make the device speak. The relevant method is named speak, where the text, the queue mode and some additional parameters can be passed. It is important to describe queue mode. It is the queuing strategy to be used by the TTS engine, i.e. what to do when a new text has been queued to the engine. There are two options:

  • QUEUE_ADD: the new entry is added at the end of the playback queue
  • QUEUE_FLUSH: all entries in the playback queue (media to be played and text to be synthesized) are dropped and replaced by the new entry

All the above are translated to the code provided below:

01 package;
03 import;
04 import android.content.Intent;
05 import android.os.Bundle;
06 import android.speech.tts.TextToSpeech;
07 import android.speech.tts.TextToSpeech.OnInitListener;
08 import android.view.View;
09 import android.view.View.OnClickListener;
10 import android.widget.Button;
11 import android.widget.EditText;
12 import android.widget.Toast;
14 public class TtsActivity extends Activity implements OnInitListener {
16     private int MY_DATA_CHECK_CODE = 0;
18     private TextToSpeech tts;
20     private EditText inputText;
21     private Button speakButton;
23  @Override
24  public void onCreate(Bundle savedInstanceState) {
26   super.onCreate(savedInstanceState);
27   setContentView(R.layout.main);
29   inputText = (EditText) findViewById(;
30   speakButton = (Button) findViewById(;
32   speakButton.setOnClickListener(new OnClickListener() {           
33    @Override
34    public void onClick(View v) {
35        String text = inputText.getText().toString();
36        if (text!=null && text.length()>0) {
37     Toast.makeText(TtsActivity.this, "Saying: " + text, Toast.LENGTH_LONG).show();
38     tts.speak(text, TextToSpeech.QUEUE_ADD, null);
39        }
40    }
41       });
43   Intent checkIntent = new Intent();
44       checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
45       startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
47     }
48 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
49         if (requestCode == MY_DATA_CHECK_CODE) {
50             if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
51                 // success, create the TTS instance
52                 tts = new TextToSpeech(this, this);
53             }
54             else {
55                 // missing data, install it
56                 Intent installIntent = new Intent();
57                 installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
58                 startActivity(installIntent);
59             }
60         }
62     }
64     @Override
65     public void onInit(int status) {       
66         if (status == TextToSpeech.SUCCESS) {
67             Toast.makeText(TtsActivity.this,
68                     "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
69         }
70         else if (status == TextToSpeech.ERROR) {
71             Toast.makeText(TtsActivity.this,
72                     "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
73         }
74     }
76 }


and main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”;
android:orientation=”vertical” >







The engine support is checked in the onActivityResult method. If there is support for TTS, we initialize the engine, else a new Intent is launched in order to trigger the relevant installation. After initialization, we used a Toast in order to notify the user about the operation status. Finally, we use a text field where the text is provided by the user and a button which feeds the engine with the provided text.

In order to run the project, an appropriate emulator device is needed. If you have none available, use the following configuration in the AVD manager (note that “Android 1.6 – API Level 4” or higher is needed):

Provide the text you wish to hear into the text field, hit the button and listen to emulator speak to you!

That’s all folks, nice and simple. Now, make your devices speak! You can download the created Eclipse project here.

  1. seo link building says:

    Outstanding post, I think people should learn a lot from this web site its very user genial . “Choose your pleasures for yourself, and do not let them be imposed upon you.” by Lord Chesterfield.

  2. electronic blackjack games says:

    Regards for all your efforts that you have put in this. very interesting info . “Illegitimacy is something we should talk about in terms of not having it.” by Dan Quayle.

  3. student loan database says:

    Great ñ I should certainly pronounce, impressed with your site. I had no trouble navigating through all the tabs and related information ended up being truly easy to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or something, site theme . a tones way for your client to communicate. Excellent task.

  4. blackjack games online says:

    I think this website has some rattling superb information for everyone. “Philosophy triumphs easily over past evils and future evils but present evils triumph over it.” by La Rochefoucauld.

  5. hybrid car reviews says:

    Keep up the fantastic work , I read few articles on this web site and I think that your web site is real interesting and contains lots of superb info .

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s