Android Text to Speech converter code in Android Studio

Hello Guys. Welcome to our new tutorial of Android Text to Speech converter using Android Studio. Here we will teach you how to use text to speech in android. Here we will also explain how to change between different languages, pitch and speed level. Major application of android text to speech example is in different types of Apps like Google Play Books to Read Aloud your favorite book or Google Translate to speak translations aloud so that you can hear the pronunciation of a word. You can see demo of this tutorial below:

Demo of Android Text to Speech Example




Download full code here:

Android Text to Speech

How to make Android text to Speech App

Here we will use TextToSpeech class to synthesize speech from text for immediate playback or to create a sound file. It can be used only after initialization of text which in turn can be implemented through TextToSpeech.OnInitListener.

Creating a New Project – Android text to Speech

  1. Open Android Studio and create a new project text to Speech and company domain application.example.com (We have used our own company domain i.e androidtutorialpoint.com. Similarly you can use yours).
  2. Click Next and choose Min SDK (It should be maximum possible). Again Click Next and Choose Blank Activity .
  3. Choose the Activity as MainActivity and click next.
  4. Leave all other things as default and Click Finish.
  5. Layout of Android Text to Speech Example

    Add following code inside activity_main.xml

    activity_main.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        android:id="@+id/activity_main"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.androidtutorialpoint.texttospeech.MainActivity">
        <Button
            android:id="@+id/btnSpeak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="130dp"
            android:text="Speak Out" />
        <EditText
            android:id="@+id/txtText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="61dp"
            android:ems="10"
            android:inputType="text" />
    </RelativeLayout>

    Working code of Android text to speech

    Add following code inside MainActivity.java of Android Studio:

    MainActivity.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    package com.androidtutorialpoint.texttospeech;
    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;
    public class MainActivity extends Activity implements
            TextToSpeech.OnInitListener {
        /** Called when the activity is first created. */
        private TextToSpeech tts;
        private Button btnSpeak;
        private EditText txtText;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_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();
                }
            });
        }
        @Override
        public void onDestroy() {
            // Don't forget to shutdown tts!
            if (tts != null) {
                tts.stop();
                tts.shutdown();
            }
            super.onDestroy();
        }
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                int result = tts.setLanguage(Locale.US);
                if (result == TextToSpeech.LANG_MISSING_DATA
                        || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS", "This Language is not supported");
                } else {
                    btnSpeak.setEnabled(true);
                    speakOut();
                }
            } else {
                Log.e("TTS", "Initilization Failed!");
            }
        }
        private void speakOut() {
            CharSequence text = txtText.getText();
            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null,"id1");
        }
    }
    In the above, we have applied an OnClickListener for button <>btnSpeak such that whenever user clicks on it function speakOut() will be called. This function will take text as an input and convert it into voice using tts.speak. In the speak() function second argument is provided as TextToSpeech.QUEUE_FLUSH where all entries in the playback queue (media to be played and text to be synthesized) are dropped and replaced by the new entry. You can also provide QUEUE_ADD where the new entry is added at the end of the playback queue.

    Change language, pitch or speed level of voice

    You can change language using setLanguage() function. For Example: To set French language, set tts.setLanguage(Locale.FRANCE).
    Similarly to change pitch of voice use tts.setPitch(0.6) and for speed use tts.setSpeechRate(2).
    Now run this App. You can see demo of this tutorial in the video given at the start. All the best !!

Post a Comment

what do you think ?

Previous Post Next Post