How to Implement Text To Speech in Android Studio - Android Hire
Android Phones

How to Implement Text To Speech in Android Studio

Dhaval
Text-to-speech (TTS) technology reads clearly digital manual – the words on computers, smartphones, and tablets. This tutorial explains how to implement text to speech in android or android speech synthesis.

Steps to Implement Text To Speech in Android Studio

Android allows you to convert your text into a voice. Android provides TextToSpeech class for this purpose. You need to instantiate an object of this class and also specify the initListener.

Step 1: Create a New Project

  1. Create a new project by going to File New Android Project. and fill the required details.
  2. Implement your main Activity class from OnInitListener
  3. Now add the following code to your class.

Step 2: Modify MainActivity.java

Implement the main activity class from TextToSpeech.OnInitListener to handle the initialization of the TTS engine.

package com.example.texttospeech;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View;

import java.util.Locale;

public class MainActivity extends Activity implements OnInitListener {
    TextToSpeech t1;
    EditText ed1;
    Button b1;

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

        ed1 = findViewById(R.id.editText);
        b1 = findViewById(R.id.button);

        t1 = new TextToSpeech(getApplicationContext(), this);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String toSpeak = ed1.getText().toString();
                Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
                t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null, null);
            }
        });
    }

    @Override
    public void onInit(int status) {
        if (status != TextToSpeech.ERROR) {
            t1.setLanguage(Locale.UK);
        }
    }

    @Override
    public void onPause() {
        if (t1 != null) {
            t1.stop();
            t1.shutdown();
        }
        super.onPause();
    }
}

Step 3: Define the Layout

Create activity_main.xml to define the user interface with a TextViewEditText, and a Button.

Step 4: Update AndroidManifest.xml

Ensure the MainActivity is declared in your AndroidManifest.xml.

" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.texttospeech">

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            intent-filter>
        activity>
    application>
manifest>

Changing Language :

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

tts.setLanguage(Locale.CHINESE); // Chinese language

Read More: How to change the background transition in Android Studio

Changing Pitch Rate :

You can set a 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.

 tts.setPitch(0.6);

Changing Speed Rate :

The speed rate can be set using setSpeechRate(). This also will take a 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

tts.setSpeechRate(2);

Conclusion

You can easily implement text-to-speech functionality in your Android app. This feature can enhance accessibility and user interaction, making your app more user-friendly.

Dhaval's profile picture

Dhaval

Dhaval is a consummate tech enthusiast with a penchant for software development and game creation. His technical prowess is reflected not only in his innovative projects but also in the insightful articles he pens. Beyond the world of technology, Dhaval immerses himself in the world of literature, indulging in novels that captivate his imagination during his leisure hours.

Related Posts

7 Top Samsung Galaxy Ring Alternatives for 2025

7 Top Samsung Galaxy Ring Alternatives for 2025

Tired of waiting for the Samsung Galaxy Ring to hit the market? You’re not alone. The smart ring market already has many impressive alternatives available now, despite the buzz around Samsung’s upcoming smart ring. These devices pack advanced health tracking and contactless payment features that might surpass Samsung’s planned offerings. The current lineup of smart […]

What Is Quiet Mode on Instagram and How to Activate It

What Is Quiet Mode on Instagram and How to Activate It

Ever wondered what Quiet Mode on Instagram is all about? This simple yet powerful Instagram feature helps you take a break from the constant buzz of notifications and focus on what truly matters. Whether you’re striving for better work-life balance, dedicating time to studying, or simply trying to disconnect from social media distractions, Quiet Mode […]

How to Make a Bed in Minecraft (Step-by-Step Guide)

How to Make a Bed in Minecraft (Step-by-Step Guide)

A bed in Minecraft is very important. It lets you skip the night and set your spawn point, so if you die, you will return to your bed instead of the original world spawn. This guide will show you how to gather materials, craft a bed, and set your spawn point. We’ll also show you how to customize your bed, build bunk […]

10 Best MMORPG Games For Android

10 Best MMORPG Games For Android

Not too long ago, MMORPG games and powerful gaming consoles were mostly exclusive to PCs. They required high-end graphics and systems to deliver their immersive gameplay. But times have changed. With the rise of gaming-oriented smartphones, you can now enjoy PC-like gaming experiences on your Android device. Thanks to these technological advancements and faster internet […]

Roblox: Fruit Battlegrounds codes (January 2025)

Roblox: Fruit Battlegrounds codes (January 2025)

Fruit Battlegrounds codes are all about getting more gems and help you to shoot up your rank in this One Piece anime-inspired game. This Fruit Battlegrounds was made by Popo developer. It is an action-packed game where players battle it out using unique fruit-based abilities. With constant updates, new fruits, and exciting challenges, it’s a fruity frenzy you won’t […]

Roblox: Ultimate Football Codes (January 2025)

Roblox: Ultimate Football Codes (January 2025)

Want to get some extra items for Ultimate Football in Roblox? You’ve come to the right place! Here’s the latest list of codes to help you score touchdowns and look stylish on the field. These codes offer free rewards such as coins and cosmetics to enhance your gameplay. What are Ultimate Football Codes? Ultimate Football […]

Roblox: Da Hood Codes (January 2025)

Roblox: Da Hood Codes (January 2025)

Are you a fan of Roblox games, in this article we will look at the Roblox Da Hood Codes for December 2024 that will help you unlock exclusive items, improve your gameplay and dominate the streets of Da Hood. You can feel that the game is inspired by the Grand Theft Auto series and there […]