Android Text to Speech Tutorial

In this android tutorial we are going to learn creating Android Text to Speech. Android allows you convert your text into voice. Not only you can convert it but it also allows you to speak text in variety of different languages.

Android Text to Speech Tutorial

Download projct file of  Android Text  To Speech  Tutorial – Android Studio

I developed a simple interface with one input field and a button to trigger a event that will take text from input field and speaks out.

texttospeech

1. Create a new project by going to File ⇒ New Android Project. and fill required details.

2. Now add following code in src/MainActivity.java.

package com.andrious.texttospeech;

import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    TextToSpeech t1;
    EditText write;
    ImageButton speakbtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        write=(EditText) findViewById(R.id.editText);
        speakbtn=(ImageButton)findViewById(R.id.board);

        t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    t1.setLanguage(Locale.UK);
                }
            }
        });
        speakbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String toSpeak = write.getText().toString();
                Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
                t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
            }
        });

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

3. Now add following xml in activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:orientation="vertical"
 android:gravity="center"
 android:background="#3F51B5"
 android:padding="10dp"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <EditText
     android:id="@+id/editText"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginBottom="5dp"
     android:background="#ffffff"
     android:padding="8dp"
     android:inputType="textMultiLine"
     android:maxHeight="140dp"
     android:scrollbars="vertical"
     android:hint="Write your text here :"/>
 <ImageButton
     android:layout_gravity="end"
     android:layout_width="90dp"
     android:layout_height="60dp"
     android:shadowRadius="5"
     android:id="@+id/board"
     app:srcCompat="@drawable/speaker"
     android:scaleType="fitXY"/>
</LinearLayout>

4. Now run your project and test your app by entering some text in input filed.

Changing Language

You can change language to speak by using setLanguage() function. Lot of languages are supported like Canada, French, Chinese, Germany etc.,

text to speech

t1.setLanguage(Locale.UK); // UK language

Changing Pitch Rate

You can set speed pitch level by using setPitch() function. By default the value is 1.0 You can set lower values than 1.0 to decrease pitch level or greater values for increase pitch level.

t1.setPitch(0.6);

Changing Speed Rate

The speed rate can be set using setSpeechRate(). This also will take default of 1.0 value. You can double the speed rate by setting 2.0 or make half the speed level by setting 0.5

t1.setSpeechRate(2);

 

14 Comments

  1. Christiane Rutana

    July 11, 2018 at 11:22 pm

    Do you have a spam problem on this blog; I also am a blogger, and I was curious about your situation; we have developed some nice methods and we are looking to trade methods with other folks, please shoot me an e-mail if interested.

  2. Berry Fahrenkrug

    July 15, 2018 at 2:28 pm

    Appreciating the hard work you put into your site and detailed information you offer. It’s great to come across a blog every once in a while that isn’t the same old rehashed information. Wonderful read! I’ve saved your site and I’m adding your RSS feeds to my Google account.

  3. Mitchell Rosero

    July 20, 2018 at 5:27 pm

    One thing is that while you are searching for a education loan you may find that you will need a cosigner. There are many circumstances where this is correct because you may find that you do not employ a past history of credit so the loan provider will require that you’ve someone cosign the financial loan for you. Good post.

  4. Sachiko Dubinsky

    July 21, 2018 at 2:54 pm

    It’s actually a nice and useful piece of information. I’m glad that you shared this useful info with us. Please keep us informed like this. Thanks for sharing.

  5. Yuki Cesari

    July 23, 2018 at 1:35 pm

    One thing I have actually noticed is that often there are plenty of misguided beliefs regarding the finance institutions intentions whenever talking about foreclosed. One fantasy in particular would be the fact the bank would like your house. Your banker wants your hard earned money, not the home. They want the funds they gave you with interest. Keeping away from the bank will simply draw the foreclosed conclusion. Thanks for your post.

  6. furtdsolinopv

    October 7, 2018 at 2:26 pm

    I was looking through some of your articles on this website and I think this website is rattling informative! Continue putting up.

  7. Leo Zaneski

    October 27, 2018 at 4:37 pm

    you got a very great website, Gladiola I found it through yahoo.

  8. loans no credit check

    December 8, 2018 at 10:16 am

    Do you need to justify that? Haha|

  9. ViolaBig

    December 25, 2018 at 1:30 am

    Hello. I have checked your andrious.com and i see you’ve got some duplicate content so probably it is the reason that you
    don’t rank hi in google. But you can fix this issue fast. There is a tool
    that creates content like human, just search in google: miftolo’s tools

  10. Jarod Markie

    January 9, 2019 at 6:27 pm

    I was studying some of your content on this internet site and I believe this web site is really informative ! Retain putting up.

  11. smart defrag 6.0.1 key

    February 20, 2019 at 9:25 pm

    You could certainly see your skills in the work you write. The sector hopes for even more passionate writers such as you who aren’t afraid to mention how they believe. All the time go after your heart.

  12. Freebies

    April 26, 2019 at 5:18 am

    Get real free stuff and free samples by mail, coupons, deals and giveaways. http://www.cravefreebies.com/free-samples/ Updated constantly with all the newest Free Stuff, Free Samples, Freebies, Coupons and Deals.

  13. Free Stuff

    April 26, 2019 at 9:30 pm

    The best free stuff, free samples, freebies, deals, and coupons. Updated daily, http://www.cravefreebies.com/freebies/ so there’s always something fresh.

  14. Free Stuff

    May 25, 2019 at 8:04 pm

    You made some good points there. I did a search on the theme and found a good number of people will have the same opinion with your blog.

Leave a Reply