Class DefaultAudioSink
- java.lang.Object
-
- com.google.android.exoplayer2.audio.DefaultAudioSink
-
- All Implemented Interfaces:
AudioSink
public final class DefaultAudioSink extends Object implements AudioSink
Plays audio data. The implementation delegates to anAudioTrackand 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceDefaultAudioSink.AudioProcessorChainProvides a chain of audio processors, which are used for any user-defined processing and applying playback parameters (if supported).static classDefaultAudioSink.BuilderA builder to createDefaultAudioSinkinstances.static classDefaultAudioSink.DefaultAudioProcessorChainThe default audio processor chain, which applies a (possibly empty) chain of user-defined audio processors followed bySilenceSkippingAudioProcessorandSonicAudioProcessor.static classDefaultAudioSink.InvalidAudioTrackTimestampExceptionThrown when the audio track has provided a spurious timestamp, iffailOnSpuriousAudioTimestampis set.static interfaceDefaultAudioSink.OffloadModeAudio offload mode configuration.static interfaceDefaultAudioSink.OutputModeOutput mode of the audio sink.-
Nested classes/interfaces inherited from interface com.google.android.exoplayer2.audio.AudioSink
AudioSink.ConfigurationException, AudioSink.InitializationException, AudioSink.Listener, AudioSink.SinkFormatSupport, AudioSink.UnexpectedDiscontinuityException, AudioSink.WriteException
-
-
Field Summary
Fields Modifier and Type Field Description static floatDEFAULT_PLAYBACK_SPEEDThe default playback speed.static booleanfailOnSpuriousAudioTimestampWhether to throw anDefaultAudioSink.InvalidAudioTrackTimestampExceptionwhen a spurious timestamp is reported fromAudioTrack.getTimestamp(android.media.AudioTimestamp).static floatMAX_PITCHThe maximum allowed pitch factor.static floatMAX_PLAYBACK_SPEEDThe maximum allowed playback speed.static floatMIN_PITCHThe minimum allowed pitch factor.static floatMIN_PLAYBACK_SPEEDThe minimum allowed playback speed.static intOFFLOAD_MODE_DISABLEDThe audio sink will never play in offload mode.static intOFFLOAD_MODE_ENABLED_GAPLESS_DISABLEDThe audio sink will prefer offload playback, disabling gapless offload support.static intOFFLOAD_MODE_ENABLED_GAPLESS_NOT_REQUIREDThe audio sink will prefer offload playback even if this might result in silence gaps between tracks.static intOFFLOAD_MODE_ENABLED_GAPLESS_REQUIREDThe audio sink will prefer offload playback except if the track is gapless and the device does not advertise support for gapless playback in offload.static intOUTPUT_MODE_OFFLOADThe audio sink plays encoded audio in offload.static intOUTPUT_MODE_PASSTHROUGHThe audio sink plays encoded audio in passthrough.static intOUTPUT_MODE_PCMThe audio sink plays PCM audio.-
Fields inherited from interface com.google.android.exoplayer2.audio.AudioSink
CURRENT_POSITION_NOT_SET, SINK_FORMAT_SUPPORTED_DIRECTLY, SINK_FORMAT_SUPPORTED_WITH_TRANSCODING, SINK_FORMAT_UNSUPPORTED
-
-
Constructor Summary
Constructors Constructor Description DefaultAudioSink(AudioCapabilities audioCapabilities, AudioProcessor[] audioProcessors)Deprecated.DefaultAudioSink(AudioCapabilities audioCapabilities, AudioProcessor[] audioProcessors, boolean enableFloatOutput)Deprecated.DefaultAudioSink(AudioCapabilities audioCapabilities, DefaultAudioSink.AudioProcessorChain audioProcessorChain, boolean enableFloatOutput, boolean enableAudioTrackPlaybackParams, @com.google.android.exoplayer2.audio.DefaultAudioSink.OffloadMode int offloadMode)Deprecated.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidconfigure(Format inputFormat, int specifiedBufferSize, int[] outputChannels)Configures (or reconfigures) the sink.voiddisableTunneling()Disables tunneling.voidenableTunnelingV21()Enables tunneling, if possible.voidexperimentalFlushWithoutAudioTrackRelease()Flushes the sink, after which it is ready to receive buffers from a new playback position.voidflush()Flushes the sink, after which it is ready to receive buffers from a new playback position.AudioAttributesgetAudioAttributes()Returns the audio attributes used for audio playback, ornullif the sink does not use audio attributes.longgetCurrentPositionUs(boolean sourceEnded)Returns the playback position in the stream starting at zero, in microseconds, orAudioSink.CURRENT_POSITION_NOT_SETif it is not yet available.@com.google.android.exoplayer2.audio.AudioSink.SinkFormatSupport intgetFormatSupport(Format format)Returns the level of support that the sink provides for a givenFormat.PlaybackParametersgetPlaybackParameters()Returns the activePlaybackParameters.booleangetSkipSilenceEnabled()Returns whether silences are skipped in the audio stream.booleanhandleBuffer(ByteBuffer buffer, long presentationTimeUs, int encodedAccessUnitCount)Attempts to process data from aByteBuffer, starting from its current position and ending at its limit (exclusive).voidhandleDiscontinuity()Signals to the sink that the next buffer may be discontinuous with the previous buffer.booleanhasPendingData()Returns whether the sink has data pending that has not been consumed yet.booleanisEnded()Returns whetherAudioSink.playToEndOfStream()has been called and all buffers have been processed.voidpause()Pauses playback.voidplay()Starts or resumes consuming audio if initialized.voidplayToEndOfStream()Processes any remaining data.voidreset()Resets the sink, releasing any resources that it currently holds.voidsetAudioAttributes(AudioAttributes audioAttributes)Sets attributes for audio playback.voidsetAudioSessionId(int audioSessionId)Sets the audio session id.voidsetAuxEffectInfo(AuxEffectInfo auxEffectInfo)Sets the auxiliary effect.voidsetListener(AudioSink.Listener listener)Sets the listener for sink events, which should be the audio renderer.voidsetPlaybackParameters(PlaybackParameters playbackParameters)Attempts to set the playback parameters.voidsetPlayerId(PlayerId playerId)Sets thePlayerIdof the player using this audio sink.voidsetSkipSilenceEnabled(boolean skipSilenceEnabled)Sets whether silences should be skipped in the audio stream.voidsetVolume(float volume)Sets the playback volume.booleansupportsFormat(Format format)Returns whether the sink supports a givenFormat.
-
-
-
Field Detail
-
DEFAULT_PLAYBACK_SPEED
public static final float DEFAULT_PLAYBACK_SPEED
The default playback speed.- See Also:
- Constant Field Values
-
MIN_PLAYBACK_SPEED
public static final float MIN_PLAYBACK_SPEED
The minimum allowed playback speed. Lower values will be constrained to fall in range.- See Also:
- Constant Field Values
-
MAX_PLAYBACK_SPEED
public static final float MAX_PLAYBACK_SPEED
The maximum allowed playback speed. Higher values will be constrained to fall in range.- See Also:
- Constant Field Values
-
MIN_PITCH
public static final float MIN_PITCH
The minimum allowed pitch factor. Lower values will be constrained to fall in range.- See Also:
- Constant Field Values
-
MAX_PITCH
public static final float MAX_PITCH
The maximum allowed pitch factor. Higher values will be constrained to fall in range.- See Also:
- Constant Field Values
-
OFFLOAD_MODE_DISABLED
public static final int OFFLOAD_MODE_DISABLED
The audio sink will never play in offload mode.- See Also:
- Constant Field Values
-
OFFLOAD_MODE_ENABLED_GAPLESS_REQUIRED
public static final int OFFLOAD_MODE_ENABLED_GAPLESS_REQUIRED
The audio sink will prefer offload playback except if the track is gapless and the device does not advertise support for gapless playback in offload.Use this option to prioritize seamless transitions between tracks of the same album to power savings.
- See Also:
- Constant Field Values
-
OFFLOAD_MODE_ENABLED_GAPLESS_NOT_REQUIRED
public static final int OFFLOAD_MODE_ENABLED_GAPLESS_NOT_REQUIRED
The audio sink will prefer offload playback even if this might result in silence gaps between tracks.Use this option to prioritize battery saving at the cost of a possible non seamless transitions between tracks of the same album.
- See Also:
- Constant Field Values
-
OFFLOAD_MODE_ENABLED_GAPLESS_DISABLED
public static final int OFFLOAD_MODE_ENABLED_GAPLESS_DISABLED
The audio sink will prefer offload playback, disabling gapless offload support.Use this option if gapless has undesirable side effects. For example if it introduces hardware issues.
- See Also:
- Constant Field Values
-
OUTPUT_MODE_PCM
public static final int OUTPUT_MODE_PCM
The audio sink plays PCM audio.- See Also:
- Constant Field Values
-
OUTPUT_MODE_OFFLOAD
public static final int OUTPUT_MODE_OFFLOAD
The audio sink plays encoded audio in offload.- See Also:
- Constant Field Values
-
OUTPUT_MODE_PASSTHROUGH
public static final int OUTPUT_MODE_PASSTHROUGH
The audio sink plays encoded audio in passthrough.- See Also:
- Constant Field Values
-
failOnSpuriousAudioTimestamp
public static boolean failOnSpuriousAudioTimestamp
Whether to throw anDefaultAudioSink.InvalidAudioTrackTimestampExceptionwhen a spurious timestamp is reported fromAudioTrack.getTimestamp(android.media.AudioTimestamp).The flag must be set before creating a player. Should be set to
truefor testing and debugging purposes only.
-
-
Constructor Detail
-
DefaultAudioSink
@Deprecated @InlineMe(replacement="new DefaultAudioSink.Builder().setAudioCapabilities(audioCapabilities).setAudioProcessors(audioProcessors).build()", imports="com.google.android.exoplayer2.audio.DefaultAudioSink") public DefaultAudioSink(@Nullable AudioCapabilities audioCapabilities, AudioProcessor[] audioProcessors)
Deprecated.
-
DefaultAudioSink
@Deprecated @InlineMe(replacement="new DefaultAudioSink.Builder().setAudioCapabilities(audioCapabilities).setAudioProcessors(audioProcessors).setEnableFloatOutput(enableFloatOutput).build()", imports="com.google.android.exoplayer2.audio.DefaultAudioSink") public DefaultAudioSink(@Nullable AudioCapabilities audioCapabilities, AudioProcessor[] audioProcessors, boolean enableFloatOutput)
Deprecated.
-
DefaultAudioSink
@Deprecated @InlineMe(replacement="new DefaultAudioSink.Builder().setAudioCapabilities(audioCapabilities).setAudioProcessorChain(audioProcessorChain).setEnableFloatOutput(enableFloatOutput).setEnableAudioTrackPlaybackParams(enableAudioTrackPlaybackParams).setOffloadMode(offloadMode).build()", imports="com.google.android.exoplayer2.audio.DefaultAudioSink") public DefaultAudioSink(@Nullable AudioCapabilities audioCapabilities, DefaultAudioSink.AudioProcessorChain audioProcessorChain, boolean enableFloatOutput, boolean enableAudioTrackPlaybackParams, @com.google.android.exoplayer2.audio.DefaultAudioSink.OffloadMode int offloadMode)
Deprecated.
-
-
Method Detail
-
setListener
public void setListener(AudioSink.Listener listener)
Description copied from interface:AudioSinkSets the listener for sink events, which should be the audio renderer.- Specified by:
setListenerin interfaceAudioSink- Parameters:
listener- The listener for sink events, which should be the audio renderer.
-
setPlayerId
public void setPlayerId(@Nullable PlayerId playerId)Description copied from interface:AudioSinkSets thePlayerIdof the player using this audio sink.- Specified by:
setPlayerIdin interfaceAudioSink- Parameters:
playerId- ThePlayerId, or null to clear a previously set id.
-
supportsFormat
public boolean supportsFormat(Format format)
Description copied from interface:AudioSinkReturns whether the sink supports a givenFormat.- Specified by:
supportsFormatin interfaceAudioSink- Parameters:
format- The format.- Returns:
- Whether the sink supports the format.
-
getFormatSupport
public @com.google.android.exoplayer2.audio.AudioSink.SinkFormatSupport int getFormatSupport(Format format)
Description copied from interface:AudioSinkReturns the level of support that the sink provides for a givenFormat.- Specified by:
getFormatSupportin interfaceAudioSink- Parameters:
format- The format.- Returns:
- The level of support provided.
-
getCurrentPositionUs
public long getCurrentPositionUs(boolean sourceEnded)
Description copied from interface:AudioSinkReturns the playback position in the stream starting at zero, in microseconds, orAudioSink.CURRENT_POSITION_NOT_SETif it is not yet available.- Specified by:
getCurrentPositionUsin interfaceAudioSink- Parameters:
sourceEnded- Specifytrueif no more input buffers will be provided.- Returns:
- The playback position relative to the start of playback, in microseconds.
-
configure
public void configure(Format inputFormat, int specifiedBufferSize, @Nullable int[] outputChannels) throws AudioSink.ConfigurationException
Description copied from interface:AudioSinkConfigures (or reconfigures) the sink.- Specified by:
configurein interfaceAudioSink- Parameters:
inputFormat- 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. Specifynullto leave the input unchanged. Otherwise, the element at indexispecifies index of the input channel to map to output channeliwhen preprocessing input buffers. After the map is applied the audio data will haveoutputChannels.lengthchannels.- Throws:
AudioSink.ConfigurationException- If an error occurs configuring the sink.
-
play
public void play()
Description copied from interface:AudioSinkStarts or resumes consuming audio if initialized.
-
handleDiscontinuity
public void handleDiscontinuity()
Description copied from interface:AudioSinkSignals to the sink that the next buffer may be discontinuous with the previous buffer.- Specified by:
handleDiscontinuityin interfaceAudioSink
-
handleBuffer
public boolean handleBuffer(ByteBuffer buffer, long presentationTimeUs, int encodedAccessUnitCount) throws AudioSink.InitializationException, AudioSink.WriteException
Description copied from interface:AudioSinkAttempts to process data from aByteBuffer, starting from its current position and ending at its limit (exclusive). The position of theByteBufferis advanced by the number of bytes that were handled.AudioSink.Listener.onPositionDiscontinuity()will be called ifpresentationTimeUsis 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
ByteBuffermust be provided to subsequent calls until it has been fully consumed, except in the case of an intervening call toAudioSink.flush()(or toAudioSink.configure(Format, int, int[])that causes the sink to be flushed).- Specified by:
handleBufferin interfaceAudioSink- Parameters:
buffer- 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.- Returns:
- Whether the buffer was handled fully.
- Throws:
AudioSink.InitializationException- If an error occurs initializing the sink.AudioSink.WriteException- If an error occurs writing the audio data.
-
playToEndOfStream
public void playToEndOfStream() throws AudioSink.WriteExceptionDescription copied from interface:AudioSinkProcesses any remaining data.AudioSink.isEnded()will returntruewhen no data remains.- Specified by:
playToEndOfStreamin interfaceAudioSink- Throws:
AudioSink.WriteException- If an error occurs draining data to the sink.
-
isEnded
public boolean isEnded()
Description copied from interface:AudioSinkReturns whetherAudioSink.playToEndOfStream()has been called and all buffers have been processed.
-
hasPendingData
public boolean hasPendingData()
Description copied from interface:AudioSinkReturns whether the sink has data pending that has not been consumed yet.- Specified by:
hasPendingDatain interfaceAudioSink
-
setPlaybackParameters
public void setPlaybackParameters(PlaybackParameters playbackParameters)
Description copied from interface:AudioSinkAttempts to set the playback parameters. The audio sink may override these parameters if they are not supported.- Specified by:
setPlaybackParametersin interfaceAudioSink- Parameters:
playbackParameters- The new playback parameters to attempt to set.
-
getPlaybackParameters
public PlaybackParameters getPlaybackParameters()
Description copied from interface:AudioSinkReturns the activePlaybackParameters.- Specified by:
getPlaybackParametersin interfaceAudioSink
-
setSkipSilenceEnabled
public void setSkipSilenceEnabled(boolean skipSilenceEnabled)
Description copied from interface:AudioSinkSets whether silences should be skipped in the audio stream.- Specified by:
setSkipSilenceEnabledin interfaceAudioSink
-
getSkipSilenceEnabled
public boolean getSkipSilenceEnabled()
Description copied from interface:AudioSinkReturns whether silences are skipped in the audio stream.- Specified by:
getSkipSilenceEnabledin interfaceAudioSink
-
setAudioAttributes
public void setAudioAttributes(AudioAttributes audioAttributes)
Description copied from interface:AudioSinkSets 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.
- Specified by:
setAudioAttributesin interfaceAudioSink- Parameters:
audioAttributes- The attributes for audio playback.
-
getAudioAttributes
public AudioAttributes getAudioAttributes()
Description copied from interface:AudioSinkReturns the audio attributes used for audio playback, ornullif the sink does not use audio attributes.- Specified by:
getAudioAttributesin interfaceAudioSink
-
setAudioSessionId
public void setAudioSessionId(int audioSessionId)
Description copied from interface:AudioSinkSets the audio session id.- Specified by:
setAudioSessionIdin interfaceAudioSink
-
setAuxEffectInfo
public void setAuxEffectInfo(AuxEffectInfo auxEffectInfo)
Description copied from interface:AudioSinkSets the auxiliary effect.- Specified by:
setAuxEffectInfoin interfaceAudioSink
-
enableTunnelingV21
public void enableTunnelingV21()
Description copied from interface:AudioSinkEnables tunneling, if possible. The sink is reset if tunneling was previously disabled. Enabling tunneling is only possible if the sink is based on a platformAudioTrack, and requires platform API version 21 onwards.- Specified by:
enableTunnelingV21in interfaceAudioSink
-
disableTunneling
public void disableTunneling()
Description copied from interface:AudioSinkDisables tunneling. If tunneling was previously enabled then the sink is reset and any audio session id is cleared.- Specified by:
disableTunnelingin interfaceAudioSink
-
setVolume
public void setVolume(float volume)
Description copied from interface:AudioSinkSets the playback volume.
-
pause
public void pause()
Description copied from interface:AudioSinkPauses playback.
-
flush
public void flush()
Description copied from interface:AudioSinkFlushes the sink, after which it is ready to receive buffers from a new playback position.The audio session may remain active until
AudioSink.reset()is called.
-
experimentalFlushWithoutAudioTrackRelease
public void experimentalFlushWithoutAudioTrackRelease()
Description copied from interface:AudioSinkFlushes the sink, after which it is ready to receive buffers from a new playback position.Does not release the
AudioTrackheld 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).- Specified by:
experimentalFlushWithoutAudioTrackReleasein interfaceAudioSink
-
-