AndroidTextToSpeechEngine
TextToSpeechEngine implementation based on Android's TextToSpeech. An AndroidTextToSpeechEngine instance can only be used once it has completed its initialization. Implement the OnEngineReadyListener to be notified of the completion of the initialization. When you are done using the AndroidTextToSpeechEngine instance, call the close() method to release resources.
Example usage:
val ttsEngine = AndroidTextToSpeechEngine(context)
ttsEngine.addOnEngineReadyListener(object : OnEngineReadyListener {
override fun onReady() {
// TTS engine is ready for use
}
override fun onError(error: TextToSpeechEngineError) {
// Handle TTS engine initialization error
}
})
// Voices API usage (once TTS engine is initialized):
val voicesResult = ttsEngine.getVoices()
if (voicesResult.isSuccess()) {
val availableVoices = voicesResult.value()
val voicesInCurrentLanguage = availableVoices.filter {
it.locale.language == currentLanguage.language // filter voices by current language
}
val selectedVoice = voicesInCurrentLanguage.first() // select preferred voice depending on the use case
val setVoiceResult = ttsEngine.setVoice(selectedVoice)
if (setVoiceResult.isFailure()) {
// Handle setting voice error
}
} else {
// Handle getting voices error
}
ttsEngine.close() // Release the resources when done
Important: This is a Public Preview API. It may be changed or removed at any time.
Properties
Supported phonetic alphabets. This engine supports only IPA_PHONETIC_ALPHABET.
Functions
Sets a listener to report on the readiness of the TextToSpeechEngine.
Changes the language that is used to synthesize audio messages.
Returns the current language used to synthesize audio messages.
Checks whether the specified language as represented by the Locale is available.
Synthesizes an audio message. If another audioMessage is currently being processed this one will be added to the queue. If there is already another AudioMessages in the queue it will be flushed and replaced by this one.
Synthesizes a tagged message. TaggedMessage is parsed into MessageType.Ssml format prior to being synthesized. PhoneticAlphabetNotSupportedError is thrown if an unsupported phonetic alphabet is used.
Removes the listener that reports on the readiness of the TextToSpeechEngine.
Stops synthesis of current AudioMessage and discards all messages in the queue.