Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

Android – Text to speech simple example

Posted on 21/10/2010
android   In this article I will show the most simple way to add text to speech abilities to your Android application.

public class Text2SpeechTest extends Activity implements OnInitListener {

	TextToSpeech talker;

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

    public void say(String text2say){
    	talker.speak(text2say, TextToSpeech.QUEUE_FLUSH, null);
    }

	@Override
	public void onInit(int status) {
		say("Hello World");

	}

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

		super.onDestroy();
	}
}

  • Actually that’s the whole code.
    You just need to create a TextToSpeech object.
  • Notice the constructor takes 2 parameters, the first is the Context Activity extends from. The second is an OnInitListener which you will need to implement.
  • Ofcourese there are a lot more options but this is the gist of it. You can read more in the Android developer website.

Download code example

7 thoughts on “Android – Text to speech simple example”

  1. Technotalkative says:
    13/06/2011 at 12:15

    Here is a nice example given by android itself: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/TextToSpeechActivity.html

    Reply
    1. ram says:
      21/11/2012 at 20:42

      Its good example

      Reply
  2. Victoriano Román says:
    20/03/2013 at 16:01

    Muy bueno. Funciona en el emulador y en mi tableta.
    Muchas gracias.

    Very good. It works in the emulator and on my tablet.
    Thank you very much.

    Reply
  3. Meghna says:
    09/08/2013 at 22:14

    Cool Man!! Thanks a lot, Even this
    http://www.compiletimeerror.com/2013/08/text-to-speech-in-android.html might help you out, have a look..

    Reply
  4. Amruta Ahire says:
    31/10/2013 at 10:09

    Hey..It really works,But it does not works on Nexus 4,Samsung Galaxy Y

    Reply
  5. Txema Garcia says:
    05/11/2013 at 20:53

    anyone knows how to add a language?, such as Spanish

    ——————-

    Alguien sabe como añadir un idioma que no esta soportado de serie, como por ejemplo Español.

    Gracias. un saludo.

    Reply
  6. Kaifi says:
    20/10/2014 at 08:59

    Can anyone tell that how to use google to speech tts and espeak app both in my application. i want to use both, randomly.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
1d96f52e7159fe09c7a3dd2a9816d166-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme