DefaultAudioSink
@UnstableApi
public final class DefaultAudioSink implements 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 |
|---|
public interface DefaultAudioSink.AudioOffloadSupportProviderProvides the |
public interface DefaultAudioSink.AudioProcessorChain extends AudioProcessorChainThis interface is deprecated. |
public interface DefaultAudioSink.AudioTrackBufferSizeProviderProvides the buffer size to use when creating an |
public interface DefaultAudioSink.AudioTrackProviderThis interface is deprecated. Use |
public final class DefaultAudioSink.BuilderA builder to create |
public class DefaultAudioSink.DefaultAudioProcessorChain implements DefaultAudioSink.AudioProcessorChainThe default audio processor chain, which applies a (possibly empty) chain of user-defined audio processors followed by |
@DocumentedOutput mode of the audio sink. |
Constants |
|
|---|---|
static final float |
DEFAULT_PLAYBACK_SPEED = 1.0fThe default playback speed. |
static final float |
MAX_PITCH = 8.0fThe maximum allowed pitch factor. |
static final float |
MAX_PLAYBACK_SPEED = 8.0fThe maximum allowed playback speed. |
static final float |
MIN_PITCH = 0.1fThe minimum allowed pitch factor. |
static final float |
MIN_PLAYBACK_SPEED = 0.1fThe minimum allowed playback speed. |
static final int |
The audio sink plays encoded audio in offload. |
static final int |
The audio sink plays encoded audio in passthrough. |
static final int |
OUTPUT_MODE_PCM = 0The audio sink plays PCM audio. |
Public fields |
|
|---|---|
AudioAttributes |
|
PlaybackParameters |
|
boolean |
Public methods |
|
|---|---|
void |
Configures (or reconfigures) the sink. |
void |
Disables tunneling. |
void |
Enables tunneling, if possible. |
void |
flush()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 |
long |
Returns the size of the underlying |
long |
getCurrentPositionUs(boolean sourceEnded)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 |
PlaybackParameters |
Returns the active |
boolean |
Returns whether silences are skipped in the audio stream. |
boolean |
handleBuffer(Attempts to process data from a |
void |
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 |
void |
pause()Pauses playback. |
void |
play()Starts or resumes consuming audio if initialized. |
void |
Processes any remaining data. |
void |
release()Releases the audio sink. |
void |
reset()Resets the sink, releasing any resources that it currently holds. |
void |
setAudioAttributes(AudioAttributes audioAttributes)Sets attributes for audio playback. |
void |
setAudioOutputProvider(AudioOutputProvider audioOutputProvider)Sets the |
void |
setAudioSessionId(int audioSessionId)Sets the audio session id. |
void |
setAuxEffectInfo(AuxEffectInfo auxEffectInfo)Sets the auxiliary effect. |
void |
Sets the |
void |
setListener(AudioSink.Listener listener)Sets the listener for sink events, which should be the audio renderer. |
void |
@RequiresApi(value = 29)Sets offload delay padding on the |
void |
@RequiresApi(value = 29)Sets audio offload mode, if possible. |
void |
setPlaybackParameters(PlaybackParameters playbackParameters)Attempts to set the playback parameters. |
void |
setPlayerId(@Nullable PlayerId playerId)Sets the |
void |
setPreferredDevice(@Nullable AudioDeviceInfo audioDeviceInfo)Sets the preferred audio device. |
void |
setSkipSilenceEnabled(boolean skipSilenceEnabled)Sets whether silences should be skipped in the audio stream. |
void |
setVirtualDeviceId(int virtualDeviceId)Sets the virtual device id. |
void |
setVolume(float volume)Sets the playback volume. |
boolean |
supportsFormat(Format format)Returns whether the sink supports a given |
Inherited Constants |
||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Inherited methods |
||
|---|---|---|
|
Constants
DEFAULT_PLAYBACK_SPEED
public static final float DEFAULT_PLAYBACK_SPEED = 1.0f
The default playback speed.
MAX_PITCH
public static final float MAX_PITCH = 8.0f
The maximum allowed pitch factor. Higher values will be constrained to fall in range.
MAX_PLAYBACK_SPEED
public static final float MAX_PLAYBACK_SPEED = 8.0f
The maximum allowed playback speed. Higher values will be constrained to fall in range.
MIN_PITCH
public static final float MIN_PITCH = 0.1f
The minimum allowed pitch factor. Lower values will be constrained to fall in range.
MIN_PLAYBACK_SPEED
public static final float MIN_PLAYBACK_SPEED = 0.1f
The minimum allowed playback speed. Lower values will be constrained to fall in range.
OUTPUT_MODE_OFFLOAD
public static final int OUTPUT_MODE_OFFLOAD = 1
The audio sink plays encoded audio in offload.
OUTPUT_MODE_PASSTHROUGH
public static final int OUTPUT_MODE_PASSTHROUGH = 2
The audio sink plays encoded audio in passthrough.
Public fields
Public methods
configure
public void configure(
Format inputFormat,
int specifiedBufferSize,
@Nullable int[] outputChannels
)
Configures (or reconfigures) the sink.
| Parameters | |
|---|---|
Format inputFormat |
The format of audio data provided in the input buffers. |
int specifiedBufferSize |
A specific size for the playback buffer in bytes, or 0 to infer a suitable buffer size. |
@Nullable int[] outputChannels |
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
public void disableTunneling()
Disables tunneling. If tunneling was previously enabled then the sink is reset and any audio session id is cleared.
enableTunnelingV21
public void enableTunnelingV21()
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
public void flush()
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
public AudioAttributes getAudioAttributes()
Returns the audio attributes used for audio playback, or null if the sink does not use audio attributes.
getAudioTrackBufferSizeUs
public long getAudioTrackBufferSizeUs()
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
public long getCurrentPositionUs(boolean sourceEnded)
Returns the playback position in the stream starting at zero, in microseconds, or CURRENT_POSITION_NOT_SET if it is not yet available.
| Parameters | |
|---|---|
boolean sourceEnded |
Specify |
| Returns | |
|---|---|
long |
The playback position relative to the start of playback, in microseconds. |
getFormatOffloadSupport
public AudioOffloadSupport getFormatOffloadSupport(Format format)
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
public int getFormatSupport(Format format)
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
public PlaybackParameters getPlaybackParameters()
Returns the active PlaybackParameters.
getSkipSilenceEnabled
public boolean getSkipSilenceEnabled()
Returns whether silences are skipped in the audio stream.
handleBuffer
public boolean handleBuffer(
ByteBuffer buffer,
long presentationTimeUs,
int encodedAccessUnitCount
)
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 | |
|---|---|
ByteBuffer buffer |
The buffer containing audio data. |
long presentationTimeUs |
The presentation timestamp of the buffer in microseconds. |
int encodedAccessUnitCount |
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
public void handleDiscontinuity()
Signals to the sink that the next buffer may be discontinuous with the previous buffer.
hasPendingData
public boolean hasPendingData()
Returns whether the sink has data pending that has not been consumed yet.
isEnded
public boolean isEnded()
Returns whether playToEndOfStream has been called and all buffers have been processed.
playToEndOfStream
public void playToEndOfStream()
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. |
setAudioAttributes
public void setAudioAttributes(AudioAttributes audioAttributes)
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. |
setAudioOutputProvider
public void setAudioOutputProvider(AudioOutputProvider audioOutputProvider)
Sets the AudioOutputProvider to use as the output path.
setAuxEffectInfo
public void setAuxEffectInfo(AuxEffectInfo auxEffectInfo)
Sets the auxiliary effect.
setListener
public void setListener(AudioSink.Listener listener)
Sets the listener for sink events, which should be the audio renderer.
| Parameters | |
|---|---|
AudioSink.Listener listener |
The listener for sink events, which should be the audio renderer. |
setOffloadDelayPadding
@RequiresApi(value = 29)
public void setOffloadDelayPadding(int delayInFrames, int paddingInFrames)
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)
public void setOffloadMode(@AudioSink.OffloadMode int offloadMode)
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.
setPlaybackParameters
public void setPlaybackParameters(PlaybackParameters playbackParameters)
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
public void setPlayerId(@Nullable PlayerId playerId)
Sets the PlayerId of the player using this audio sink.
setPreferredDevice
public void setPreferredDevice(@Nullable AudioDeviceInfo audioDeviceInfo)
Sets the preferred audio device.
| Parameters | |
|---|---|
@Nullable AudioDeviceInfo audioDeviceInfo |
The preferred |
setSkipSilenceEnabled
public void setSkipSilenceEnabled(boolean skipSilenceEnabled)
Sets whether silences should be skipped in the audio stream.
setVirtualDeviceId
public void setVirtualDeviceId(int virtualDeviceId)
Sets the virtual device id.
| Parameters | |
|---|---|
int virtualDeviceId |
The |
setVolume
public void setVolume(float volume)
Sets the playback volume.
| Parameters | |
|---|---|
float volume |
Linear output gain to apply to all channels. Should be in the range [0.0, 1.0]. |
supportsFormat
public boolean supportsFormat(Format format)
Returns whether the sink supports a given Format.
| Parameters | |
|---|---|
Format format |
The format. |
| Returns | |
|---|---|
boolean |
Whether the sink supports the format. |