Android Text To Speech Tutorial

  • 0

Android Text To Speech Tutorial

Category : Uncategorized

In android, you can convert your text into speech by the help of TextToSpeech class. After completion of the conversion, you can playback or create the sound file.

Constructor of Text To Speech class

  • Text To Speech(Context context, Text To Speech.OnInitListener)

Methods of Text To Speech class

The commonly used methods of TextToSpeech class are as follows:

Method Description
int speak (String text, int queueMode, HashMap<string, string=””> params) converts the text into speech. Queue Mode may be QUEUE_ADD or QUEUE_FLUSH. Request parameters can be null, KEY_PARAM_STREAM, KEY_PARAM_VALUME etc.
int setSpeechRate(float speed) it sets the speed for the speech.
int setPitch(float speed) it sets the pitch for the speech.
int setLanguage (Locale loc) it sets the locale specific language for the speech.
void shutdown() it releases the resource set by TextToSpeech Engine.
int stop() it interrupts the current utterance (whether played or rendered to file) and discards other utterances in the queue.

TextToSpeech.OnInitListener Interface

You need to implement TextToSpeech.OnInitListener interface, for performing event handling on TextToSpeech engine.

Method of TextToSpeech.OnInitListener Interface

There is only one method in this interface.

Method Description
void onInit (int status) Called to signal the completion of the TextToSpeech engine initialization. The status can be SUCCESS or ERROR.

Android TextToSpeech Example

Let’s write the code to convert text into voice.

activity_main.xml

Drag one textview, one edittext and one button for the layout. Now the activity_main.xml file will look like this:

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <EditText
  7.         android:id=“@+id/editText1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentLeft=“true”
  11.         android:layout_alignParentTop=“true”
  12.         android:layout_marginLeft=“77dp”
  13.         android:layout_marginTop=“42dp”
  14.         android:ems=“10” >
  15.         <requestFocus />
  16.     </EditText>
  17.     <Button
  18.         android:id=“@+id/button1”
  19.         android:layout_width=“wrap_content”
  20.         android:layout_height=“wrap_content”
  21.         android:layout_alignLeft=“@+id/editText1”
  22.         android:layout_below=“@+id/editText1”
  23.         android:layout_marginLeft=“59dp”
  24.         android:layout_marginTop=“39dp”
  25.         android:text=“Speak” />
  26.     <TextView
  27.         android:id=“@+id/textView1”
  28.         android:layout_width=“wrap_content”
  29.         android:layout_height=“wrap_content”
  30.         android:layout_alignBaseline=“@+id/editText1”
  31.         android:layout_alignBottom=“@+id/editText1”
  32.         android:layout_alignParentLeft=“true”
  33.         android:text=“Enter Text:” />
  34. </RelativeLayout>

Activity class

Let’s see the code to speak the given text.

File: MainActivity.java
  1. package com.example.texttospeech;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import java.util.Locale;
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.speech.tts.TextToSpeech;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. public class MainActivity extends Activity implements
  14. TextToSpeech.OnInitListener {
  15. /** Called when the activity is first created. */
  16. private TextToSpeech tts;
  17. private Button buttonSpeak;
  18. private EditText editText;
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. tts = new TextToSpeech(thisthis);
  24. buttonSpeak = (Button) findViewById(R.id.button1);
  25. editText = (EditText) findViewById(R.id.editText1);
  26. buttonSpeak.setOnClickListener(new View.OnClickListener() {
  27.     @Override
  28.     public void onClick(View arg0) {
  29.         speakOut();
  30.     }
  31. });
  32. }
  33. @Override
  34. public void onDestroy() {
  35. // Don’t forget to shutdown tts!
  36. if (tts != null) {
  37.     tts.stop();
  38.     tts.shutdown();
  39. }
  40. super.onDestroy();
  41. }
  42. @Override
  43. public void onInit(int status) {
  44. if (status == TextToSpeech.SUCCESS) {
  45.     int result = tts.setLanguage(Locale.US);
  46.     if (result == TextToSpeech.LANG_MISSING_DATA
  47.             || result == TextToSpeech.LANG_NOT_SUPPORTED) {
  48.         Log.e(“TTS”“This Language is not supported”);
  49.     } else {
  50.         buttonSpeak.setEnabled(true);
  51.         speakOut();
  52.     }
  53. else {
  54.     Log.e(“TTS”“Initilization Failed!”);
  55. }
  56. }
  57. private void speakOut() {
  58. String text = editText.getText().toString();
  59. tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
  60. }
  61.     @Override
  62.     public boolean onCreateOptionsMenu(Menu menu) {
  63.         // Inflate the menu; this adds items to the action bar if it is present.
  64.         getMenuInflater().inflate(R.menu.activity_main, menu);
  65.         return true;
  66.     }
  67. }

Leave a Reply