Text To Speech In Android

Introduction

 
Android allows to user to convert his or her text  in to voice. Android provides a class named TextToSpeech for the purpose of converting text to voice. We can produce the voice output in different languages. Here I am going to show you how we can use this feature in Android. For that I created an application with an Edit text and a button. The edit text will accept text from the user and on clicking on the button will speech the text from the edit text that is entered by the user. For that first create your layout file.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  4. android:paddingRight="@dimen/activity_horizontal_margin"  
  5. android:paddingTop="@dimen/activity_vertical_margin"  
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  7.     <TextView android:text="Please enter the text to speech" android:layout_width="wrap_content"  
  8. android:layout_height="wrap_content"  
  9. android:textSize="20sp"  
  10. android:layout_alignParentTop="true"  
  11. android:layout_centerHorizontal="true"  
  12. android:id="@+id/textView" />  
  13.     <EditText  
  14. android:layout_width="wrap_content"  
  15. android:layout_height="wrap_content"  
  16. android:id="@+id/editText"  
  17. android:layout_below="@+id/textView"  
  18. android:layout_alignLeft="@+id/textView"  
  19. android:layout_alignStart="@+id/textView"  
  20. android:layout_alignRight="@+id/textView"  
  21. android:layout_alignEnd="@+id/textView" />  
  22.     <Button  
  23. android:layout_width="wrap_content"  
  24. android:layout_height="wrap_content"  
  25. android:text="Speech"  
  26. android:id="@+id/speechButton"  
  27. android:layout_below="@+id/editText"  
  28. android:layout_centerHorizontal="true"  
  29. android:layout_marginTop="43dp" />  
  30. </RelativeLayout>  
Bind the xml ui controls in to mainActivity in the onCreate(),
  1. editText = (EditText) findViewById(R.id.editText);  
  2. Button button = (Button) findViewById(R.id.speechButton);  
For using Text to Speech we need to initialise this and implement the onInit() method. This will contain a parameter status, please see the code below,
  1. textToSpeech = new TextToSpeech(MainActivity.thisnew TextToSpeech.OnInitListener() {  
  2. @Override  
  3. public void onInit(int i) {  
  4. if( i != TextToSpeech.ERROR){  
  5. textToSpeech.setLanguage(Locale.US);  
  6.         }  
  7.     }  
  8. }) ;  
In the onInit() please check the status for errors, if there is no error then set your language here. There are lots of locales available, some of them are as follows
  • CANADA
  • CHINA
  • ENGLISH
  • FRANCE
  • GERMANY
  • ITALY
  • JAPAN
  • etc
Now in the speech button click call the text to speech class to convert the text into voice. please see the code below.
  1. button.setOnClickListener(new View.OnClickListener() {  
  2. @Override  
  3. public void onClick(View view) {  
  4.         String toSpeak = editText.getText().toString() ;  
  5. textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null) ;  
  6.     }  
  7. });  
And in the onPause() we need to stop the text to speech object, please see the code below...
  1. public void onPause(){  
  2. if( textToSpeech != null){  
  3. textToSpeech.stop() ;  
  4. textToSpeech.shutdown() ;  
  5.     }  
  6. super.onPause();  
  7. }   
  • stop() will stop the speaking.
  • shutdown() release the resources used by Text To Speech  engine. 
Apart from the speak() there is some other methods like getLanguage(), isSpeaking() shutDown() etc.
 
Please see the screenshots also...