Click here to Skip to main content
Click here to Skip to main content

Using Text to Speech (TTS) engine in an Android application

, 19 Sep 2013
Rate this:
Please Sign up or sign in to vote.
In this article I will discuss how to implement text to speech (TTS) engine in an Android application.

Introduction

We often come across scenarios where some information or data has to be presented to a user which we generally do so by displaying some text or images on the users mobile device. But wouldn't it be nice if the information is 'read' out to the user making the overall experience a bit more exciting.

In this article we will write our first application with speech capabilities.

Background

Android 1.6 (SDK API level 4) introduced the text to speech (TTS) engine. We can use this API to produce speech synthesis from within our applications, allowing them to speak or talk to the users.

Getting Started

Step 1

Due to storage space constraints on some Android devices, the language packs are not always preinstalled on the devices. So, before using the TTS engine it will be nice to confirm that the language packs are installed on the device.

Step 2

If the language packs or the resource files required for TTS to be operational are not currently available on the device, we will have to request the TTS engine to initiate the installation.

Code to implement 'Step 1' and 'Step 2' -

Import the package-

import android.speech.tts.TextToSpeech.Engine;
private static int TTS_DATA_CHECK = 1;

private void confirmTTSData() {
    Intent intent = new Intent(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, we will start a new Activity using the ACTION_INSTALL_TTS_DATA action from the TTS Engine class to initiate its installation.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TTS_DATA_CHECK) {
        if (resultCode == Engine.CHECK_VOICE_DATA_PASS) {
            //Voice data exists        
        }
        else {
            Intent installIntent = new Intent(Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }
}

Making the Application 'Speak'

Once we've confirmed that the voice data is available on the device, we need to create and initialize an object of TextToSpeech class.

The new TextToSpeech object cannot be used until the initialization is complete. In order to determine whether the TextToSpeech engine has been initialized we will implement the onInit(int status) method of the TextToSpeech.OnInitListener interface that is fired when the TTS engine has been initialized.

TextToSpeech tts = new TextToSpeech(this, new OnInitListener() {
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            //Speak
        }
        else {
            //Handle initialization error here
        }
    }
});

When TextToSpeech engine has been initialized we can use the speak method to synthesize voice using the default device audio output.

tts.speak("Hello!", 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.

Method Detail

public int speak(String text, int queueMode, HashMap<string,string> params)

Details about the parameters that the method accepts can be found here.

When we have finished speaking, we have to call stop method to halt the voice output and shutdown method to free the TTS resources.

Points of Interest

The TextToSpeech class has a few useful methods that allow us to manipulate the voice output, which if used correctly will enhance the user's experience with your application.

  • setPitch - Sets the speech pitch for the TextToSpeech engine.
  • setSpeechRate - Sets the speech rate.
  • setLanguage - Sets the text-to-speech language.

The setLanguage method takes a Locale value to specify the country and language of the text being spoken. This affects the way the text is spoken to ensure the correct language and pronunciation models are used. For instance 'Z' is pronounced as 'Zee' in US and 'Zed' in UK

A quick and easy way to find the Locale of the user is:

import java.util.Locale;
Locale locale = this.getResources().getConfiguration().locale;

The complete TextToSpeech class reference can be found here.

Sample Code to set Pitch, Speech Rate and Language

private void speakInUKLocale() {
    //Confirm whether the desired Locale is available
    if (tts.isLanguageAvailable(Locale.UK) >= 0) 
        tts.setLanguage(Locale.UK);
        
    //Set Pitch, 1.0 is Normal    
    //Lower values lower the tone of the synthesized voice, greater values increase it.
    tts.setPitch(0.8f);

    //Set Speech Rate, 1.0 is Normal
    //Lower values slow down the speech, greater values accelerate it 
    tts.setSpeechRate(1.1f);
            
    tts.speak("Hello!", TextToSpeech.QUEUE_ADD, null);        
}

Demo Project

I have created a simple demo project to help you get started with TTS on Android.

Hope this small article helps someone out there.

Happy coding!

History

  • September 19, 2013: First version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

gupta.avinash
Architect
India India
No Biography provided
Follow on   Twitter

Comments and Discussions

 
QuestionIf languages not supported Pinmemberعمر القادري28-Jan-14 22:29 
AnswerRe: If languages not supported Pinmembergupta.avinash29-Jan-14 0:03 
GeneralMy vote of 5 PinmemberThatsAlok9-Oct-13 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 19 Sep 2013
Article Copyright 2013 by gupta.avinash
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid