Overview
The text-to-speech feature provides natural voice synthesis for Spanish text, with automatic initialization and lifecycle management.The TTS engine is configured for Chilean Spanish (
es-CL) to provide natural pronunciation for local users.Architecture
The feature consists of a single controller class:- TextToSpeechController (
services/TextToSpeechController.kt:7) - Manages TTS lifecycle and playback
Implementation
TheTextToSpeechController class wraps Android’s TextToSpeech API:
Key features
Chilean Spanish
Optimized for Spanish (Chile) locale
Auto-initialization
Automatic TTS engine setup on creation
Queue management
QUEUE_FLUSH mode for immediate playback
Lifecycle aware
Proper cleanup to prevent memory leaks
Usage
Initialization process
The TTS engine initializes asynchronously:The controller tracks initialization state with the
ready flag. Speech requests before initialization complete are silently ignored.Initialization states
| State | Description | Behavior |
|---|---|---|
| Initializing | TTS engine loading | speak() calls ignored |
| Ready | Locale set to es-CL | speak() calls processed |
| Failed | Initialization error | speak() calls ignored |
Speaking text
Thespeak() method handles text-to-speech conversion:
Parameters explained
text (String)
text (String)
The text to be spoken. Automatically trimmed and validated.
QUEUE_FLUSH
QUEUE_FLUSH
Playback mode that clears any pending speech and starts immediately.Alternative:
QUEUE_ADD appends to queue instead of replacing.params (null)
params (null)
Optional parameters bundle (not used in this implementation).
utteranceId ("phrase_tts")
utteranceId ("phrase_tts")
Unique identifier for tracking this speech request.
Queue modes
- QUEUE_FLUSH
- QUEUE_ADD
Interrupts any ongoing speech and starts immediately:Use case: User clicks a new phrase while another is playing.
Stopping speech
Stop playback immediately:Lifecycle management
Properly clean up resources when the controller is no longer needed:In Compose
Integration with phrase management
The home screen demonstrates TTS integration with saved phrases (ui/home/HomeScreen.kt:88):
Validation
The controller includes automatic validation:Validation checks
- Initialization check:
if (!ready) return - Text cleaning:
text.trim() - Empty check:
if (clean.isBlank()) return
Validation is automatic - you don’t need to check these conditions before calling
speak().Error handling
The TTS engine may fail to initialize on some devices:Common initialization failures
Missing TTS engine
Missing TTS engine
Cause: Device doesn’t have a TTS engine installedSolution: Prompt user to install Google Text-to-Speech from Play Store
Language not supported
Language not supported
Cause: TTS engine doesn’t support Spanish (Chile)Solution: Fall back to generic Spanish or prompt language download
Resource unavailable
Resource unavailable
Advanced usage
Speech rate and pitch
Customize voice characteristics:Utterance callbacks
Track speech progress:Testing
Test TTS availability
Emulator testing
- Most emulators include Google TTS engine
- Spanish voice may need to be downloaded
- Test with various text lengths and special characters
Best practices
Always clean up
Call
destroy() in onDispose or component cleanup to release resources.Handle initialization
TTS initializes asynchronously - calls before ready are ignored.
Validate input
The controller validates text automatically, but check for meaningful content.
Consider queue mode
Use
QUEUE_FLUSH for immediate feedback, QUEUE_ADD for sequences.The TTS engine runs in the background. If your app is backgrounded during speech, playback continues until completion.
Dependencies
No additional dependencies required - TextToSpeech is part of Android framework.Locale support
The controller is configured for Chilean Spanish:Related features
Phrase Management
Store and manage phrases to speak
Speech-to-Text
Convert speech to text input