Pages

Thursday, May 19, 2011

Hello Worlds Part 4 – Android Text to Speech

This is part 4 of a multi-part series that looks at some basic Android I/O using simple Hello World programs. At this point I’ll assume you know how to make a new Android Project in Eclipse as that was covered in Part 1.
In this part we are going to look at using Android’s Text to Speech capabilities to get our Android device (or emulator) to actually say “Hello World” to the user.  Text to Speech was introduced in Android 1.6, so when you create your new Android project make sure your minimum required SDK is set to Android 1.6 (or API level 4).  Here is the code we are going to use to make our Android device speak to us.
package com.learnandroid.helloworld;
 
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
 
public class Hello4 extends Activity implements OnInitListener
{
 
 private TextToSpeech tts;
 static final int TTS_CHECK_CODE = 0;
 
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        tts = new TextToSpeech(this, this);
    }
 
 @Override
 public void onInit(int initStatus) {
  if (initStatus == TextToSpeech.SUCCESS)
  {
   tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);
  }
 }
}
The first thing you should notice here is the line:
public class Hello4 extends Activity implements OnInitListener
This is different from what we have seen before because it has the extra part at the end “implements OnInitListener”. If you are familiar with Java you know that implements the OnInitListener interface. This will allow our activity to respond to the event triggered when the TextToSpeech engine finishes initializing.  We will use the onInit method for this purpose later in our code.
Inside the class I’ve defined a class variable:
private TextToSpeech tts;
so it will be available to both methods in our activity. Then in the onCreate method I initialize the variable using the TextToSpeech class.
tts = new TextToSpeech(this, this);
If you get an error on this line make sure you have the import android.speech.tts.TextToSpeech; statement at the top. in Eclipse you can use Ctrl + Shift + O to automatically update your import statements.
The constructor for the TextToSpeech class takes two parameters.  The first specifies the the context in which we’re running, and the second is the OnInitListener that should be called.  Since our Hello4 class inherits from Activity we can use it for the context, and since it implements OnInitListener we can use it for the OnInitListener.  Thus I simply provided the keyword “this” for both parameters.
Since we specified our class as the onInitListener to use, the class method onInit will automatically be called when the TextToSpeech class is initialized.
public void onInit(int initStatus) {
  if (initStatus == TextToSpeech.SUCCESS)
  {
   tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);
  }
 }
This method is passed a integer indicating the status after initialization. We will compare this status code to the constant provided by the TextToSpeech class to indicate successful initialization (TextToSpeech.SUCCESS). If Initialization was successful we will call the speak method to actually have your android device speak “Hello World”.
tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);
This method takes three parameters.  The first is the text that should be spoken.  The second is the mode to use for speech.  The mode used for speech can allow you to either speak the text immediately (using QUEUE_FLUSH as we did here) or queue up multiple strings to be read. Since this is intended to be a very simple example we won’t cover this functionality here.  The final parameter allows you to pass a list of additional parameters.  This is also outside the scope of this example.
This is the end of our tutorial on Text To Speech.  Thanks for reading and be sure to check back; there is one more Hello World example forthcoming.

No comments:

Post a Comment

Popular Posts