public final class DefaultAudioSink extends java.lang.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 and 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.WriteException| Modifier and Type | Field and Description |
|---|---|
static boolean |
enablePreV21AudioSessionWorkaround
Whether to enable a workaround for an issue where an audio effect does not keep its session
active across releasing/initializing a new audio track, on platform builds where
Util.SDK_INT < 21. |
static boolean |
failOnSpuriousAudioTimestamp
Whether to throw an
DefaultAudioSink.InvalidAudioTrackTimestampException when a spurious timestamp is
reported from AudioTrack.getTimestamp(android.media.AudioTimestamp). |
CURRENT_POSITION_NOT_SET| Constructor and Description |
|---|
DefaultAudioSink(AudioCapabilities audioCapabilities,
AudioProcessor[] audioProcessors)
Creates a new default audio sink.
|
DefaultAudioSink(AudioCapabilities audioCapabilities,
AudioProcessor[] audioProcessors,
boolean enableConvertHighResIntPcmToFloat)
Creates a new default audio sink, optionally using float output for high resolution PCM.
|
DefaultAudioSink(AudioCapabilities audioCapabilities,
DefaultAudioSink.AudioProcessorChain audioProcessorChain,
boolean enableConvertHighResIntPcmToFloat)
Creates a new default audio sink, optionally using float output for high resolution PCM and
with the specified
audioProcessorChain. |
| Modifier and Type | Method and Description |
|---|---|
void |
configure(int inputEncoding,
int inputChannelCount,
int inputSampleRate,
int specifiedBufferSize,
int[] outputChannels,
int trimStartFrames,
int trimEndFrames)
Configures (or reconfigures) the sink.
|
void |
disableTunneling()
Disables tunneling.
|
void |
enableTunnelingV21(int tunnelingAudioSessionId)
Enables tunneling, if possible.
|
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. |
PlaybackParameters |
getPlaybackParameters()
Gets the active
PlaybackParameters. |
boolean |
handleBuffer(java.nio.ByteBuffer buffer,
long presentationTimeUs)
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 |
isEncodingSupported(int encoding)
Returns whether it's possible to play audio in the specified encoding.
|
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 |
release()
Releases any resources associated with this instance.
|
void |
reset()
Resets the sink, after which it is ready to receive buffers from a new playback position.
|
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.
|
PlaybackParameters |
setPlaybackParameters(PlaybackParameters playbackParameters)
Attempts to set the playback parameters and returns the active playback parameters, which may
differ from those passed in.
|
void |
setVolume(float volume)
Sets the playback volume.
|
public static boolean enablePreV21AudioSessionWorkaround
Util.SDK_INT < 21.
The flag must be set before creating a player.
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 enableConvertHighResIntPcmToFloat)
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.enableConvertHighResIntPcmToFloat - Whether to enable conversion of high resolution
integer PCM to 32-bit float for output, if possible. Functionality that uses 16-bit integer
audio processing (for example, speed and pitch adjustment) will not be available when float
output is in use.public DefaultAudioSink(@Nullable
AudioCapabilities audioCapabilities,
DefaultAudioSink.AudioProcessorChain audioProcessorChain,
boolean enableConvertHighResIntPcmToFloat)
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.enableConvertHighResIntPcmToFloat - Whether to enable conversion of high resolution
integer PCM to 32-bit float for output, if possible. Functionality that uses 16-bit integer
audio processing (for example, speed and pitch adjustment) will not be available when float
output 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 isEncodingSupported(@C.Encoding int encoding)
AudioSinkisEncodingSupported in interface AudioSinkencoding - The audio encoding.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(@C.Encoding int inputEncoding, int inputChannelCount, int inputSampleRate, int specifiedBufferSize, @Nullable int[] outputChannels, int trimStartFrames, int trimEndFrames) throws AudioSink.ConfigurationException
AudioSinkconfigure in interface AudioSinkinputEncoding - The encoding of audio data provided in the input buffers.inputChannelCount - The number of channels.inputSampleRate - The sample rate in Hz.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.trimStartFrames - The number of audio frames to trim from the start of data written to the
sink after this call.trimEndFrames - The number of audio frames to trim from data written to the sink
immediately preceding the next call to AudioSink.reset() or this method.AudioSink.ConfigurationException - If an error occurs configuring the sink.public void play()
AudioSinkpublic void handleDiscontinuity()
AudioSinkhandleDiscontinuity in interface AudioSinkpublic boolean handleBuffer(java.nio.ByteBuffer buffer,
long presentationTimeUs)
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.reset() (or to
AudioSink.configure(int, int, int, int, int[], int, int) that causes the sink to be reset).
handleBuffer in interface AudioSinkbuffer - The buffer containing audio data.presentationTimeUs - The presentation timestamp of the buffer in microseconds.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 PlaybackParameters setPlaybackParameters(PlaybackParameters playbackParameters)
AudioSinksetPlaybackParameters in interface AudioSinkplaybackParameters - The new playback parameters to attempt to set.public PlaybackParameters getPlaybackParameters()
AudioSinkPlaybackParameters.getPlaybackParameters 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(int tunnelingAudioSessionId)
AudioSinkAudioTrack, and requires platform API version 21 onwards.enableTunnelingV21 in interface AudioSinktunnelingAudioSessionId - The audio session id to use.public void disableTunneling()
AudioSinkdisableTunneling in interface AudioSinkpublic void setVolume(float volume)
AudioSinkpublic void pause()
AudioSinkpublic void reset()
AudioSink
The audio session may remain active until AudioSink.release() is called.