Search Tutorials

Thursday 31 October 2013

Convert Text to Speech in Android

This is a text to speech android application which takes text from edit text and speaks in a given language. Create new android project and drop text view, edit text, button on linear layout and give id txtText to edit text and btnspeak to button. The code of android XML file is given below:

Convert Text to Speech in Android
Convert Text to Speech in Android

<?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="#345" >
 
<TextView android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Text To Speech"
   android:padding="15dip"
   android:textColor="#0587d9"
   android:textSize="26dip"
   android:gravity="center"
   android:textStyle="bold"/>
 
<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"/>
 
<Button android:id="@+id/btnSpeak"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Speak Out"
   android:layout_margin="10dip"/>
</LinearLayout>

Now open Java file and initialize TTS (Text To Speech) object and check given language is available or not, given language is supported or not, etc. The code of android Java file is given below with explanation:


package com.innosen.texttospeech; //your package name

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.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AndroidTextToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
  private int result=0;
  private TextToSpeech tts;
  private Button btnSpeak;
  private EditText txtText;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tts = new TextToSpeech(this, this);
    btnSpeak = (Button)findViewById(R.id.btnSpeak);
    txtText = (EditText)findViewById(R.id.txtText);
    //button on click event
    btnSpeak.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View arg0) {
          speakOut();
        }
    });
  }
  //shutdown tts when activity destroy
  @Override
  public void onDestroy() {
  // Don't forget to shutdown!
  if (tts != null) {
    tts.stop();
    tts.shutdown();
   }
   super.onDestroy();
  }
  //It will called before TTS started
  @Override
  public void onInit(int status) {
  // TODO Auto-generated method stub
  //check status for TTS is initialized or not
  if (status == TextToSpeech.SUCCESS) {
  //if TTS initialized than set language
  result = tts.setLanguage(Locale.US);

  // tts.setPitch(5); // you can set pitch level
  // tts.setSpeechRate(2); //you can set speech speed rate

  //check language is supported or not
  //check language data is available or not
 if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
   Toast.makeText(this, "Missing data", Toast.LENGTH_LONG).show();
   //disable button
   btnSpeak.setEnabled(false);
  } else {
   //if all is good than enable button convert text to speech
   btnSpeak.setEnabled(true);
   }
  } else {
      Log.e("TTS", "Initilization Failed");
     }
  }
  //call this method to speak text
  private void speakOut() {
  String text = txtText.getText().toString();
  if(result!=tts.setLanguage(Locale.US))
  {
  Toast.makeText(getApplicationContext(), "Enter right Words...... ", Toast.LENGTH_LONG).show();
  }else
   {
    //speak given text
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
   }
  }
}

Enter some text in Edit text box and click on given below button and listen voice. Voice may not come if you are testing this project on emulator, so install .APK file of this project in your mobile and use. If you any doubts please comment. Share this post and help all android developers.

Related Tutorials:-

Play mp3 file from a project folder

Use SQLite Database

Enable and Disable WiFi

Enable and Disable Bluetooth

Get Latitude and Longitude of the current Location

Use Sensor | Motion detection

27 comments:

  1. this program has error in line no 22, 24 and 25. please check it asap.

    ReplyDelete
    Replies
    1. i didn't find any error in the programme

      Delete
    2. i am having o\lot many errors,... please help me,.....

      Delete
    3. check correctly ..it works.

      Delete
    4. can u plzz solve our problem sir ??

      Delete
    5. can u plzz solve our problem sir ??

      Delete
  2. thank u so much sir... it actually works :)

    ReplyDelete
  3. Hey, Thanks, this worked. but how can I save output as an audio file?

    ReplyDelete
  4. sir it is not working on mobile
    "Unfortunately app has stop working" error occur please helpus

    ReplyDelete
  5. plzzz sir rply quickly . . .

    ReplyDelete
  6. we r in trouble plzzzz

    ReplyDelete
    Replies
    1. ask me on hangout or send logcat on email id: info@coders-hub.com

      Delete
  7. Sir whe I opened the app in my mobile it is not working instead a dialogue pops out "Unfortunately app is stopped " .Kindly help in this regard asap

    ReplyDelete
  8. Hi Sir,

    I had coded same code in my eclipse in r.java file it is getting error

    error as below

    Multiple markers at this line
    - some cannot be resolved to a type
    - Syntax error on token "some", ;
    expected
    - to cannot be resolved to a type
    - Syntax error on token "speak", delete
    this token

    ReplyDelete
  9. how to build TTS for new langauge?

    ReplyDelete
    Replies
    1. tts.setLanguage(Locale.US);

      after Locale.(DOT) press CTRL+SPACE and check available languages.

      Delete
  10. Hi I put code the program runs but no voice is hear.thak for help

    ReplyDelete
  11. Sir, how to implement a text to speech in a custom view layout..
    when a bitmap is clicked it will speak out the database i had in my app

    ReplyDelete
  12. please help me. in java file i have an error plz let me know how to fix it :
    error: setContentView(R.layout.main); cana not resolve symbol main

    ReplyDelete
  13. Sir could you please help me with codes for android offline voice command recognition
    Thanks

    ReplyDelete
  14. I wanna develop an android app that enables me to control my phone using certain voice commands. Can you help me with that sir?
    And I wanna know which will be better?
    Using Android Studio or Eclipse

    ReplyDelete
    Replies
    1. Eclipse is outdated now.so u have to develop whatever you want in android studio

      Delete
  15. There is no error but i cant hear the audio

    ReplyDelete
  16. works !! good job bro.

    ReplyDelete
  17. This comment has been removed by a blog administrator.

    ReplyDelete

Back to Top