This is an old site. Please visit the new site!

Kaloer.com

RSS Feed

Make your application speak

July 21, 2009 by Mads Kalør

Filed under: Android, Programming

Google has developed a text-to-speech library for Android. This project is primarily made for blind people, so they can use an Android powered phone. However, the text-to-speech library can be useful in other applications - imaging a talking translate application. You don't need to know how to pronounce the words because the application will say them for you. If the application could recognize speech and convert it to text, this can work as an digital interpreter.

I will try to give an example of a very speaking application.
The TTS-library is available on the Android market, but if you're using the emulator you can download it form the eyes-free project page, where from the library can be downloaded too. If the user of the application don't have the TTS library installed, a dialog will pop up when the application starts.

TTS

The TTS service is initialized here:

TTS myTTS = new TTS(Context context, TTS.InitListener callback, boolean displayInstallMessage)

The TTS.InitListener is called when the TTS service is installed and ready for use. The TTS service cannot speak when this listener not has been called.

TTS.InitListener myInitListener = new TTS.InitListener() {

	public void onInit(int arg0) {
		//The TTS service is now ready
	}
};

To speak, the TTS.speak method is used:

speak(String text, int queueMode, java.lang.String[] params)

The String text is the text to speak. If the queueMode is set to 1, only one speech is done at time. The new speeches will therefore be called when the speech called before has finished. Otherwise, the new speech will begin and the old stop, when the TTS.speak method is called. The String[] params is the params to use. These do not have any effect with eSpeak - only pre-recorded voices.

I have made an example, where "What's up, dude" is said when a button is pressed.

package com.kaloer.ttsexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.google.tts.TTS;
import com.google.tts.TTS.InitListener;

public class TTSExample extends Activity implements InitListener {

	private boolean ttsReady = false;
	private TTS myTTS;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// Initializes the TTS service
		myTTS = new TTS(this, this, true);
		Button speakBtn = (Button) findViewById(R.id.speakBtn);
		speakBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// What's up?
				speak("What's up, dude?");
			}
		});
	}

	private void speak(String textToSpeek) {
		// Checks if the TTS service is ready and installed
		if (ttsReady) {
			// This param does only work on pre-recorded voice and has no
			// effect on this speak.
			String[] params = { "VOICE_ROBOT" };
			// Speaks the text
			myTTS.speak(textToSpeek, 0, params);
		} else {
			// Notifys the user if the TTS service is not ready or installed
			Toast.makeText(this, "TTS is not ready or  installed",
					Toast.LENGTH_LONG).show();
		}
	}

	public void onInit(int arg0) {
		// this method is called when the TTS service is ready.
		// if the service not is installed, this will not be called.
		// we can set a the boolean ttsReady to true, so we can be
		// sure that the service is ready for use.
		ttsReady = true;
	}
}

We can also set the language for the speech. The TTS.setLanguage(String language) can set the language (the accent,) and is set as a IETF language tag. Eg is "en" equal to the English language and "fr" is equal to French. Not every language is usable in the TTS library.
We can modify our code to say "Ce qui est en place, mec?" (I'm sorry if it's not real French, but it's made by Google Translate,) and, hopefully, with a french accent (I'm sorry to say this, but after I've heard it, it doesn't sound that much french.)

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// Initializes the TTS service
		myTTS = new TTS(this, this, true);
		//Sets the language to French
		myTTS.setLanguage("fr");
		Button speakBtn = (Button) findViewById(R.id.speakBtn);
		speakBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// What's up?
				speak("Ce qui est en place, mec?");
			}
		});
	}
 

You can find the documentation of TTS here

No responses to “Make your application speak”

Leave a comment