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.
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.
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);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
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
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
