In this tutorial we are going to implement text to speech in the android application. One of the special feature of android is speech accessibility to speak different language.
User needs to enter a text and press the button to hear it.
File : res/layout/activity_text_to_speech.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Text_to_speechActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Enter the text to speech"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:ems="12" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="67dp"
android:src="@drawable/speak" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:text="Speak"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
</RelativeLayout>
File : Text_to_speechActivity.java
package com.mindmedia.text_to_speech;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class Text_to_speechActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech speech;
private ImageButton speak;
private EditText comment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_to_speech);
speech = new TextToSpeech(this, this);
speak = (ImageButton) findViewById(R.id.imageButton1);
comment = (EditText) findViewById(R.id.editText1);
// button on click event
speak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speak();
}
});
}
@Override
public void onDestroy() {
// Don’t forget to shutdown tts!
if (speech != null) {
speech.stop();
speech.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status)
{
if (status == TextToSpeech.SUCCESS)
{
int result = speech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
}
else
{
speak.setEnabled(true);
speak();
}
}
else
{
Log.e("TTS", "Initilization Failed!");
}
}
private void speak() {
String text = comment.getText().toString();
speech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
Note : import android.speech.tts.TextToSpeech is used for speak capability
Output :