@UnstableApi
interface AudioOutput

Known direct subclasses
ForwardingAudioOutput

A forwarding base class that delegates all calls to the provided AudioOutput.


An interface to wrap an object that can play audio, like an AudioTrack.

Summary

Nested types

Listener for AudioOutput events.

Thrown when a failure occurs writing to the output.

Public functions

Unit

Adds a Listener for audio output events.

Unit
attachAuxEffect(effectId: Int)

Attaches an auxiliary effect to the output.

Unit

Flushes the audio data from the output.

Int

Returns the audio session ID or AUDIO_SESSION_ID_UNSET if unknown.

Long

Returns the buffer size in frames of the underlying AudioOutput.

PlaybackParameters!

Returns the playback parameters.

Long

Returns the current playout timestamp in microseconds.

Int

Returns the sample rate of the underlying AudioOutput.

Boolean

Returns whether this output is playing back in offload mode.

Boolean

Returns whether the audio output is stalled.

Unit

Pauses playback.

Unit

Starts or resumes playing audio.

Unit

Releases the AudioOutput.

Unit

Removes a Listener for audio output events.

Unit

Sets the send level for the auxiliary effect.

Unit
setOffloadDelayPadding(delayInFrames: Int, paddingInFrames: Int)

Sets the offload delay and padding.

Unit

Notifies the output that this is the end of the stream.

Unit

Sets the playback parameters.

Unit
setPlayerId(playerId: PlayerId!)

Sets the PlayerId on the audio output.

Unit

Sets the preferred audio device for routing.

Unit
setVolume(volume: Float)

Sets the volume of the AudioOutput.

Unit

Signal that the last buffer has been written and playback should stop after playing out all remaining buffers

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

Writes audio data to the audio output for playback with a presentation timestamp.

Public functions

addListener

fun addListener(listener: AudioOutput.Listener!): Unit

Adds a Listener for audio output events.

attachAuxEffect

fun attachAuxEffect(effectId: Int): Unit

Attaches an auxiliary effect to the output.

flush

fun flush(): Unit

Flushes the audio data from the output.

getAudioSessionId

fun getAudioSessionId(): Int

Returns the audio session ID or AUDIO_SESSION_ID_UNSET if unknown.

getBufferSizeInFrames

fun getBufferSizeInFrames(): Long

Returns the buffer size in frames of the underlying AudioOutput.

getPlaybackParameters

fun getPlaybackParameters(): PlaybackParameters!

Returns the playback parameters.

getPositionUs

fun getPositionUs(): Long

Returns the current playout timestamp in microseconds.

getSampleRate

fun getSampleRate(): Int

Returns the sample rate of the underlying AudioOutput.

isOffloadedPlayback

fun isOffloadedPlayback(): Boolean

Returns whether this output is playing back in offload mode.

isStalled

fun isStalled(): Boolean

Returns whether the audio output is stalled.

pause

fun pause(): Unit

Pauses playback.

play

fun play(): Unit

Starts or resumes playing audio.

release

fun release(): Unit

Releases the AudioOutput.

removeListener

fun removeListener(listener: AudioOutput.Listener!): Unit

Removes a Listener for audio output events.

setAuxEffectSendLevel

fun setAuxEffectSendLevel(level: Float): Unit

Sets the send level for the auxiliary effect.

setOffloadDelayPadding

fun setOffloadDelayPadding(delayInFrames: Int, paddingInFrames: Int): Unit

Sets the offload delay and padding.

setOffloadEndOfStream

fun setOffloadEndOfStream(): Unit

Notifies the output that this is the end of the stream.

setPlaybackParameters

fun setPlaybackParameters(playbackParams: PlaybackParameters!): Unit

Sets the playback parameters.

setPlayerId

fun setPlayerId(playerId: PlayerId!): Unit

Sets the PlayerId on the audio output.

setPreferredDevice

fun setPreferredDevice(preferredDevice: AudioDeviceInfo?): Unit

Sets the preferred audio device for routing.

setVolume

fun setVolume(volume: Float): Unit

Sets the volume of the AudioOutput.

stop

fun stop(): Unit

Signal that the last buffer has been written and playback should stop after playing out all remaining buffers

write

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

Writes audio data to the audio output for playback with a presentation timestamp.

The provided buffer must be a direct byte buffer with native byte order. The method is read-only and will not modify the bytes. It processes data from buffer.position() to buffer.limit() (exclusive). Every call will advance buffer.position() to reflect the number of bytes written.

The first time this method is called with a new buffer, the buffer must contain one or more complete encoded access units or complete PCM frames.

If this method returns false, the same buffer must be used for subsequent calls to this method until it has been fully consumed, or until an intervening call to flush.

Parameters
buffer: ByteBuffer!

The buffer containing the audio data.

encodedAccessUnitCount: Int

The number of encoded access units in the buffer when the buffer was first used in this method, or 1 if the buffer contains PCM audio. When calling this method again after it returned false, the same number of encoded access units must be provided.

presentationTimeUs: Long

The presentation timestamp, in microseconds, of the first access unit or PCM frame when the buffer was first used in this method. When calling this method again after it returned false, the same timestamp must be provided.

Returns
Boolean

Whether the buffer was handled fully.

Throws
androidx.media3.exoplayer.audio.AudioOutput.WriteException

If the write fails. The audio output cannot be reused after a failure and needs to be recreated.