Android TutorialsLearn to develop an application that converts text into speech in android

Learn to develop an application that converts text into speech in android

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 :

  1. First of all download the ADT- Bundle from developer.android.com and extract it .
  2. For downloading and installation of eclipse you can follow the following link Installing Android SDK
  3. Now open up eclipse and click on File -> create new Android Application Projects
  4. text to speech

  5. Provide a project name for example ” TextSpeech ” and set the Minimum Required and Target SDK for the project as ” 19 “.
  6. Click on Next and select Blank from the appeared dialog box
  7. 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
  8. Click on Finish and your MainActivity.java and activity_main.xml files will be generated.
  9. 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
  10. layout

  11. Similarly drag an Plain text i.e an EditText and Image button .
  12. Hence, your layout will look like as shown below :
  13. layout final

  14. 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.
  15. Hence the complete code for your activity_main.xml file is given below :
  16. <?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>

  17. Here is the ImageView of the above code :
  18. speech
    text

  19. Now just move to MainActivity.java file and extend your class with Activity implements TextToSpeech.OnInitListener
  20. Hence, the complete code is given below which is self explanatory :
  21. 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);
    }
    
    
    }

  22. Here is the image view of the above code :
  23. text to speech
    convert text to speech
    text
    speech application

  24. Now run your project and you will get your application running on the emulator as shown below :
  25. emulator

  26. Just type some text in the given EditText and click on the ImageButton i.e. image of a Boy in this example.
  27. hello

  28. Click on the image button and it will transform your text into speech.

Hence, we have successfully learnt to develop an application that converts text into speech in android.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -