Home > Uncategorized > TextToSpeach Example

TextToSpeach Example

One of the many features that Android provides out of the box is the one of “speech synthesis”. This is also known as “Text-To-Speech” (TTS) and is mainly the capability of the device to “speak” text of different languages. This feature was introduced in version 1.6 of the Android platform and you can find an introductory article at the official Android-Developers blog.

main.xml

TextToSpeachActivity.java

package com.sst.tts;

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

public class TextToSpeachActivity extends Activity implements OnInitListener {
private EditText text;
private Button button;
TextToSpeech _tts;
boolean _ttsActive = false;
private Object powerLock;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final Button button = (Button) findViewById(R.id.button1);
text = (EditText) findViewById(R.id.edittext);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click

_ttsActive = true;
_tts.speak(text.getText().toString(), TextToSpeech.QUEUE_FLUSH,
null);

}
});

// text = (EditText) findViewById(R.id.editText1);
}

@Override
public void onPause() {

try {
// Stop talking when we lose focus
if (_tts != null) {
_tts.stop();
_ttsActive = false;
}
} catch (Exception e) {
}
}

@Override
public void onResume() {
super.onResume();

// Create our text to speech object.
_tts = new TextToSpeech(getApplicationContext(), this);
}

@Override
public void onDestroy() {
super.onDestroy();
try {
// We’re closing down so kill it with fire.
if (_tts != null) {
_tts.shutdown();
_tts = null;
}

// powerLock.release();
} catch (Exception e) {
}
}

public void onInit(int status) {
// If the TTS init is successful set a flag to say we
// can be used; say hello
if (status == TextToSpeech.SUCCESS) {

_ttsActive = true;
_tts.speak(
“Hello Deepthy Haridas How are you How is going your job”,
TextToSpeech.QUEUE_FLUSH, null);
}
}
}

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: