Byron Kiourtzoglou

About Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Android Text-To-Speech Application

In this tutorial we shall show you how to use the TextToSpeech class to convert Text-to-Speech in Android. The idea is straightforward. We want to type text in an text area and then press button to transform it to speech. You can actually listen to it when you launch the built in application of Android.
 
 
 
 
 
 
 

So the basic steps you have to take in order to convert Text to Speech in Anroid you have to:

  • Create an Activity that extends OnInitListner
  • Specify and create a TextToSpeech instance
  • Create an EditText component for the User to type his text
  • Create a Button that when clicked the text is converted to speech and played through the speakers
  • Create a new Intent and use setAction to set up the intent with the TextToSpeech engine
package com.javacodegeeks.android.tts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TtsActivity extends Activity implements OnInitListener {

    private int MY_DATA_CHECK_CODE = 0;

    private TextToSpeech tts;

    private EditText inputText;
    private Button speakButton;

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  inputText = (EditText) findViewById(R.id.input_text);
  speakButton = (Button) findViewById(R.id.speak_button);

  speakButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

 String text = inputText.getText().toString();

 if (text!=null && text.length()>0) {
    Toast.makeText(TtsActivity.this, "Saying: " + text, Toast.LENGTH_LONG).show();
    tts.speak(text, TextToSpeech.QUEUE_ADD, null);

 }
   }

});

  Intent checkIntent = new Intent();

checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);

startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

    }
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

    tts = new TextToSpeech(this, this);

} 

else {

    // missing data, install it

    Intent installIntent = new Intent();

    installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);

    startActivity(installIntent);

}

  }

    }

    @Override
    public void onInit(int status) {

  if (status == TextToSpeech.SUCCESS) {

Toast.makeText(TtsActivity.this, 

  "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();

  }

  else if (status == TextToSpeech.ERROR) {

Toast.makeText(TtsActivity.this, 

  "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();

  }
    }

}

 
This was an Android tutorial an how to convert Text to Speech.

Related Article:

Related Whitepaper:

Beginning Android Application Development (Free Sample Chapter)

Create must-have applications for the latest Android OS!

The Android OS is a popular and flexible platform for many of today's most in-demand mobile devices. This full-color guide offers you a hands-on introduction to creating Android applications for the latest mobile devices. Veteran author Wei Meng Lee accompanies each lesson with real-world examples to drive home the content he covers. Beginning with an overview of core Android features and tools, he moves at a steady pace while teaching everything you need to know to successfully develop your own Android applications.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.