BetterFrenchTts

class BetterFrenchTts(context: Context, config: BetterFrenchTts.Config = Config())

High-level French TTS wrapper around Android's native TextToSpeech.

Automatically selects the best offline French voice, generates SSML behind the scenes, and exposes a Kotlin DSL for fine-grained speech control.

Quick start

val tts = BetterFrenchTts(context, BetterFrenchTts.Config(
onReady = { it.speak("Bonjour le monde !") }
))

DSL usage

tts.speak {
text("Bonjour.")
pause(500)
slow { text("Ceci est important.") }
}

Coroutine usage

lifecycleScope.launch {
val result = tts.speakAndAwait("Bonjour le monde !")
}

Parameters

context

Android context (application context is used internally to avoid leaks).

config

Optional Config to customize voice selection, presets, and callbacks.

See also

Constructors

Link copied to clipboard
constructor(context: Context, config: BetterFrenchTts.Config = Config())

Types

Link copied to clipboard

Strategy for managing audio focus while speaking.

Link copied to clipboard
object Companion
Link copied to clipboard
data class Config(val defaultPreset: SpeechPreset = SpeechPreset.NEUTRAL, val preferredVoiceNames: List<String> = FrenchVoiceSelector.DEFAULT_PREFERRED_VOICES, val preprocessText: Boolean = true, val autoChunkLongText: Boolean = true, val audioFocus: BetterFrenchTts.AudioFocusMode = AudioFocusMode.DUCK, val onReady: (BetterFrenchTts) -> Unit? = null, val onInitError: (Int) -> Unit? = null)

Configuration for BetterFrenchTts.

Properties

Link copied to clipboard

Zero-based index of the item currently being spoken, or -1 if no queue is active.

Link copied to clipboard

The currently active Voice, or null if the engine is not yet ready or no voice was found.

Link copied to clipboard

true once the TTS engine has been initialized and a French voice has been selected.

Link copied to clipboard

true if a queue is active but paused.

Link copied to clipboard

true if a queue is currently playing (not paused).

Link copied to clipboard

true if the TTS engine is currently speaking an utterance.

Link copied to clipboard

Number of items currently in the queue. Returns 0 when no queue is active.

Functions

Link copied to clipboard

Adds a pronunciation rule to the dictionary.

Link copied to clipboard
fun buildSsml(block: SpeechBuilder.() -> Unit): String

Returns the SSML that would be generated by the DSL block, without speaking it.

fun buildSsml(text: String, preset: SpeechPreset = config.defaultPreset): String

Returns the SSML that would be generated for text with the given preset, without speaking it.

Link copied to clipboard

Removes all entries from the pronunciation dictionary.

Link copied to clipboard

Clears the speech queue and stops any ongoing playback.

Link copied to clipboard

Adds a DSL-built speech item to the queue.

fun enqueue(text: String, preset: SpeechPreset = config.defaultPreset): BetterFrenchTts

Adds a text item to the speech queue.

Link copied to clipboard
fun enqueueAll(texts: List<String>, preset: SpeechPreset = config.defaultPreset): BetterFrenchTts

Adds multiple text items to the queue at once, all sharing the same preset.

Link copied to clipboard

Returns all available offline French voices on this device, sorted by quality (descending).

Link copied to clipboard
fun onDone(callback: (String) -> Unit): BetterFrenchTts

Registers a callback invoked on the main thread when an utterance finishes successfully.

Link copied to clipboard
fun onError(callback: (String) -> Unit): BetterFrenchTts

Registers a callback invoked on the main thread when an utterance fails.

Link copied to clipboard
fun onQueueFinished(callback: () -> Unit): BetterFrenchTts

Registers a callback invoked on the main thread when all queue items have been spoken.

Link copied to clipboard

Registers a callback invoked on the main thread each time a new queue item starts playing.

Link copied to clipboard
fun onStart(callback: (String) -> Unit): BetterFrenchTts

Registers a callback invoked on the main thread when an utterance starts playing.

Link copied to clipboard

Registers a callback invoked on the main thread each time the TTS engine starts speaking a new word or text range.

Link copied to clipboard

Pauses the speech queue.

Link copied to clipboard

Starts playing the speech queue from the beginning.

Link copied to clipboard

Removes a word from the pronunciation dictionary.

Link copied to clipboard

Resumes a paused speech queue.

Link copied to clipboard
fun setVoice(voice: Voice)

Manually sets the active TTS voice.

Link copied to clipboard
fun shutdown()

Releases all TTS resources. Must be called when the instance is no longer needed (e.g. in Activity.onDestroy() or a Compose DisposableEffect).

Link copied to clipboard

Skips to the next item in the queue.

Link copied to clipboard
fun speak(queueMode: Int = TextToSpeech.QUEUE_FLUSH, block: SpeechBuilder.() -> Unit): SpeechResult

Speaks content built with the Kotlin DSL.

fun speak(text: String, preset: SpeechPreset = config.defaultPreset, queueMode: Int = TextToSpeech.QUEUE_FLUSH): SpeechResult

Speaks text aloud using the given preset.

Link copied to clipboard
suspend fun speakAndAwait(queueMode: Int = TextToSpeech.QUEUE_FLUSH, block: SpeechBuilder.() -> Unit): SpeechResult

Suspends until the TTS engine finishes speaking the DSL content.

suspend fun speakAndAwait(text: String, preset: SpeechPreset = config.defaultPreset): SpeechResult

Suspends until the TTS engine finishes speaking text.

Link copied to clipboard
fun speakSsml(ssml: String, queueMode: Int = TextToSpeech.QUEUE_FLUSH): SpeechResult

Speaks raw SSML directly, bypassing the DSL and preset system.

Link copied to clipboard
fun speakWithPreset(preset: SpeechPreset, queueMode: Int = TextToSpeech.QUEUE_FLUSH, block: SpeechBuilder.() -> Unit): SpeechResult

Speaks DSL content wrapped in the given preset prosody.

Link copied to clipboard
fun stop()

Stops any ongoing speech immediately and clears the playback queue.

Link copied to clipboard
fun synthesizeToFile(text: String, file: File, preset: SpeechPreset = config.defaultPreset): SpeechResult

Renders text to a WAV audio file using the given preset.