First Android App – Text to Speech

5 minute read

So I’ve been itching to get back to enhancing/fixing my android application but because of work I’ve been unable to. Finally today I decided to put some time on this and build a highly requested feature of the application (well, actually only one person requested it and it’s the only comment in my app!) and that was the text to speech feature.  I sat down about and thought about (and wrote down) how this should be implemented.  The first thing I decided was that this would only be implemented for the Flash Card mode of the application.  It didn’t make any sense for the user to have this feature while browsing the questions in the ‘All Civics Questions’ mode (where all questions are implemented in a horizontal list that users can scroll thru to view each question).

Once I decided where this new feature would be implemented I had to decide how this should be setup so that the user can enable/disable this feature. For this I decided to implement an application preferences activity that allows the user to do this.

The user will be able to get to this screen from the home page, flash card mode or the all civics questions activities.  To enable this feature all the user needs to do is to check the box and that’s it!  Now for the fun part, I had to read up on how to setup the Text to Speech component and all I had to do was read up on the wonderfully documented feature in the Android SDK web site.  Here is the link for those who are curious.

To set this up all you have to do is to make sure that the Text to Speech functionality is installed on the phone.  This is something that may not be installed by default on some phones because of the space requirements (it’s not that big of a component … when I downloaded it, I think it was about 4.3 Megabytes).   Anyhow, you first have to check to see if this is installed or not on the system, this is done with the following code:

Intent checkTextToSpeech = new Intent();
checkTextToSpeech.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTextToSpeech, MY_DATA_CHECK_CODE);

After you make this call, you have to check what the status of the check to see if it has it installed or not.  Below is the code to check for this (because we started the activity above with startActivityForResult, we must call the onActivityResult method):

// Object that will be used to speak out the questions/answers in the application
private TextToSpeech textToSpeech;
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == MY_DATA_CHECK_CODE)
    {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
        {
            // success, create the TTS instance
            textToSpeech = new TextToSpeech(this, this);
        }
        else
        {
            // missing text to speech, so we have to install it.
            // This will start up a new activity going to the Android Market
            // to install the Text to Speech component. When the user installs
            // this component, the user will be brought back to the application
            Intent installIntent = new Intent();
            installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }
}

So now that you have the Text to Speech component installed and ready to go, the next thing you have to do is to setup the TextToSpeech object to use the appropriate text to speak.  This is done by implementing the OnInitListener interface:

...
import android.preference.PreferenceManager;
...
/**
 * Note: we are implementing the
 * PreferenceManager.OnInitListener interface so that on the
 * onInit method we setup the TextToSpeech object
 */
public class QuestionActivity extends Activity implements OnClickListener, OnInitListener
{
    // ...
    @Override
    public void onInit(int status)
    {
        // The textToSpeech object has already been initialized, now we are
        // setting the language (in my case this will be set to English)
        // then we call the speak method with the text and some other parameters
        // that will be used. Note: I am using a custom method to strip out HTML code
        // from the question so that the speech engine doesn't accidentally speak out
        // any html code.
        textToSpeech.setLanguage(Locale.ENGLISH);
        textToSpeech.speak(Util.stripOutHtml(questionTextString), TextToSpeech.QUEUE_FLUSH, null);
    }
    // ...
}

After calling the code above that’s pretty much all I had to do to have the questions and answers be spoken by the text to speech engine!  I had to do some minor changes to the content to make sure that there were appropriate pauses in the answers that had multiple lines of answers (I had to add periods to end each line).  After I was done, I did some testing of all 100 questions and I was done!  Then with the magic of git, I merged the ‘text_to_speech’ branch to the master brand and all my work was now done!  Simple and beautiful code, this was truly a joy of a feature to implement!

There are a few other features that I’ll need to work on for the application.  Once I am done with those features I’ll push out version 1.20 of the application. On an additional note, I noticed today that the application hit the 300 download mark! This was a reason why I wanted to work on the application today, I really did get a great boost of joy from the fact that 300 people had downloaded the application and tried it out!  Now, the only bad thing about this is the fact that of those 300 downloads there are only 200 active installations.  I hope that this feature and others I’ll be implementing will bring the number back up again!

Leave a comment