AudioSinkpublic final class DefaultAudioSink extends Object implements AudioSink
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.
| Modifier and Type | Class | Description |
|---|---|---|
static interface |
DefaultAudioSink.AudioProcessorChain |
Provides a chain of audio processors, which are used for any user-defined processing and
applying playback parameters (if supported).
|
static class |
DefaultAudioSink.DefaultAudioProcessorChain |
The default audio processor chain, which applies a (possibly empty) chain of user-defined audio
processors followed by
SilenceSkippingAudioProcessor and SonicAudioProcessor. |
static class |
DefaultAudioSink.InvalidAudioTrackTimestampException |
Thrown when the audio track has provided a spurious timestamp, if
failOnSpuriousAudioTimestamp is set. |
AudioSink.ConfigurationException, AudioSink.InitializationException, AudioSink.Listener, AudioSink.SinkFormatSupport, AudioSink.WriteException| Modifier and Type | Field | Description |
|---|---|---|
static float |
DEFAULT_PLAYBACK_SPEED |
The default playback speed.
|
static boolean |
failOnSpuriousAudioTimestamp |
Whether to throw an
DefaultAudioSink.InvalidAudioTrackTimestampException when a spurious timestamp is
reported from AudioTrack.getTimestamp(android.media.AudioTimestamp). |
static float |
MAX_PITCH |
The maximum allowed pitch factor.
|
static float |
MAX_PLAYBACK_SPEED |
The maximum allowed playback speed.
|
static float |
MIN_PITCH |
The minimum allowed pitch factor.
|
static float |
MIN_PLAYBACK_SPEED |
The minimum allowed playback speed.
|
CURRENT_POSITION_NOT_SET, SINK_FORMAT_SUPPORTED_DIRECTLY, SINK_FORMAT_SUPPORTED_WITH_TRANSCODING, SINK_FORMAT_UNSUPPORTED| Constructor | Description |
|---|---|
DefaultAudioSink(AudioCapabilities audioCapabilities,
AudioProcessor[] audioProcessors) |
Creates a new default audio sink.
|
DefaultAudioSink(AudioCapabilities audioCapabilities,
AudioProcessor[] audioProcessors,
boolean enableFloatOutput) |
Creates a new default audio sink, optionally using float output for high resolution PCM.
|
DefaultAudioSink(AudioCapabilities audioCapabilities,
DefaultAudioSink.AudioProcessorChain audioProcessorChain,
boolean enableFloatOutput,
boolean enableAudioTrackPlaybackParams,
boolean enableOffload) |
Creates a new default audio sink, optionally using float output for high resolution PCM and
with the specified
audioProcessorChain. |
| Modifier and Type | Method | Description |
|---|---|---|
void |
configure(Format inputFormat,
int specifiedBufferSize,
int[] outputChannels) |
Configures (or reconfigures) the sink.
|
void |
disableTunneling() |
Disables tunneling.
|
void |
enableTunnelingV21() |
Enables tunneling, if possible.
|
void |
experimentalFlushWithoutAudioTrackRelease() |
Flushes the sink, after which it is ready to receive buffers from a new playback position.
|
void |
flush() |
Flushes the sink, after which it is ready to receive buffers from a new playback position.
|
long |
getCurrentPositionUs(boolean sourceEnded) |
Returns the playback position in the stream starting at zero, in microseconds, or
AudioSink.CURRENT_POSITION_NOT_SET if it is not yet available. |
int |
getFormatSupport(Format format) |
Returns the level of support that the sink provides for a given
Format. |
PlaybackParameters |
getPlaybackParameters() |
Returns the active
PlaybackParameters. |
boolean |
getSkipSilenceEnabled() |
Returns whether silences are skipped in the audio stream.
|
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). |
void |
handleDiscontinuity() |
Signals to the sink that the next buffer may be discontinuous with the previous buffer.
|
boolean |
hasPendingData() |
Returns whether the sink has data pending that has not been consumed yet.
|
boolean |
isEnded() |
Returns whether
AudioSink.playToEndOfStream() has been called and all buffers have been processed. |
void |
pause() |
Pauses playback.
|
void |
play() |
Starts or resumes consuming audio if initialized.
|
void |
playToEndOfStream() |
Processes any remaining data.
|
void |
reset() |
Resets the renderer, releasing any resources that it currently holds.
|
void |
setAudioAttributes(AudioAttributes audioAttributes) |
Sets attributes for audio playback.
|
void |
setAudioSessionId(int audioSessionId) |
Sets the audio session id.
|
void |
setAuxEffectInfo(AuxEffectInfo auxEffectInfo) |
Sets the auxiliary effect.
|
void |
setListener(AudioSink.Listener listener) |
Sets the listener for sink events, which should be the audio renderer.
|
void |
setPlaybackParameters(PlaybackParameters playbackParameters) |
Attempts to set the playback parameters.
|
void |
setSkipSilenceEnabled(boolean skipSilenceEnabled) |
Sets whether silences should be skipped in the audio stream.
|
void |
setVolume(float volume) |
Sets the playback volume.
|
boolean |
supportsFormat(Format format) |
Returns whether the sink supports a given
Format. |
public static final float DEFAULT_PLAYBACK_SPEED
public static final float MIN_PLAYBACK_SPEED
public static final float MAX_PLAYBACK_SPEED
public static final float MIN_PITCH
public static final float MAX_PITCH
public static boolean failOnSpuriousAudioTimestamp
DefaultAudioSink.InvalidAudioTrackTimestampException when a spurious timestamp is
reported from AudioTrack.getTimestamp(android.media.AudioTimestamp).
The flag must be set before creating a player. Should be set to true for testing and
debugging purposes only.
public DefaultAudioSink(@Nullable
AudioCapabilities audioCapabilities,
AudioProcessor[] audioProcessors)
audioCapabilities - The audio capabilities for playback on this device. May be null if the
default capabilities (no encoded audio passthrough support) should be assumed.audioProcessors - An array of AudioProcessors that will process PCM audio before
output. May be empty.public DefaultAudioSink(@Nullable
AudioCapabilities audioCapabilities,
AudioProcessor[] audioProcessors,
boolean enableFloatOutput)
audioCapabilities - The audio capabilities for playback on this device. May be null if the
default capabilities (no encoded audio passthrough support) should be assumed.audioProcessors - An array of AudioProcessors that will process PCM audio before
output. May be empty.enableFloatOutput - Whether to enable 32-bit float output. Where possible, 32-bit float
output will be used if the input is 32-bit float, and also if the input is high resolution
(24-bit or 32-bit) integer PCM. Audio processing (for example, speed adjustment) will not
be available when float output is in use.public DefaultAudioSink(@Nullable
AudioCapabilities audioCapabilities,
DefaultAudioSink.AudioProcessorChain audioProcessorChain,
boolean enableFloatOutput,
boolean enableAudioTrackPlaybackParams,
boolean enableOffload)
audioProcessorChain.audioCapabilities - The audio capabilities for playback on this device. May be null if the
default capabilities (no encoded audio passthrough support) should be assumed.audioProcessorChain - An DefaultAudioSink.AudioProcessorChain which is used to apply playback
parameters adjustments. The instance passed in must not be reused in other sinks.enableFloatOutput - Whether to enable 32-bit float output. Where possible, 32-bit float
output will be used if the input is 32-bit float, and also if the input is high resolution
(24-bit or 32-bit) integer PCM. Float output is supported from API level 21. Audio
processing (for example, speed adjustment) will not be available when float output is in
use.enableAudioTrackPlaybackParams - Whether to enable setting playback speed using AudioTrack.setPlaybackParams(PlaybackParams), if supported.enableOffload - Whether to enable audio offload. If an audio format can be both played
with offload and encoded audio passthrough, it will be played in offload. Audio offload is
supported from API level 29. Most Android devices can only support one offload AudioTrack at a time and can invalidate it at any time. Thus an app can
never be guaranteed that it will be able to play in offload. Audio processing (for example,
speed adjustment) will not be available when offload is in use.public void setListener(AudioSink.Listener listener)
AudioSinksetListener in interface AudioSinklistener - The listener for sink events, which should be the audio renderer.public boolean supportsFormat(Format format)
AudioSinkFormat.supportsFormat in interface AudioSinkformat - The format.@SinkFormatSupport public int getFormatSupport(Format format)
AudioSinkFormat.getFormatSupport in interface AudioSinkformat - The format.public long getCurrentPositionUs(boolean sourceEnded)
AudioSinkAudioSink.CURRENT_POSITION_NOT_SET if it is not yet available.getCurrentPositionUs in interface AudioSinksourceEnded - Specify true if no more input buffers will be provided.public void configure(Format inputFormat, int specifiedBufferSize, @Nullable int[] outputChannels) throws AudioSink.ConfigurationException
AudioSinkconfigure in interface AudioSinkinputFormat - The format of audio data provided in the input buffers.specifiedBufferSize - A specific size for the playback buffer in bytes, or 0 to infer a
suitable buffer size.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 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.AudioSink.ConfigurationException - If an error occurs configuring the sink.public void play()
AudioSinkpublic void handleDiscontinuity()
AudioSinkhandleDiscontinuity in interface AudioSinkpublic boolean handleBuffer(ByteBuffer buffer, long presentationTimeUs, int encodedAccessUnitCount) throws AudioSink.InitializationException, AudioSink.WriteException
AudioSinkByteBuffer, 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. AudioSink.Listener.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 AudioSink.flush() (or to AudioSink.configure(Format,
int, int[]) that causes the sink to be flushed).
handleBuffer in interface AudioSinkbuffer - The buffer containing audio data.presentationTimeUs - The presentation timestamp of the buffer in microseconds.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.AudioSink.InitializationException - If an error occurs initializing the sink.AudioSink.WriteException - If an error occurs writing the audio data.public void playToEndOfStream()
throws AudioSink.WriteException
AudioSinkAudioSink.isEnded() will return true when no data remains.playToEndOfStream in interface AudioSinkAudioSink.WriteException - If an error occurs draining data to the sink.public boolean isEnded()
AudioSinkAudioSink.playToEndOfStream() has been called and all buffers have been processed.public boolean hasPendingData()
AudioSinkhasPendingData in interface AudioSinkpublic void setPlaybackParameters(PlaybackParameters playbackParameters)
AudioSinksetPlaybackParameters in interface AudioSinkplaybackParameters - The new playback parameters to attempt to set.public PlaybackParameters getPlaybackParameters()
AudioSinkPlaybackParameters.getPlaybackParameters in interface AudioSinkpublic void setSkipSilenceEnabled(boolean skipSilenceEnabled)
AudioSinksetSkipSilenceEnabled in interface AudioSinkpublic boolean getSkipSilenceEnabled()
AudioSinkgetSkipSilenceEnabled in interface AudioSinkpublic void setAudioAttributes(AudioAttributes audioAttributes)
AudioSinkIf 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.
setAudioAttributes in interface AudioSinkaudioAttributes - The attributes for audio playback.public void setAudioSessionId(int audioSessionId)
AudioSinksetAudioSessionId in interface AudioSinkpublic void setAuxEffectInfo(AuxEffectInfo auxEffectInfo)
AudioSinksetAuxEffectInfo in interface AudioSinkpublic void enableTunnelingV21()
AudioSinkAudioTrack, and
requires platform API version 21 onwards.enableTunnelingV21 in interface AudioSinkpublic void disableTunneling()
AudioSinkdisableTunneling in interface AudioSinkpublic void setVolume(float volume)
AudioSinkpublic void pause()
AudioSinkpublic void flush()
AudioSinkThe audio session may remain active until AudioSink.reset() is called.
public void experimentalFlushWithoutAudioTrackRelease()
AudioSinkDoes not release the AudioTrack held by the sink.
This method is experimental, and will be renamed or removed in a future release.
Only for experimental use as part of MediaCodecAudioRenderer.experimentalSetEnableKeepAudioTrackOnSeek(boolean).
experimentalFlushWithoutAudioTrackRelease in interface AudioSink