DefaultAudioSink
@UnstableApi
class DefaultAudioSink : AudioSink
Plays audio data. The implementation delegates to an AudioOutput 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 |
|
This interface is deprecated. |
|
Provides the buffer size to use when creating an |
interface DefaultAudioSink.AudioTrackProviderThis interface is deprecated. Use |
class DefaultAudioSink.BuilderA builder to create |
|
The default audio processor chain, which applies a (possibly empty) chain of user-defined audio processors followed by |
@DocumentedOutput mode of the audio sink. |
Constants |
|
|---|---|
const Float |
DEFAULT_PLAYBACK_SPEED = 1.0fThe default playback speed. |
const Float |
MAX_PITCH = 8.0fThe maximum allowed pitch factor. |
const Float |
MAX_PLAYBACK_SPEED = 8.0fThe maximum allowed playback speed. |
const Float |
MIN_PITCH = 0.1fThe minimum allowed pitch factor. |
const Float |
MIN_PLAYBACK_SPEED = 0.1fThe 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 |
OUTPUT_MODE_PCM = 0The audio sink plays PCM audio. |
Public functions |
|
|---|---|
Unit |
Configures (or reconfigures) the sink. |
Unit |
Disables tunneling. |
Unit |
Enables tunneling, if possible. |
Unit |
flush()Flushes the sink, after which it is ready to receive buffers from a new playback position. |
Long |
Returns the size of the underlying |
Long |
getCurrentPositionUs(sourceEnded: Boolean)Returns the playback position in the stream starting at zero, in microseconds, or |
AudioOffloadSupport! |
getFormatOffloadSupport(format: Format!)Returns the level of offload support that the sink can provide for a given |
Int |
Returns the level of support that the sink provides for a given |
Boolean |
handleBuffer(Attempts to process data from a |
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 |
isEnded()Returns whether |
Unit |
pause()Pauses playback. |
Unit |
play()Starts or resumes consuming audio if initialized. |
Unit |
Processes any remaining data. |
Unit |
release()Releases the audio sink. |
Unit |
reset()Resets the sink, releasing any resources that it currently holds. |
Unit |
setAudioOutputProvider(audioOutputProvider: AudioOutputProvider!)Sets the |
Unit |
setAudioSessionId(audioSessionId: Int)Sets the audio session id. |
Unit |
setAuxEffectInfo(auxEffectInfo: AuxEffectInfo!)Sets the auxiliary effect. |
Unit |
Sets the |
Unit |
setListener(listener: AudioSink.Listener!)Sets the listener for sink events, which should be the audio renderer. |
Unit |
@RequiresApi(value = 29)Sets offload delay padding on the |
Unit |
@RequiresApi(value = 29)Sets audio offload mode, if possible. |
Unit |
setPlayerId(playerId: PlayerId?)Sets the |
Unit |
setPreferredDevice(audioDeviceInfo: AudioDeviceInfo?)Sets the preferred audio device. |
Unit |
setVirtualDeviceId(virtualDeviceId: Int)Sets the virtual device id. |
Unit |
Sets the playback volume. |
Boolean |
supportsFormat(format: Format!)Returns whether the sink supports a given |
Public properties |
|
|---|---|
AudioAttributes! |
|
PlaybackParameters! |
|
Boolean |
Inherited Constants |
||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Inherited functions |
||
|---|---|---|
|
Constants
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.
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 |
| 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.
getAudioTrackBufferSizeUs
fun getAudioTrackBufferSizeUs(): Long
Returns the size of the underlying AudioTrack buffer in microseconds. If unsupported or the AudioTrack is not initialized then return TIME_UNSET.
If the AudioTrack is configured with a compressed encoding, then the returned duration is an estimated minimum based on the encoding's maximum encoded byte rate.
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 |
| 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.
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. |
setAudioOutputProvider
fun setAudioOutputProvider(audioOutputProvider: AudioOutputProvider!): Unit
Sets the AudioOutputProvider to use as the output path.
setAuxEffectInfo
fun setAuxEffectInfo(auxEffectInfo: AuxEffectInfo!): Unit
Sets the auxiliary effect.
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.
setPreferredDevice
fun setPreferredDevice(audioDeviceInfo: AudioDeviceInfo?): Unit
Sets the preferred audio device.
| Parameters | |
|---|---|
audioDeviceInfo: AudioDeviceInfo? |
The preferred |
setVirtualDeviceId
fun setVirtualDeviceId(virtualDeviceId: Int): Unit
Sets the virtual device id.
| Parameters | |
|---|---|
virtualDeviceId: Int |
The |