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.
- Kotlin
- Java
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();
public enum 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.
}
TextToSpeech textToSpeech = new 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.
- Kotlin
- Java
textToSpeech.addEventListener(listener: ai.pleos.playground.tts.listener.EventListener)
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.
- Kotlin
- Java
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.
}
})
String clientID = "your_client_id";
String clientSecret = "your_client_secret";
textToSpeech.registerApp(clientID, clientSecret, new OnServerConnectionListener() {
@Override
void onConnected() {
// The app can receive events once registration with the Speech server is complete
}
@Override
void onFailed(String msg) {
// 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.
- Kotlin
- Java
textToSpeech.removeEventListener(listener: ai.pleos.playground.tts.listener.EventListener)
textToSpeech.removeEventListener(listener: ai.pleos.playground.tts.listener.EventListener);
Release SDK
Call the release API to free up TextToSpeech SDK resources.
- Kotlin
- Java
textToSpeech.release()
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.
- Kotlin
- Java
val attribute = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.setUsage(AudioAttributes.USAGE_ASSISTANT)
.build()
textToSpeech.setAudioAttributes(attribute)
AudioAttributes attribute = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.setUsage(AudioAttributes.USAGE_ASSISTANT)
.build();
textToSpeech.setAudioAttributes(attribute);
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.
- Kotlin
- Java
textToSpeech.speak("Hello")
textToSpeech.stop()
textToSpeech.speak("Hello");
textToSpeech.stop();