Sunday, February 08, 2009 

Adding Text To Speech to the Translate Application in Android

I recently bought a T-Mobile G1 and I have been looking at learning how to write applications for it. I thought I would put up a quick tutorial on how to add text-to-speech capabilities to the translation application. The translation application was written by Cédric Beust.

Preliminaries:

Download and install the Android Software Development kit from here:

http://code.google.com/android/intro/installing.html

Information about using the Text-to-speech API can be found here:

http://eyes-free.googlecode.com/svn/trunk/documentation/tutorial/tutorial.html

Information about the Translation application can be found here:

http://beust.com/weblog/archives/000500.html

You can find information and source-code for applications that people from Google have written for Android here: http://code.google.com/p/apps-for-android/

1. Download the source code for the the Translate application.

You can use any svn client that you want to download the source code for the Translator application. I used tortoisecvs. Create a directory somewhere, and download the source code from http://apps-for-android.googlecode.com/svn/trunk/Translate


Photobucket


2. Create a new project in Eclipse

Select File->new->Android Project. Then select the option to "Create a New Project from existing source". Under location, click browse and select the directory where you downloaded the Translator files to. Give the project a name that is meaningful to you. I chose "TranslateActivity2". The other fields are automatically filled in for you.


Photobucket


3. Download the TTS library and add it as an external library

Download the tts library stub from here:

http://eyes-free.googlecode.com/svn/trunk/commonlibs/TTS_library_stub.jar

Go to Project > Properties > Java Build Path > Libraries and click on "Add External JARs..." Then add in the TTS_library_stub.jar file.



Photobucket


4. Edit the source code to use the TTS library

In the TranslateActivity.java file make the following changes:

In the imports section, add






import com.google.tts.TTS;

After the line "public class TranslateActivity extends Activity implements OnClickListener {" add a line that says:





private TTS myTts;



In the "onCreate(Bundle icicle)" method add:






myTts = new TTS(this, ttsInitListener, true);

After the oncreate method add:





private TTS.InitListener ttsInitListener = new TTS.InitListener() {

public void onInit(int version) {


}

};


In the "doTranslate" method, after "setOutputText(result);" add:





myTts.speak(result, 0, null);

In the "setNewLanguage" method, add:





if(from)
{

if(language.getShortName().contains("zh"))

myTts.setLanguage("zh");

else

myTts.setLanguage(language.getShortName());

}



Conclusion

In this quick tutorial, I have shown you how to add speech to a real application using the text to speech library. I hope you have found it useful. Also, I hope they will include text to speech in a future version of the Translate application.