Offline Android Text‑to‑Speech without Third‑Party SDKs

This guide shows how to create an offline Android app that converts any text to speech using the platform‑provided TextToSpeech class, covering UI layout with EditText and Button, a singleton SpeechUtils helper, language, pitch and rate configuration, and full code snippets for a working demo.

The Dominant Programmer
The Dominant Programmer
The Dominant Programmer
Offline Android Text‑to‑Speech without Third‑Party SDKs

Goal

Implement offline speech synthesis on Android without third‑party SDKs.

UI Layout

Create a layout containing an EditText (input text) and a Button (trigger). Assign id attributes to both widgets.

layout screenshot
layout screenshot

Activity Code

Button button = (Button) findViewById(R.id.button);
EditText editText = (EditText) findViewById(R.id.editTextTextPersonName2);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        SpeechUtils.getInstance(LoginActivity.this)
                   .speakText(editText.getText().toString());
    }
});

The click handler retrieves the text from the EditText and forwards it to SpeechUtils.

SpeechUtils Singleton

import android.content.Context;
import android.speech.tts.TextToSpeech;
import java.util.Locale;

public class SpeechUtils {
    private static SpeechUtils singleton;
    private TextToSpeech textToSpeech;

    public static SpeechUtils getInstance(Context context) {
        if (singleton == null) {
            synchronized (SpeechUtils.class) {
                if (singleton == null) {
                    singleton = new SpeechUtils(context);
                }
            }
        }
        return singleton;
    }

    private SpeechUtils(Context context) {
        textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    textToSpeech.setLanguage(Locale.CHINA);
                    textToSpeech.setPitch(1.5f); // higher = higher‑pitched (female‑like) voice
                    textToSpeech.setSpeechRate(0.5f); // slower for clearer articulation
                }
            }
        });
    }

    public void speakText(String text) {
        if (textToSpeech != null) {
            textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        }
    }
}

The class uses double‑checked locking to ensure a single TextToSpeech instance. Initialization configures language, pitch, and speech rate.

Usage Note

When invoking the utility from an Activity, pass the activity context as ActivityName.this (e.g., LoginActivity.this) to avoid referencing the inner OnClickListener object.

SpeechUtils.getInstance(LoginActivity.this).speakText(editText.getText().toString());

Language Support

Older discussions claimed Chinese was unsupported; recent Android versions include it. Set the language with:

textToSpeech.setLanguage(Locale.CHINA);
language support screenshot
language support screenshot

Additional Properties

textToSpeech.setPitch(1.5f); // higher value yields higher‑pitched voice

textToSpeech.setSpeechRate(0.5f); // slower rate improves intelligibility

Reference Documentation

Android official TextToSpeech API reference: https://developer.android.google.cn/reference/android/speech/tts/TextToSpeech

API documentation screenshot
API documentation screenshot

Demo Project

Download the complete demo archive from:

https://files.cnblogs.com/files/badaoliumangqizhi/%E8%AF%AD%E9%9F%B3%E6%92%AD%E6%8A%A5.rar

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMobile DevelopmentAndroidSpeech SynthesisOfflineTextToSpeech
The Dominant Programmer
Written by

The Dominant Programmer

Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.