How To Use Android’s Text To Speech Capabilities

 

This is a sample activity which shows How to use Android’s Text to Speech capabilities. Text to Speech was introduced in Android 1.6, so when you create your new Android project make sure your minimum required SDK is set to Android 1.6 (or API level 4).
Last topic published on this forum was How to send an Email.

Underlying Algorithm:

Basic description of algorithm in step by step form:
1.) Create a Project MyTextToSpeech.
2.) Put the following code snippet in res/layout/main.xml:

 

<!--?xml version="1.0" encoding="utf-8"?-->
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

android:text=""
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<button> android:text="Speak to me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

 

 

3.) You just need to import the following packages and create a TextToSpeech object.

 

import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;

 

 

4.) The second is an OnInitListener which you will need to implement.
5.) Run the Application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. MyTextToSpeech. Enter following information:
Project name: MyTextToSpeech
Build Target: Android APIs 2.1
Application name: MyTextToSpeech
Package name: com.app.MyTextToSpeech
Create Activity: MyTextToSpeech

On Clicking Finish MyTextToSpeech code structure is generated with the necessary Android Packages being imported along with MyTextToSpeech.java. MyTextToSpeech class will look like following:

 

package com.app.MyTextToSpeech;

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 MyTextToSpeech extends Activity implements OnInitListener{
/** Called when the activity is first created. */
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(MyTextToSpeech.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(MyTextToSpeech.this, "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
}
else if (status == TextToSpeech.ERROR) {
Toast.makeText(MyTextToSpeech.this, "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
}
}
}

 

 

Output –The final output:

Leave a Comment:

15 comments
Anuradha says August 1, 2011

This is very nice app. Excellent!!. I am looking for BTAudio streaming app.Do you have any example.

Reply
shanisk says September 20, 2011

[…] How to use Android’s Text to Speech capabilities Posted by Sushant on July 29, 2011 This is a sample activity which shows How to use Android’s Text to Speech capabilities. Text to Speech was introduced in Android 1.6, so when you create your new Android project make sure your minimum required SDK is set to Android 1.6 (or API level 4). Last topic published on this forum was How to… Filed in: Android Programming Tutorials 1 Page 1 of 712345»…Last » 0.000000 0.000000 Share this:TwitterFacebookLike this:LikeBe the first to like this post. Categories: Android Tutorial And Help […]

Reply
sourabh says September 28, 2011

Does it require internet connection for conversion?

Reply
software says October 20, 2011

I used to be suggested this blog via my cousin. I’m not sure whether this submit is written by way of him as no one else recognize such distinctive about my problem. You are incredible! Thank you!

Reply
download ebook free says November 4, 2011

Useful information. Lucky me I found your web site by chance, and I am shocked why this twist of fate didn’t took place earlier! I bookmarked it.

Reply
Sasquatch Hosting offersgreat hosting says November 7, 2011

You are in point of fact a just right webmaster. The website loading pace is amazing. It sort of feels that you are doing any unique trick. Furthermore, The contents are masterwork. you’ve done a wonderful process in this matter!

Reply
progi na telefon says January 3, 2012

Hi my family member! I want to say that this article is awesome, great written and include approximately all significant infos. I would like to see extra posts like this .

Reply
waqas says May 10, 2012

I have installed this application on my phone but it produces following error:
“The application
My TextToSpeach(process com.app.MyTextToSpeach) has stoped unexpectedly.Please try again.”
Please help me . . .

Reply
waqas says May 10, 2012

I have installed this application on my phone but it produces following error:
"The application
My TextToSpeach(process com.app.MyTextToSpeach) has stoped unexpectedly.Please try again."
Please help me . . .

Reply
waqas says May 10, 2012

I have installed this application on my phone but it produces following error:
\&quot;The application
My TextToSpeach(process com.app.MyTextToSpeach) has stoped unexpectedly.Please try again.\&quot;
Please help me . . .

Reply
waqas says May 10, 2012

I have installed this application on my phone but it produces following error:
“The application
My TextToSpeach(process com.app.MyTextToSpeach) has stoped unexpectedly.Please try again”
Please help me . . .

Reply
Mobile Designer says June 11, 2012

I appreciate, result in I found just what I was having a look for. You have ended my four day long hunt! God Bless you man. Have a nice day. Bye

Reply
dimpal says July 5, 2012

plz give this apps full code and report argent

Reply
dolly1234 says July 5, 2012

How to use Android’s Text to Speech capabilities
plz give the full coding and report for this apps

Reply
the notion of psychology says July 26, 2013

Thanks , I have recently been searching for info about this subject for ages and yours is the best I have came upon so far. However, what in regards to the conclusion? Are you certain about the source?|What i don’t understood is in truth how you’re now not really a lot more well-appreciated than you might be right now. You’re very intelligent.

Reply
Add Your Reply