Skip to main content

Make the first API call

This explains the order of TextToSpeech API calls and provides examples of scenarios that can be implemented with the API.

Order of API calls

Initialize SDK

When starting the TextToSpeech SDK, call the initialize API to initialize the TextToSpeech SDK.


enum class Mode {
HYBRID, // If the network is not connected, the SDK will connect to OnDevice speech synthesis as needed.
SERVER, // Connects to speech synthesis only when the network is available.
ON_DEVICE // Connects to OnDevice speech synthesis.
}

val textToSpeech = TextToSpeech(context, Mode.HYBRID) // Mode must be set

textToSpeech.initialize();

Add eventListener

Before registering the TextToSpeech app, call the addEventListener API to register the TextToSpeech event callback.

textToSpeech.addEventListener(listener: ai.pleos.playground.tts.listener.EventListener)

Register App

Call the registerApp API to register the TextToSpeech app with the TextToSpeech server. If the MODE is ON_DEVICE, this API does not need to be used.

val clientID = "your_client_id"
val clientSecret = "your_client_secret"
textToSpeech.registerApp(clientID, clientSecret, object : OnServerConnectionListener {
override fun onConnected() {
// The app can receive events once registration with the Speech server is complete
}

override fun onFailed(msg: String) {
// If the registration to the Speech server fails, an event with an error message will be delivered to the app.
}
})

Remove eventListener

Before exiting the TextToSpeech app, remove the registered event callback. To remove the event callback, call the removeEventListener API.

textToSpeech.removeEventListener(listener: ai.pleos.playground.tts.listener.EventListener)

Release SDK

Call the release API to free up TextToSpeech SDK resources.

textToSpeech.release()

Example scenarios for using the API

1. Setting AudioAttribute

After defining the AudioAttribute, call the setAudioAttributes API to set the attributes. If not set by the app, it defaults to AudioAttributes.CONTENT_TYPE_MEDIA.

val attribute = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.setUsage(AudioAttributes.USAGE_ASSISTANT)
.build()

textToSpeech.setAudioAttributes(attribute)
info

For more details on the AudioAttribute class, refer to the Android official documentation.

2. Start/Stop TextToSpeech Conversion

The following is example code for starting and stopping TextToSpeech conversion.

textToSpeech.speak("Hello")
textToSpeech.stop()