Text to speech in ANDROID, A Simple Example.

By | April 9, 2011

Hello everyone……..

In this simple example I will show you how to use Text To Speech in ANDROID. This is one of the unique features in ANDROID.
Unlike iPhone , ANDROID has a built in TTS Library which supports many languages. But iPhone doesn’t have this feature. They have to depend on third-party software for implementing this.

Now go on create a fresh project and copy this following code to it.

package com.TTS;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;

public class TTS extends Activity implements TextToSpeech.OnInitListener {

	  TextToSpeech tts;
	  @Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);
	    tts = new TextToSpeech(this,this);
	  }

	  public void onInit(int status) {
	    Locale loc = new Locale("eng", "","");
	    if(tts.isLanguageAvailable(loc) >= TextToSpeech.LANG_AVAILABLE){
	      tts.setLanguage(loc);
	    }
	    tts.speak("Text to speech in ANDROID from coderzheaven, Hope you like it.", TextToSpeech.QUEUE_FLUSH, null);
	  }

	  @Override
	  protected void onDestroy() {
	    super.onDestroy();
	    tts.shutdown();
	  }
}

After running this application you will hear the sound that is given as text in this line.

tts.speak(“Text to speech in ANDROID from coderzheaven, Hope you like it.”, TextToSpeech.QUEUE_FLUSH, null);

Please leave your comments if this post was useful.

3 thoughts on “Text to speech in ANDROID, A Simple Example.

  1. Malik

    @tabletki na odchudzanie-> thank you so much… We are working more on Android, iPhone and other related stuffs… we will update with other useful resources from these… Keep in touch… and once again thank you for your support… 🙂

    Reply
  2. varsha

    its nice.
    i need to display that text in emulator.can any1 help me

    Reply

Leave a Reply

Your email address will not be published.