DefaultAudioSink


@UnstableApi
class DefaultAudioSink : AudioSink


Plays audio data. The implementation delegates to an AudioTrack and handles playback position smoothing, non-blocking writes and reconfiguration.

If tunneling mode is enabled, care must be taken that audio processors do not output buffers with a different duration than their input, and buffer processors must produce output corresponding to their last input immediately after that input is queued. This means that, for example, speed adjustment is not possible while using tunneling.

Summary

Nested types

Provides the AudioOffloadSupport to convey the level of offload support the sink can provide.

This interface is deprecated.

Use androidx.media3.common.audio.AudioProcessorChain.

Provides the buffer size to use when creating an AudioTrack.

A builder to create DefaultAudioSink instances.

The default audio processor chain, which applies a (possibly empty) chain of user-defined audio processors followed by SilenceSkippingAudioProcessor and SonicAudioProcessor.

Thrown when the audio track has provided a spurious timestamp, if failOnSpuriousAudioTimestamp is set.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = TYPE_USE)
@IntDef(value = )
annotation DefaultAudioSink.OutputMode

Output mode of the audio sink.

Constants

const Float

The default playback speed.

const Float
MAX_PITCH = 8.0f

The maximum allowed pitch factor.

const Float

The maximum allowed playback speed.

const Float
MIN_PITCH = 0.1f

The minimum allowed pitch factor.

const Float

The minimum allowed playback speed.

const Int

The audio sink plays encoded audio in offload.

const Int

The audio sink plays encoded audio in passthrough.

const Int

The audio sink plays PCM audio.

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.

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.

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
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
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
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
setVolume(volume: Float)

Sets the playback volume.

Boolean

Returns whether the sink supports a given Format.

Inherited Constants

From androidx.media3.exoplayer.audio.AudioSink
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.

Inherited functions

From androidx.media3.exoplayer.audio.AudioSink
Unit
setOutputStreamOffsetUs(outputStreamOffsetUs: Long)

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

Constants

DEFAULT_PLAYBACK_SPEED

const val DEFAULT_PLAYBACK_SPEED = 1.0f: Float

The default playback speed.

MAX_PITCH

const val MAX_PITCH = 8.0f: Float

The maximum allowed pitch factor. Higher values will be constrained to fall in range.

MAX_PLAYBACK_SPEED

const val MAX_PLAYBACK_SPEED = 8.0f: Float

The maximum allowed playback speed. Higher values will be constrained to fall in range.

MIN_PITCH

const val MIN_PITCH = 0.1f: Float

The minimum allowed pitch factor. Lower values will be constrained to fall in range.

MIN_PLAYBACK_SPEED

const val MIN_PLAYBACK_SPEED = 0.1f: Float

The minimum allowed playback speed. Lower values will be constrained to fall in range.

OUTPUT_MODE_OFFLOAD

const val OUTPUT_MODE_OFFLOAD = 1: Int

The audio sink plays encoded audio in offload.

OUTPUT_MODE_PASSTHROUGH

const val OUTPUT_MODE_PASSTHROUGH = 2: Int

The audio sink plays encoded audio in passthrough.

OUTPUT_MODE_PCM

const val OUTPUT_MODE_PCM = 0: Int

The audio sink plays PCM audio.

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.

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.

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.

onAudioCapabilitiesChanged

fun onAudioCapabilitiesChanged(audioCapabilities: AudioCapabilities!): Unit

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.

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.

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.

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.

Public properties

audioAttributes

var audioAttributesAudioAttributes!

failOnSpuriousAudioTimestamp

java-static val failOnSpuriousAudioTimestampBoolean

Whether to throw an InvalidAudioTrackTimestampException when a spurious timestamp is reported from getTimestamp.

The flag must be set before creating a player. Should be set to true for testing and debugging purposes only.

playbackParameters

var playbackParametersPlaybackParameters!

skipSilenceEnabled

var skipSilenceEnabledBoolean