Text To Speech (TTS)
Setup a speaker
Section titled “Setup a speaker”var tts by remember { mutableStateOf<TextToSpeech?>(null) }var ttsReady by remember { mutableStateOf(false) }
LaunchedEffect(Unit) { tts = TextToSpeech(context) { status -> if (status == TextToSpeech.SUCCESS) { tts?.language = Locale.US ttsReady = true } }}
DisposableEffect(Unit) { onDispose { tts?.stop() tts?.shutdown() }}Speak the text
Section titled “Speak the text”tts?.speak(text, TextToSpeech.QUEUE_FLUSH, null, null)Queue Options
Section titled “Queue Options”The second parameter decides the TTS engine behavior when there’s new text to speak:
| Usage | Description |
|---|---|
TextToSpeech.QUEUE_FLUSH | Speak immediately |
TextToSpeech.QUEUE_ADD | Speak right after the current speech is done |