Pages

Subscribe:

Monday, February 14, 2011

Android - Text To Speach

Here is a sample program for Text to Speach

Screen



Layout


<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget33"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_x="0px"
android:layout_y="0px"
>
</TextView>
<EditText
android:id="@+id/txtMessage"
android:layout_width="207px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="68px"
android:layout_y="39px"
>
</EditText>
<TextView
android:id="@+id/widget34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:layout_x="20px"
android:layout_y="48px"
>
</TextView>
<Button
android:id="@+id/btnSpeakNow"
android:layout_width="146px"
android:layout_height="wrap_content"
android:text="Speak Now!"
android:layout_x="96px"
android:layout_y="121px"
>
</Button>
</AbsoluteLayout>


Java Code

package com.text2speech;

import java.util.Locale;

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.TextView;

public class Text2Speech extends Activity implements OnInitListener {
public TextToSpeech tts;

TextView txtMessage;
Button btnSpeakNow;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

txtMessage = (TextView) findViewById(R.id.txtMessage);
btnSpeakNow = (Button) findViewById(R.id.btnSpeakNow);

btnSpeakNow.setOnClickListener((OnClickListener) new Speak());
btnSpeakNow.setEnabled(true);

Intent checkIntent = new Intent();

checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, 0);
tts = new TextToSpeech(this, this);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
btnSpeakNow.setEnabled(true);
}

@Override
public void onInit(int status) {

}

class Speak implements Button.OnClickListener {

@Override
public void onClick(View arg0) {

String speech1 = txtMessage.getText().toString();
tts.setLanguage(Locale.US);
tts.speak(speech1, TextToSpeech.QUEUE_FLUSH, null);
String speech2 = "Neenga Yeppadi Irukkenga?";
String speech3 = "Enrichware Yeppadi Irkku?";
tts.speak(speech2, TextToSpeech.QUEUE_ADD, null);
tts.speak(speech3, TextToSpeech.QUEUE_ADD, null);
}
}

}



0 comments:

Post a Comment