Visit Sponsor

Written by 4:15 pm Android

Android TextToSpeech Example with Kotlin & Java

TextToSpeech (TTS) enables Android apps to convert text into spoken audio. This feature enhances accessibility, user engagement, and hands-free interaction in apps such as readers, translators, or voice-assisted utilities.

This updated guide on javatechig.com covers TextToSpeech implementation using modern APIs, proper language handling, and lifecycle management for robust integration.

What Is TextToSpeech in Android?

Android’s TextToSpeech API provides a system service that turns text into spoken words by using language engines available on the device.

Key benefits:

  • Accessibility support
  • Spoken feedback
  • Hands-free user experiences

This API works with both Kotlin and Java projects.

Adding TextToSpeech in Android Project

TextToSpeech is part of the Android framework; no extra Gradle dependency is required.

Ensure minimum SDK is set appropriately:

minSdkVersion 21+

Higher SDK levels improve language support and engine stability.

Initializing TextToSpeech

Kotlin Initialization

lateinit var tts: TextToSpeech

tts = TextToSpeech(this) { status ->
    if (status == TextToSpeech.SUCCESS) {
        tts.language = Locale.US
    }
}

Java Initialization

TextToSpeech tts = new TextToSpeech(this, status -> {
    if (status == TextToSpeech.SUCCESS) {
        tts.setLanguage(Locale.US);
    }
});

In both cases, onInit callback confirms when the engine is ready.

Speaking Text Programmatically

Kotlin Example

fun speak(text: String) {
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, "tts1")
}

Java Example

tts.speak("Hello from TTS", TextToSpeech.QUEUE_FLUSH, null, "tts1");

Use unique utterance IDs to track speech callbacks if needed.

Handling Language and Locale Support

Always verify that the selected language is supported:

val result = tts.setLanguage(Locale.UK)
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
    Log.e("TTS", "Language not supported")
}

Missing data can be resolved by installing voice packs via device settings.

Stopping and Releasing Resources

TextToSpeech consumes system resources. Always release it when no longer needed.

Kotlin

override fun onDestroy() {
    super.onDestroy()
    tts.stop()
    tts.shutdown()
}

Java

@Override
protected void onDestroy() {
    super.onDestroy();
    tts.stop();
    tts.shutdown();
}

Proper shutdown prevents memory leaks and runtime issues.

Advanced Features

Utterance Progress Listener

Track speech events:

tts.setOnUtteranceProgressListener(object : UtteranceProgressListener() {
    override fun onStart(utteranceId: String) {}
    override fun onDone(utteranceId: String) {}
    override fun onError(utteranceId: String) {}
})

Useful for sequencing spoken text or UI actions.

Common Errors & Fixes

1. No Sound Output

Cause: TTS engine not initialized
Fix: Confirm SUCCESS in init callback

2. Unsupported Language Error

Cause: Missing voice data
Fix: Prompt user to install language data

3. Multiple Overlapping Speech Requests

Cause: QUEUE_ADD misuse
Fix: Use QUEUE_FLUSH for immediate speech

Best Practices (2026 Updated)

  • Test on real devices with different TTS engines
  • Provide user preferences for speech rate and pitch
  • Avoid long text passages without user consent
  • Release TTS in onDestroy() to free resources
  • Leverage UtteranceProgressListener for advanced flows
Visited 5 times, 2 visit(s) today
Close