public class SpokestackTTSOutput extends SpeechOutput implements com.google.android.exoplayer2.Player.EventListener, android.media.AudioManager.OnAudioFocusChangeListener, androidx.lifecycle.DefaultLifecycleObserver
The Spokestack audio player uses ExoPlayer to handle automatic playback of TTS responses. It responds to events in the Android component lifecycle, pausing and resuming itself along with its host app's activities.
Note that this audio player does not provide a UI. It is designed to be used
within a TTS subsystem controlled by a TTSManager
in an app that
wants to delegate all media management to Spokestack; if fine control over
playback is desired, consider adding a TTSListener
to the TTSManager
and managing audio via its methods.
Additionally, this component requires an Android Context
to be
attached to the manager that has created it. If the manager is meant to
persist across different Activity
s, the Context
used must
either be the application context, or it must be re-set on the
manager when the Activity context changes.
Constructor and Description |
---|
SpokestackTTSOutput(SpeechConfig config)
Creates a new audio output component.
|
Modifier and Type | Method and Description |
---|---|
void |
audioReceived(AudioResponse response)
Notifies the component that audio is available for immediate playback or
caching at the specified location.
|
void |
close() |
com.google.android.exoplayer2.ExoPlayer |
getMediaPlayer()
Gets the current media player instance.
|
void |
onAudioFocusChange(int focusChange) |
void |
onCreate(androidx.lifecycle.LifecycleOwner owner) |
void |
onDestroy(androidx.lifecycle.LifecycleOwner owner) |
void |
onIsPlayingChanged(boolean isPlaying) |
void |
onLoadingChanged(boolean isLoading) |
void |
onPause(androidx.lifecycle.LifecycleOwner owner) |
void |
onPlaybackParametersChanged(com.google.android.exoplayer2.PlaybackParameters playbackParameters) |
void |
onPlaybackSuppressionReasonChanged(int playbackSuppressionReason) |
void |
onPlayerError(com.google.android.exoplayer2.ExoPlaybackException error) |
void |
onPlayerStateChanged(boolean playWhenReady,
int playbackState) |
void |
onPositionDiscontinuity(int reason) |
void |
onRepeatModeChanged(int repeatMode) |
void |
onResume(androidx.lifecycle.LifecycleOwner owner) |
void |
onSeekProcessed() |
void |
onShuffleModeEnabledChanged(boolean shuffleModeEnabled) |
void |
onStart(androidx.lifecycle.LifecycleOwner owner) |
void |
onStop(androidx.lifecycle.LifecycleOwner owner) |
void |
onTimelineChanged(com.google.android.exoplayer2.Timeline timeline,
int reason) |
void |
onTimelineChanged(com.google.android.exoplayer2.Timeline timeline,
Object manifest,
int reason) |
void |
onTracksChanged(com.google.android.exoplayer2.source.TrackGroupArray trackGroups,
com.google.android.exoplayer2.trackselection.TrackSelectionArray trackSelections) |
void |
pauseContent()
Pause playback of any current content, storing the player's state
internally for later resumption.
|
void |
playContent()
Start or resume playback of any TTS responses.
|
void |
prepare()
Establish the media player and allocate its internal resources.
|
void |
setAndroidContext(android.content.Context androidContext)
Sets the output's Android context.
|
void |
stopPlayback()
Stops playback of any currently playing and queued synthesis results and
clears the play queue.
|
eventReceived
addListener, dispatch
public SpokestackTTSOutput(SpeechConfig config)
config
- A configuration object. This class does not require any
configuration properties, but this constructor is required
for participation in the TTS subsystem.public void setAndroidContext(@NonNull android.content.Context androidContext)
SpeechOutput
setAndroidContext
in class SpeechOutput
androidContext
- The Android context.@Nullable public com.google.android.exoplayer2.ExoPlayer getMediaPlayer()
public void prepare()
public void close()
close
in interface AutoCloseable
public void audioReceived(AudioResponse response)
SpeechOutput
audioReceived
in class SpeechOutput
response
- The TTS response containing the URI of the synthesized
audio.public void stopPlayback()
SpeechOutput
stopPlayback
in class SpeechOutput
public void onPlayerStateChanged(boolean playWhenReady, int playbackState)
onPlayerStateChanged
in interface com.google.android.exoplayer2.Player.EventListener
public void onPlayerError(@NotNull com.google.android.exoplayer2.ExoPlaybackException error)
onPlayerError
in interface com.google.android.exoplayer2.Player.EventListener
public void onAudioFocusChange(int focusChange)
onAudioFocusChange
in interface android.media.AudioManager.OnAudioFocusChangeListener
public void onResume(@NonNull androidx.lifecycle.LifecycleOwner owner)
onResume
in interface androidx.lifecycle.DefaultLifecycleObserver
public void onStop(@NonNull androidx.lifecycle.LifecycleOwner owner)
onStop
in interface androidx.lifecycle.DefaultLifecycleObserver
public void playContent()
public void pauseContent()
public void onCreate(@NonNull androidx.lifecycle.LifecycleOwner owner)
onCreate
in interface androidx.lifecycle.DefaultLifecycleObserver
public void onStart(@NonNull androidx.lifecycle.LifecycleOwner owner)
onStart
in interface androidx.lifecycle.DefaultLifecycleObserver
public void onPause(@NonNull androidx.lifecycle.LifecycleOwner owner)
onPause
in interface androidx.lifecycle.DefaultLifecycleObserver
public void onDestroy(@NonNull androidx.lifecycle.LifecycleOwner owner)
onDestroy
in interface androidx.lifecycle.DefaultLifecycleObserver
public void onTimelineChanged(@NotNull com.google.android.exoplayer2.Timeline timeline, int reason)
onTimelineChanged
in interface com.google.android.exoplayer2.Player.EventListener
public void onTimelineChanged(@NotNull com.google.android.exoplayer2.Timeline timeline, @Nullable Object manifest, int reason)
onTimelineChanged
in interface com.google.android.exoplayer2.Player.EventListener
public void onTracksChanged(@NotNull com.google.android.exoplayer2.source.TrackGroupArray trackGroups, @NotNull com.google.android.exoplayer2.trackselection.TrackSelectionArray trackSelections)
onTracksChanged
in interface com.google.android.exoplayer2.Player.EventListener
public void onLoadingChanged(boolean isLoading)
onLoadingChanged
in interface com.google.android.exoplayer2.Player.EventListener
public void onPlaybackSuppressionReasonChanged(int playbackSuppressionReason)
onPlaybackSuppressionReasonChanged
in interface com.google.android.exoplayer2.Player.EventListener
public void onIsPlayingChanged(boolean isPlaying)
onIsPlayingChanged
in interface com.google.android.exoplayer2.Player.EventListener
public void onRepeatModeChanged(int repeatMode)
onRepeatModeChanged
in interface com.google.android.exoplayer2.Player.EventListener
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled)
onShuffleModeEnabledChanged
in interface com.google.android.exoplayer2.Player.EventListener
public void onPositionDiscontinuity(int reason)
onPositionDiscontinuity
in interface com.google.android.exoplayer2.Player.EventListener
public void onPlaybackParametersChanged(@NotNull com.google.android.exoplayer2.PlaybackParameters playbackParameters)
onPlaybackParametersChanged
in interface com.google.android.exoplayer2.Player.EventListener
public void onSeekProcessed()
onSeekProcessed
in interface com.google.android.exoplayer2.Player.EventListener
Copyright © 2020. All rights reserved.