how to Convert Text to Speech in Android

activity_main.xml

<?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>


MainActivity.java

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);
   }
  }
}


Comments

Popular posts from this blog

how to fetch blob image gallery in php html sql

how to save image to pdf in android

How to create album view with fancybox slider view in html php complete code