In this tutorial we will learn how to convert text into speech using Android programming.
Here we are going to develop an application whose layout consists an EditText which will accept the text as input and Image button which will perform some event on the click i.e. whenever you provide the text as input and press on the image button, it will convert the text into speech.
- To convert text into speech follow the steps given below :
- First of all download the ADT- Bundle from developer.android.com and extract it .
- For downloading and installation of eclipse you can follow the following link Installing Android SDK
- Now open up eclipse and click on File -> create new Android Application Projects
- Provide a project name for example ” TextSpeech ” and set the Minimum Required and Target SDK for the project as ” 19 “.
- Click on Next and select Blank from the appeared dialog box
- Now Click on Next and it will show you the dialog box which contains your java file i.e MainActivity and xml layout name : activity_main
- Click on Finish and your MainActivity.java and activity_main.xml files will be generated.
- Now just move to activity_main.xml file i.e in its Graphical Layout and drag and drop TextView on the layout as shown in the image and for example set the text as Text To Speech
- Similarly drag an Plain text i.e an EditText and Image button .
- Hence, your layout will look like as shown below :
- Now go to the activity_main.xml file the tab besides the graphical layout and change the id’s of the EditText, TextView, ImageButton, etc for simplicity of understanding.
- Hence the complete code for your activity_main.xml file is given below :
- Here is the ImageView of the above code :
- Now just move to MainActivity.java file and extend your class with Activity implements TextToSpeech.OnInitListener
- Hence, the complete code is given below which is self explanatory :
- Here is the image view of the above code :
- Now run your project and you will get your application running on the emulator as shown below :
- Just type some text in the given EditText and click on the ImageButton i.e. image of a Boy in this example.
- Click on the image button and it will transform your text into speech.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ffffff"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Text To Speech" android:padding="15dip" android:textColor="#800000" android:textSize="26dip" android:gravity="center" android:textStyle="bold|italic"/> <EditText android:id="@+id/txtText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Enter some text to speak" android:layout_marginTop="20dip" android:layout_margin="10dip"/> <ImageButton android:id="@+id/btnSpeak" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="50dip" android:layout_marginBottom="10dip" android:src="@drawable/bol" android:background="#FFFFFF" android:text="Speak Out" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CLICK ME" android:gravity="center" android:layout_marginLeft="148dip" android:textStyle="bold|italic"/> </LinearLayout>
package com.example.textspeech; import java.util.Locale; import android.os.Bundle; import android.app.Activity; import android.speech.tts.TextToSpeech; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.ImageButton; public class MainActivity extends Activity implements TextToSpeech.OnInitListener { /** Called when the activity is first created. */ // Creation of Objects : private TextToSpeech tts; private ImageButton btnSpeak; private EditText txtText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tts = new TextToSpeech(this, this); // Finding the Id of the Image Button i.e btnSpeak that is provided in the layout btnSpeak = (ImageButton) findViewById(R.id.btnSpeak); // Finding the Id of the EditText that is dragged in the layout txtText = (EditText) findViewById(R.id.txtText); // button on click event btnSpeak.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { speakOut(); } }); } @Override public void onDestroy() { // Don't forget to shutdown! if (tts != null) { tts.stop(); tts.shutdown(); } super.onDestroy(); } @Override public void onInit(int status) { // TODO Auto-generated method stub if (status == TextToSpeech.SUCCESS) { //Setting the language to US...you can change it to different language if you want int result = tts.setLanguage(Locale.US); // tts.setPitch(5); // set pitch level you can modify it if required // tts.setSpeechRate(2); // set speech speed rate if required if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { // If the language is not supported display it in the logcat Log.e("TTS", "Language is not supported"); } else { btnSpeak.setEnabled(true); speakOut(); } } else { Log.e("TTS", "Initilization Failed"); } } private void speakOut() { String text = txtText.getText().toString(); tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); } }
Hence, we have successfully learnt to develop an application that converts text into speech in android.
What to do if we need voice in hindi language?