public final class TTSManager extends Object implements AutoCloseable, TTSListener
Spokestack's TTS manager follows the same setup pattern as its speech pipeline, and the two APIs are similar, but the components operate independently.
The TTS manager establishes the external service that will be used to synthesize system prompts and, optionally, a component that handles playback of the resulting audio streams without the app having to manage Android media player resources. The following configuration enables both features, using Spokestack's TTS service to process synthesis requests and its output component to automatically play them:
TTSManager ttsManager = new TTSManager.Builder()
.setTTSServiceClass("io.spokestack.spokestack.tts.SpokestackTTSService")
.setOutputClass("io.spokestack.spokestack.tts.SpokestackTTSOutput")
.setProperty("spokestack-id", "f0bc990c-e9db-4a0c-a2b1-6a6395a3d97e")
.setProperty(
"spokestack-secret",
"5BD5483F573D691A15CFA493C1782F451D4BD666E39A9E7B2EBE287E6A72C6B6")
.setAndroidContext(getApplicationContext())
.build();
A TTS manager does not need to be explicitly started to do its job, but when
it is no longer needed (or when certain Android lifecycle events occur), it's
a good idea to call release() to free any resources it's holding. If
an output class is specified, this is critical, as it may be holding a
prepared Android media player.
Once released, an explicit call to prepare() is required to
reallocate a manager's resources.
| Modifier and Type | Class and Description |
|---|---|
static class |
TTSManager.Builder
TTS manager builder.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addListener(TTSListener listener)
Add a new listener to receive events from the TTS subsystem.
|
void |
close() |
void |
eventReceived(TTSEvent event)
A notification that a TTS event has occurred.
|
SpeechOutput |
getOutput()
Get the current speech output component.
|
TTSService |
getTtsService()
Get the current TTS service.
|
void |
prepare()
Dynamically constructs TTS component classes, allocating any resources
they control.
|
void |
release()
Stops activity in the TTS subsystem and releases any resources held by
its components.
|
void |
removeListener(TTSListener listener)
Remove a TTS listener, allowing it to be garbage collected.
|
void |
setAndroidContext(android.content.Context androidContext)
Sets the android context for the TTS Subsystem.
|
void |
stopPlayback()
Stops playback of any playing or queued synthesis results.
|
void |
synthesize(SynthesisRequest request)
Synthesizes a piece of text or SSML, dispatching the result to any
registered listeners.
|
public TTSService getTtsService()
public SpeechOutput getOutput()
public void synthesize(SynthesisRequest request)
request - The synthesis request data.public void stopPlayback()
public void setAndroidContext(android.content.Context androidContext)
androidContext - the android context for the TTS subsystem.SpokestackTTSOutputpublic void addListener(TTSListener listener)
listener - The listener to add.public void removeListener(TTSListener listener)
listener - The listener to remove.public void close()
close in interface AutoCloseablepublic void prepare()
throws Exception
release()} or close().Exception - If there is an error constructing TTS components.public void release()
Once released, an explicit call to prepare() is required to
reallocate a manager's resources.
public void eventReceived(@NonNull
TTSEvent event)
TTSListenereventReceived in interface TTSListenerevent - The event from the TTS system.Copyright © 2021. All rights reserved.