@UnstableApi
interface AudioSink

Known direct subclasses
DefaultAudioSink

Plays audio data.

ForwardingAudioSink

An overridable AudioSink implementation forwarding all methods to another sink.

Known indirect subclasses
CapturingAudioSink

A ForwardingAudioSink that captures configuration, discontinuity and buffer events.

OggFileAudioBufferSink

A sink for audio buffers that writes output audio as .ogg files with a given path prefix.


A sink that consumes audio data.

Before starting playback, specify the input audio format by calling configure.

Call handleBuffer to write data, and handleDiscontinuity when the data being fed is discontinuous. Call play to start playing the written data.

Call configure whenever the input format changes. The sink will be reinitialized on the next call to handleBuffer.

Call flush to prepare the sink to receive audio data from a new playback position.

Call playToEndOfStream repeatedly to play out all data when no more input buffers will be provided via handleBuffer until the next flush. Call reset when the instance is no longer required.

The implementation may be backed by a platform AudioTrack. In this case, setAudioSessionId, setAudioAttributes, enableTunnelingV21 and disableTunneling may be called before writing data to the sink. These methods may also be called after writing data to the sink, in which case it will be reinitialized as required. For implementations that are not based on platform s, calling methods relating to audio sessions, audio attributes, and tunneling may have no effect.

Summary

Nested types

Configuration parameters used for an AudioTrack.

Thrown when a failure occurs configuring the sink.

Thrown when a failure occurs initializing the sink.

Listener for audio sink events.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = TYPE_USE)
@IntDef(value = )
annotation AudioSink.OffloadMode

Audio offload mode configuration.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = TYPE_USE)
@IntDef(value = )
annotation AudioSink.SinkFormatSupport

The level of support the sink provides for a format.

Thrown when the sink encounters an unexpected timestamp discontinuity.

Thrown when a failure occurs writing to the sink.

Constants

const Long
CURRENT_POSITION_NOT_SET = -9223372036854775808

Returned by getCurrentPositionUs when the position is not set.

const Int

The audio sink will never play in offload mode.

const Int

The audio sink will prefer offload playback even if this might result in silence gaps between tracks.

const Int

The audio sink will prefer offload playback except in the case where both the track is gapless and the device does support gapless offload playback.

const Int

The sink supports the format directly, without the need for internal transcoding.

const Int

The sink supports the format, but needs to transcode it internally to do so.

const Int

The sink does not support the format.

Public functions

Unit
configure(
    inputFormat: Format!,
    specifiedBufferSize: Int,
    outputChannels: IntArray?
)

Configures (or reconfigures) the sink.

Unit

Disables tunneling.

Unit

Enables tunneling, if possible.

Unit

Flushes the sink, after which it is ready to receive buffers from a new playback position.

AudioAttributes?

Returns the audio attributes used for audio playback, or null if the sink does not use audio attributes.

Long

Returns the playback position in the stream starting at zero, in microseconds, or CURRENT_POSITION_NOT_SET if it is not yet available.

AudioOffloadSupport!

Returns the level of offload support that the sink can provide for a given Format.

Int

Returns the level of support that the sink provides for a given Format.

PlaybackParameters!

Returns the active PlaybackParameters.

Boolean

Returns whether silences are skipped in the audio stream.

Boolean
handleBuffer(
    buffer: ByteBuffer!,
    presentationTimeUs: Long,
    encodedAccessUnitCount: Int
)

Attempts to process data from a ByteBuffer, starting from its current position and ending at its limit (exclusive).

Unit

Signals to the sink that the next buffer may be discontinuous with the previous buffer.

Boolean

Returns whether the sink has data pending that has not been consumed yet.

Boolean

Returns whether playToEndOfStream has been called and all buffers have been processed.

Unit

Pauses playback.

Unit

Starts or resumes consuming audio if initialized.

Unit

Processes any remaining data.

Unit

Releases the audio sink.

Unit

Resets the sink, releasing any resources that it currently holds.

Unit

Sets attributes for audio playback.

Unit
setAudioSessionId(audioSessionId: Int)

Sets the audio session id.

Unit

Sets the auxiliary effect.

Unit
setClock(clock: Clock!)

Sets the Clock to use for timing in this audio sink.

Unit

Sets the listener for sink events, which should be the audio renderer.

Unit
@RequiresApi(value = 29)
setOffloadDelayPadding(delayInFrames: Int, paddingInFrames: Int)

Sets offload delay padding on the AudioTrack, if possible.

Unit
@RequiresApi(value = 29)
setOffloadMode(@AudioSink.OffloadMode offloadMode: Int)

Sets audio offload mode, if possible.

Unit
setOutputStreamOffsetUs(outputStreamOffsetUs: Long)

Sets the offset that is added to the media timestamp before it is passed as presentationTimeUs in handleBuffer.

Unit

Attempts to set the playback parameters.

Unit
setPlayerId(playerId: PlayerId?)

Sets the PlayerId of the player using this audio sink.

Unit
@RequiresApi(value = 23)
setPreferredDevice(audioDeviceInfo: AudioDeviceInfo?)

Sets the preferred audio device.

Unit
setSkipSilenceEnabled(skipSilenceEnabled: Boolean)

Sets whether silences should be skipped in the audio stream.

Unit
setVolume(volume: Float)

Sets the playback volume.

Boolean

Returns whether the sink supports a given Format.

Constants

CURRENT_POSITION_NOT_SET

const val CURRENT_POSITION_NOT_SET = -9223372036854775808: Long

Returned by getCurrentPositionUs when the position is not set.

OFFLOAD_MODE_DISABLED

const val OFFLOAD_MODE_DISABLED = 0: Int

The audio sink will never play in offload mode.

OFFLOAD_MODE_ENABLED_GAPLESS_NOT_REQUIRED

const val OFFLOAD_MODE_ENABLED_GAPLESS_NOT_REQUIRED = 2: Int

The audio sink will prefer offload playback even if this might result in silence gaps between tracks.

Use this option to prioritize battery saving at the cost of a possible non seamless transitions between tracks of the same album.

OFFLOAD_MODE_ENABLED_GAPLESS_REQUIRED

const val OFFLOAD_MODE_ENABLED_GAPLESS_REQUIRED = 1: Int

The audio sink will prefer offload playback except in the case where both the track is gapless and the device does support gapless offload playback.

Use this option to prioritize uninterrupted playback of consecutive audio tracks over power savings.

SINK_FORMAT_SUPPORTED_DIRECTLY

const val SINK_FORMAT_SUPPORTED_DIRECTLY = 2: Int

The sink supports the format directly, without the need for internal transcoding.

SINK_FORMAT_SUPPORTED_WITH_TRANSCODING

const val SINK_FORMAT_SUPPORTED_WITH_TRANSCODING = 1: Int

The sink supports the format, but needs to transcode it internally to do so. Internal transcoding may result in lower quality and higher CPU load in some cases.

SINK_FORMAT_UNSUPPORTED

const val SINK_FORMAT_UNSUPPORTED = 0: Int

The sink does not support the format.

Public functions

configure

fun configure(
    inputFormat: Format!,
    specifiedBufferSize: Int,
    outputChannels: IntArray?
): Unit

Configures (or reconfigures) the sink.

Parameters
inputFormat: Format!

The format of audio data provided in the input buffers.

specifiedBufferSize: Int

A specific size for the playback buffer in bytes, or 0 to infer a suitable buffer size.

outputChannels: IntArray?

A mapping from input to output channels that is applied to this sink's input as a preprocessing step, if handling PCM input. Specify null to leave the input unchanged. Otherwise, the element at index i specifies index of the input channel to map to output channel i when preprocessing input buffers. After the map is applied the audio data will have outputChannels.length channels.

Throws
androidx.media3.exoplayer.audio.AudioSink.ConfigurationException

If an error occurs configuring the sink.

disableTunneling

fun disableTunneling(): Unit

Disables tunneling. If tunneling was previously enabled then the sink is reset and any audio session id is cleared.

enableTunnelingV21

fun enableTunnelingV21(): Unit

Enables tunneling, if possible. The sink is reset if tunneling was previously disabled. Enabling tunneling is only possible if the sink is based on a platform AudioTrack, and requires platform API version 21 onwards.

Throws
java.lang.IllegalStateException

Thrown if enabling tunneling on platform API version <21.

flush

fun flush(): Unit

Flushes the sink, after which it is ready to receive buffers from a new playback position.

The audio session may remain active until reset is called.

getAudioAttributes

fun getAudioAttributes(): AudioAttributes?

Returns the audio attributes used for audio playback, or null if the sink does not use audio attributes.

getCurrentPositionUs

fun getCurrentPositionUs(sourceEnded: Boolean): Long

Returns the playback position in the stream starting at zero, in microseconds, or CURRENT_POSITION_NOT_SET if it is not yet available.

Parameters
sourceEnded: Boolean

Specify true if no more input buffers will be provided.

Returns
Long

The playback position relative to the start of playback, in microseconds.

getFormatOffloadSupport

fun getFormatOffloadSupport(format: Format!): AudioOffloadSupport!

Returns the level of offload support that the sink can provide for a given Format.

Parameters
format: Format!

The format.

Returns
AudioOffloadSupport!

The level of support provided.

getFormatSupport

@AudioSink.SinkFormatSupport
fun getFormatSupport(format: Format!): Int

Returns the level of support that the sink provides for a given Format.

Parameters
format: Format!

The format.

Returns
Int

The level of support provided.

getPlaybackParameters

fun getPlaybackParameters(): PlaybackParameters!

Returns the active PlaybackParameters.

getSkipSilenceEnabled

fun getSkipSilenceEnabled(): Boolean

Returns whether silences are skipped in the audio stream.

handleBuffer

fun handleBuffer(
    buffer: ByteBuffer!,
    presentationTimeUs: Long,
    encodedAccessUnitCount: Int
): Boolean

Attempts to process data from a ByteBuffer, starting from its current position and ending at its limit (exclusive). The position of the ByteBuffer is advanced by the number of bytes that were handled. onPositionDiscontinuity will be called if presentationTimeUs is discontinuous with the last buffer handled since the last reset.

Returns whether the data was handled in full. If the data was not handled in full then the same ByteBuffer must be provided to subsequent calls until it has been fully consumed, except in the case of an intervening call to flush (or to configure that causes the sink to be flushed).

Parameters
buffer: ByteBuffer!

The buffer containing audio data.

presentationTimeUs: Long

The presentation timestamp of the buffer in microseconds.

encodedAccessUnitCount: Int

The number of encoded access units in the buffer, or 1 if the buffer contains PCM audio. This allows batching multiple encoded access units in one buffer.

Returns
Boolean

Whether the buffer was handled fully.

Throws
androidx.media3.exoplayer.audio.AudioSink.InitializationException

If an error occurs initializing the sink.

androidx.media3.exoplayer.audio.AudioSink.WriteException

If an error occurs writing the audio data.

handleDiscontinuity

fun handleDiscontinuity(): Unit

Signals to the sink that the next buffer may be discontinuous with the previous buffer.

hasPendingData

fun hasPendingData(): Boolean

Returns whether the sink has data pending that has not been consumed yet.

isEnded

fun isEnded(): Boolean

Returns whether playToEndOfStream has been called and all buffers have been processed.

pause

fun pause(): Unit

Pauses playback.

play

fun play(): Unit

Starts or resumes consuming audio if initialized.

playToEndOfStream

fun playToEndOfStream(): Unit

Processes any remaining data. isEnded will return true when no data remains.

Throws
androidx.media3.exoplayer.audio.AudioSink.WriteException

If an error occurs draining data to the sink.

release

fun release(): Unit

Releases the audio sink.

reset

fun reset(): Unit

Resets the sink, releasing any resources that it currently holds.

setAudioAttributes

fun setAudioAttributes(audioAttributes: AudioAttributes!): Unit

Sets attributes for audio playback. If the attributes have changed and if the sink is not configured for use with tunneling, then it is reset and the audio session id is cleared.

If the sink is configured for use with tunneling then the audio attributes are ignored. The sink is not reset and the audio session id is not cleared. The passed attributes will be used if the sink is later re-configured into non-tunneled mode.

Parameters
audioAttributes: AudioAttributes!

The attributes for audio playback.

setAudioSessionId

fun setAudioSessionId(audioSessionId: Int): Unit

Sets the audio session id.

setAuxEffectInfo

fun setAuxEffectInfo(auxEffectInfo: AuxEffectInfo!): Unit

Sets the auxiliary effect.

setClock

fun setClock(clock: Clock!): Unit

Sets the Clock to use for timing in this audio sink.

Parameters
clock: Clock!

The Clock.

setListener

fun setListener(listener: AudioSink.Listener!): Unit

Sets the listener for sink events, which should be the audio renderer.

Parameters
listener: AudioSink.Listener!

The listener for sink events, which should be the audio renderer.

setOffloadDelayPadding

@RequiresApi(value = 29)
fun setOffloadDelayPadding(delayInFrames: Int, paddingInFrames: Int): Unit

Sets offload delay padding on the AudioTrack, if possible. Setting the offload delay padding is only possible if the sink is based on a platform AudioTrack in offload mode. Also requires platform API version 29 onwards.

setOffloadMode

@RequiresApi(value = 29)
fun setOffloadMode(@AudioSink.OffloadMode offloadMode: Int): Unit

Sets audio offload mode, if possible. Enabling offload is only possible if the sink is based on a platform AudioTrack, and requires platform API version 29 onwards.

setOutputStreamOffsetUs

fun setOutputStreamOffsetUs(outputStreamOffsetUs: Long): Unit

Sets the offset that is added to the media timestamp before it is passed as presentationTimeUs in handleBuffer.

Parameters
outputStreamOffsetUs: Long

The output stream offset in microseconds.

setPlaybackParameters

fun setPlaybackParameters(playbackParameters: PlaybackParameters!): Unit

Attempts to set the playback parameters. The audio sink may override these parameters if they are not supported.

Parameters
playbackParameters: PlaybackParameters!

The new playback parameters to attempt to set.

setPlayerId

fun setPlayerId(playerId: PlayerId?): Unit

Sets the PlayerId of the player using this audio sink.

Parameters
playerId: PlayerId?

The PlayerId, or null to clear a previously set id.

setPreferredDevice

@RequiresApi(value = 23)
fun setPreferredDevice(audioDeviceInfo: AudioDeviceInfo?): Unit

Sets the preferred audio device.

Parameters
audioDeviceInfo: AudioDeviceInfo?

The preferred audio device, or null to restore the default.

setSkipSilenceEnabled

fun setSkipSilenceEnabled(skipSilenceEnabled: Boolean): Unit

Sets whether silences should be skipped in the audio stream.

setVolume

fun setVolume(volume: Float): Unit

Sets the playback volume.

Parameters
volume: Float

Linear output gain to apply to all channels. Should be in the range [0.0, 1.0].

supportsFormat

fun supportsFormat(format: Format!): Boolean

Returns whether the sink supports a given Format.

Parameters
format: Format!

The format.

Returns
Boolean

Whether the sink supports the format.