public interface AudioController
AudioController provides a way to get access to the audio data and implement a custom audio playback logic.
If an object that implements this interface is passed to the SpotifyPlayer
during creation
it will override the default AudioController and it will receive PCM data in the
onAudioDataDelivered(short[], int, int, int)
callback. The controller is responsible for
handling the data as well as onAudioFlush()
calls that will occur every time the
audio buffer should be emptied.
Important: onAudioDataDelivered(short[], int, int, int)
and onAudioFlush()
are synchronous
and therefore blocking. Any long-running operations inside these methods will most likely result
in a choppy playback. One of the possible ways of handling this
is to only write to an audio buffer in the onAudioDataDelivered(short[], int, int, int)
method
and play the audio in a separate thread. AudioRingBuffer
can be
used for that purpose.
Modifier and Type | Method and Description |
---|---|
int |
onAudioDataDelivered(short[] samples,
int sampleCount,
int sampleRate,
int channels)
Called whenever
SpotifyPlayer receives audio data. |
void |
onAudioFlush()
Called when the
SpotifyPlayer has flushed the audio buffer. |
void |
onAudioPaused()
Called when audio playback should be paused (e.g.
|
void |
onAudioResumed()
Called when audio playback should be resumed (e.g.
|
void |
start()
Called when the controller is initialized by the
SpotifyPlayer . |
void |
stop()
Called when the controller is stopped by the
SpotifyPlayer . |
void start()
SpotifyPlayer
.
Happens only once - when SpotifyPlayer
is created.
This method should contain all set-up related logic such as
starting a thread.void stop()
SpotifyPlayer
.
Happens only once - when SpotifyPlayer
is destroyed.
This method should contain all tear-down related logic
such as stopping the thread.int onAudioDataDelivered(short[] samples, int sampleCount, int sampleRate, int channels)
SpotifyPlayer
receives audio data.
This method is synchronous and therefore blocking.
Any long running operations will affect playback quality.samples
- 16-bit PCM data. The buffer contains sampleCount samples, whereby each sample contains the data
for a single audio channel.sampleCount
- Number of samples in samples buffer.sampleRate
- Sample rate in Hz (such as 22050, 44100 or 48000).channels
- Number of channels (1 = mono, 2 = stereo).void onAudioFlush()
SpotifyPlayer
has flushed the audio buffer.
Any buffered frames waiting to be played should be discarded.
This method is synchronous and therefore blocking, so any
long-running operations will affect playback quality.void onAudioPaused()
void onAudioResumed()