Tuesday, August 13, 2013

Text To Speech Example with source code

Text to speech as name suggest, It speaks whatever you write !!! It's interesting. Isn't It? Now you can add this function into your application and make more downloads.... ;)

It is very simple and less code to apply this function.Before start to integrate this function, make sure your device / emulator has support language and TTS(Text To Speech).

Now here is full source code with steps:

  • Create one project in which you want to integrate TTS.
  • Add below code in activity_main.xml file.

activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:ignore="HardcodedText" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="15dip"
        android:text="Text To Speech"
        android:textColor="#0587d9"
        android:textSize="26dip"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/txtText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:layout_marginTop="20dip"
        android:hint="Enter text to speak" />
    <Button
        android:id="@+id/btnSpeak"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:enabled="false"
        android:text="Speak" />
</LinearLayout>



  • Now implements OnInitListener to MainActivity and add unimplemented method.
  • add below code above onCreate method in MainActivity.java.


        private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;

  • Now add below code in onCreate method in MainActivity.java.
          tts = new TextToSpeech(this, this);
// Refer 'Speak' button
btnSpeak = (Button) findViewById(R.id.btnSpeak);
// Refer 'Text' control
txtText = (EditText) findViewById(R.id.txtText);
// Handle onClick event for button 'Speak'
btnSpeak.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
// Method yet to be defined
speakOut();
}

});



  • onInit(int status) method is automatically created.
  • add below code in it (onInit method).


if (status == TextToSpeech.SUCCESS) {
// Setting speech language
int result = tts.setLanguage(Locale.US);
// If your device doesn't support language you set above
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
// Cook simple toast message with message
Toast.makeText(this, "Language not supported",
Toast.LENGTH_LONG).show();
Log.e("TTS", "Language is not supported");
}
// Enable the button - It was disabled in main.xml (Go back and
// Check it)
else {
btnSpeak.setEnabled(true);
}
// TTS is not initialized properly
} else {
Toast.makeText(this, "TTS Initilization Failed", Toast.LENGTH_LONG)
.show();
Log.e("TTS", "Initilization Failed");
}


  • Create another method speakOut() and add below code, It is used to read out typed text.

private void speakOut() {
        //Get the text typed
       String text = txtText.getText().toString();
        //If no text is typed, tts will read out 'You haven't typed text'
        //else it reads out the text you typed
       if (text.length() == 0) {
           tts.speak("You haven't typed text", TextToSpeech.QUEUE_FLUSH, null);
       } else {
           tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
       }

   }

  • Don't forget to distroy activity after speak function,otherwise it  may be crashed while running.

public void onDestroy() {
        // Don't forget to shutdown!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

  • Thats It ! Run project.

Download full source code: Download




No comments:

Post a Comment