Home > DeveloperSection > Articles > Text to Speech in Android

Text to Speech in Android


Android Android 
Ratings:
0 Comment(s)
 630  View(s)
Rate this:

Text to Speech in Android

Android is providing a cool feature (from Android 1.6) called Text to Speech (TTS) which speaks the text in different languages. This tutorial explains how to work with android text to speech or android speech synthesis. In this article i also explained changing the language type, pitch level and speed level.

     Android provides Speech package that makes Text to Speech conversion very easy. android.speech.tts.TextToSpeach class provides necessary methods to the trick. At the start we need to implement the TextToSpeech.OnInitListener to do the initialization. During initialization we can set the Locale, Audio pitch rate, audio speed. setLanguage() method is used to set the language.

In this article I am creating a sample for convert text to speech

1.      Create an android project

2.      Add following views in activity_main.xml file

<?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:background="#282828"

    android:orientation="vertical" >

 

    <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" />

 

    <Spinner

        android:id="@+id/spin"

           android:layout_margin="10dip"

        android:layout_width="fill_parent"

        android:layout_height="52dp"

        android:background="@android:drawable/btn_dropdown"

        android:focusable="false" />

 

    <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 some text to speak"

        android:textColor="#ffffff" />

 

    <Button

        android:id="@+id/btnSpeak"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_margin="10dip"

        android:text="Speak Out" />

 

</LinearLayout>

3.      Add following code in MainActivity.class

 

package com.example.androiddemospeech;

 

import java.util.ArrayList;

import java.util.Locale;

 

import android.app.Activity;

import android.content.Context;

import android.graphics.Color;

import android.os.Bundle;

import android.speech.tts.TextToSpeech;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Spinner;

import android.widget.TextView;

 

public class MainActivity extends Activity implements

           TextToSpeech.OnInitListener {

     /** Called when the activity is first created. */

 

     private TextToSpeech tts;

     int spinPosition = 0;

     private Button btnSpeak;

 

     private EditText txtText;

     Spinner spinner;

     ArrayList<String> llist;

     ArrayAdapter<String> adapter;

 

     @Override

     public void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

           spinner = (Spinner) findViewById(R.id.spin);

           // add item in arraylist

           llist = new ArrayList<String>();

           llist.add("Select  Language");

           llist.add("CANADA");

           llist.add("CHINESE");

           llist.add("ENGLISH");

           llist.add("FRANCE");

           llist.add("GERMAN");

           llist.add("ITALIAN");

           llist.add("JAPAN");

           llist.add("UK");

           llist.add("US");

 

           // add list in array adapter

           adapter = new MyAdapter(getApplicationContext(),

               android.R.layout.simple_expandable_list_item_1, llist);

 

           // set adapter in spinner

           spinner.setAdapter(adapter);

 

  // TextToSpeech synthesizes speech from text for immediate playback //or to create a sound file

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

                }

 

           });

     }

 

     @Override

     public void onDestroy() {

           // Don't forget to shutdown!

           if (tts != null) {

                tts.stop();

                tts.shutdown();

           }

           super.onDestroy();

     }

 

     @Override

     public void onInit(int status) {

           // TODO Auto-generated method stub

           int result;

           // check item selection from spinner

           if (status == TextToSpeech.SUCCESS) {

                switch (spinPosition) {

 

                case 1:

                     result = tts.setLanguage(Locale.CANADA);

 

                     break;

                case 2:

                     result = tts.setLanguage(Locale.CHINESE);

 

                     break;

                case 3:

                     result = tts.setLanguage(Locale.ENGLISH);

 

                     break;

                case 4:

                     result = tts.setLanguage(Locale.FRANCE);

 

                     break;

                case 5:

                     result = tts.setLanguage(Locale.GERMAN);

 

                     break;

                case 6:

                     result = tts.setLanguage(Locale.ITALIAN);

 

                     break;

                case 7:

                     result = tts.setLanguage(Locale.JAPAN);

 

                     break;

                case 8:

                     result = tts.setLanguage(Locale.UK);

 

                     break;

                case 9:

                     result = tts.setLanguage(Locale.US);

 

                     break;

                default:

                     result = tts.setLanguage(Locale.ENGLISH);

                     break;

                }

 

                // tts.setPitch(5); // set pitch level

 

                // tts.setSpeechRate(2); // set speech speed rate

 

                if (result == TextToSpeech.LANG_MISSING_DATA

                       || result == TextToSpeech.LANG_NOT_SUPPORTED) {

 

                } else {

                     btnSpeak.setEnabled(true);

                     speakOut();

                }

 

           } else {

 

           }

 

     }

 

     private void speakOut() {

 

           String text = txtText.getText().toString();

 

           tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

     }

// Add custom adapter class for spinner item

     public class MyAdapter extends ArrayAdapter<String> {

 

           public MyAdapter(Context context, int textViewResourceId,

 

           ArrayList<String> llist) {

 

                super(context, textViewResourceId, llist);

 

                // TODO Auto-generated constructor stub

 

           }

 

           @Override

           public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {

 

                LayoutInflater inflater = getLayoutInflater();

 

                View spinnerItem = inflater.inflate(

 

                android.R.layout.simple_spinner_item, null);

 

                TextView mytext = (TextView) spinnerItem

 

                .findViewById(android.R.id.text1);

 

                mytext.setText(llist.get(position));

 

                mytext.setTextColor(Color.RED);

               

                // set selected item position

                spinPosition = position;

                mytext.setTextSize(16);

                mytext.setPadding(0, 5, 5, 6);

 

                return spinnerItem;

 

           }

     }

}

 

Now run your application

 


Don't want to miss updates? Please click the below button!

Follow MindStick