INTRODUCING ANDROID TEXT TO SPEECH


Android 1.6 (SDK API level 4) introduced the text to speech (TTS) engine. You can use this API to
produce speech synthesis from within your applications, allowing them to ‘‘talk’’ to your users.
Due to storage space constraints on some Android devices, the language packs are not always prein-
stalled on each device. Before using the TTS engine, it’s good practice to confirm the language packs
are installed.
Start a new Activity for a result using the ACTION_CHECK_TTS_DATA action from the TextToSpeech.
Engine class to check for the TTS libraries.
Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(intent, TTS_DATA_CHECK);
The onActivityResult handler will receive CHECK_VOICE_DATA_PASS if the voice data has been installed
successfully.
If the voice data is not currently available, start a new Activity using the ACTION_INSTALL_TTS_DATA
action from the TTS Engine class to initiate its installation.
Once you’ve confirmed the voice data is available, you need to create and initialize a new TextToSpeech
instance. Note that you cannot use the new Text To Speech object until initialization is complete. Pass
an OnInitListener into the constructor (as shown in Listing 15-4) that will be fired when the TTS
engine has been initialized.


boolean ttsIsInit = false;
TextToSpeech tts = null;
tts = new TextToSpeech(this, new OnInitListener() {
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
ttsIsInit = true;
// TODO Speak!
}
}
});
When Text To Speech has been initialized you can use the speak method to synthesize voice using the
default device audio output.
tts.speak("Hello, Android", TextToSpeech.QUEUE_ADD, null);
The speak method lets you specify a parameter to either add the new voice output to the existing queue,
or flush the queue and start speaking straight away.
You can affect the way the voice output sounds using the setPitch and setSpeechRate methods. Each
accepts a float parameter that modifies the pitch and speed, respectively, of the voice output.
More importantly, you can change the pronunciation of your voice output using the setLanguage
method. This method takes a Locale value to specify the country and language of the text being spoken.
This will affect the way the text is spoken to ensure the correct language and pronunciation models are

When Text To Speech has been initialized you can use the speak method to synthesize voice using the
default device audio output.
tts.speak("Hello, Android", TextToSpeech.QUEUE_ADD, null);
The speak method lets you specify a parameter to either add the new voice output to the existing queue,
or flush the queue and start speaking straight away.
You can affect the way the voice output sounds using the setPitch and setSpeechRate methods. Each
accepts a float parameter that modifies the pitch and speed, respectively, of the voice output.
More importantly, you can change the pronunciation of your voice output using the setLanguage
method. This method takes a Locale value to specify the country and language of the text being spoken.
This will affect the way the text is spoken to ensure the correct language and pronunciation models are
used.
When you have finished speaking, use stop to halt voice output and shutdown to free the TTS resources.

private static int TTS_DATA_CHECK = 1;
private TextToSpeech tts = null;
private boolean ttsIsInit = false;
private void initTextToSpeech() {
Intent intent = new Intent(Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(intent, TTS_DATA_CHECK);
}
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (requestCode == TTS_DATA_CHECK) {
if (resultCode == Engine.CHECK_VOICE_DATA_PASS) {
tts = new TextToSpeech(this, new OnInitListener() {
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
ttsIsInit = true;
if (tts.isLanguageAvailable(Locale.UK) >= 0)
tts.setLanguage(Locale.UK);
tts.setPitch(0.8f);
tts.setSpeechRate(1.1f);
speak();
}
}
});
} else {
Intent installVoice = new Intent(Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
private void speak() {
if (tts != null && ttsIsInit) {
tts.speak("Hello, Android", TextToSpeech.QUEUE_ADD, null);
}
}

@Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}



Comments

Post a Comment

Popular posts from this blog

Bluetooth Data Transfer Example

How to Create & Extract tar.gz and tar.bz2 Files in Linux