SimpleBasePlayer
@UnstableApi
public abstract class SimpleBasePlayer extends BasePlayer
CompositionPlayer |
A |
FakePlayer |
A fake |
ForwardingSimpleBasePlayer |
A |
A base implementation for Player that reduces the number of methods to implement to a minimum.
Implementation notes:
- Subclasses must override
getStateto populate the current player state on request. - The
Stateshould set theavailable commandsto indicate whichPlayermethods are supported. - All setter-like player methods (for example,
setPlayWhenReady) forward to overridable methods (for example,handleSetPlayWhenReady) that can be used to handle these requests. These methods return aListenableFutureto indicate when the request has been handled and is fully reflected in the values returned fromgetState. This class will automatically request a state update once the request is done. If the state changes can be handled synchronously, these methods can return Guava'simmediateVoidFuture. - Subclasses can manually trigger state updates with
invalidateState, for example if something changes independent ofPlayermethod calls.
- The
Statecan only be created with allowed combinations of state values, avoiding any invalid player states. - Only functionality that is declared as
availableneeds to be implemented. Other methods are automatically ignored. - Listener handling and informing listeners of state changes is handled automatically.
- The base class provides a framework for asynchronous handling of method calls. It changes the visible playback state immediately to the most likely outcome to ensure the user-visible state changes look like synchronous operations. The state is then updated again once the asynchronous method calls have been fully handled.
Summary
Nested types |
|---|
public final class SimpleBasePlayer.LivePositionSupplier implements SimpleBasePlayer.PositionSupplierA |
public final class SimpleBasePlayer.MediaItemDataAn immutable description of an item in the playlist, containing both static setup information like |
public final class SimpleBasePlayer.MediaItemData.BuilderA builder for |
public final class SimpleBasePlayer.PeriodDataData describing the properties of a period inside a |
public final class SimpleBasePlayer.PeriodData.BuilderA builder for |
public interface SimpleBasePlayer.PositionSupplierA supplier for a position. |
public final class SimpleBasePlayer.StateAn immutable state description of the player. |
public final class SimpleBasePlayer.State.BuilderA builder for |
Public fields |
|
|---|---|
final Looper |
Protected fields |
|
|---|---|
@MonotonicNonNull SimpleBasePlayer.State |
Protected constructors |
|---|
SimpleBasePlayer(Looper applicationLooper)Creates the base class. |
SimpleBasePlayer(Looper applicationLooper, Clock clock)Creates the base class. |
Public methods |
|
|---|---|
final void |
addListener(Player.Listener listener)Registers a listener to receive all events from the player. |
final void |
addMediaItems(int index, List<MediaItem> mediaItems)Adds a list of media items at the given index of the playlist. |
final void |
Clears any |
final void |
clearVideoSurface(@Nullable Surface surface)Clears the |
final void |
clearVideoSurfaceHolder(@Nullable SurfaceHolder surfaceHolder)Clears the |
final void |
clearVideoSurfaceView(@Nullable SurfaceView surfaceView)Clears the |
final void |
clearVideoTextureView(@Nullable TextureView textureView)Clears the |
final void |
This method is deprecated. Use |
final void |
decreaseDeviceVolume(@C.VolumeFlags int flags)Decreases the volume of the device. |
final Looper |
Returns the |
final AudioAttributes |
Returns the attributes for audio playback. |
final int |
Returns the audio session identifier, or |
final Player.Commands |
Returns the player's currently available |
final long |
Returns an estimate of the position in the current content or ad up to which data is buffered, in milliseconds. |
final long |
If |
final long |
If |
final int |
If |
final int |
If |
final CueGroup |
Returns the current |
final int |
Returns the index of the current |
final int |
Returns the index of the period currently being played. |
final long |
Returns the playback position in the current content or ad, in milliseconds, or the prospective position in milliseconds if the |
final Timeline |
Returns the current |
final Tracks |
Returns the current tracks. |
final DeviceInfo |
Gets the device information. |
final int |
Gets the current volume of the device. |
final long |
Returns the duration of the current content or ad in milliseconds, or |
final long |
Returns the maximum position for which |
final MediaMetadata |
Returns the current combined |
final boolean |
Whether playback will proceed when |
final PlaybackParameters |
Returns the currently active playback parameters. |
final int |
Returns the current |
final int |
Returns the reason why playback is suppressed even though |
final @Nullable PlaybackException |
Returns the error that caused playback to fail. |
final MediaMetadata |
Returns the playlist |
final int |
Returns the current |
final long |
Returns the |
final long |
Returns the |
final boolean |
Returns whether shuffling of media items is enabled. |
final Size |
Gets the size of the surface on which the video is rendered. |
final long |
Returns an estimate of the total buffered duration from the current position, in milliseconds. |
final TrackSelectionParameters |
Returns the parameters constraining the track selection. |
final VideoSize |
Gets the size of the video. |
final float |
Returns the audio volume, with 0 being silence and 1 being unity gain (signal unchanged). |
final void |
This method is deprecated. Use |
final void |
increaseDeviceVolume(@C.VolumeFlags int flags)Increases the volume of the device. |
final boolean |
Gets whether the device is muted or not. |
final boolean |
Whether the player is currently loading the source. |
final boolean |
Returns whether the player is currently playing an ad. |
final void |
moveMediaItems(int fromIndex, int toIndex, int newIndex)Moves the media item range to the new index. |
final void |
mute()Sets the audio volume to 0. |
final void |
prepare()Prepares the player. |
final void |
release()Releases the player. |
final void |
removeListener(Player.Listener listener)Unregister a listener registered through |
final void |
removeMediaItems(int fromIndex, int toIndex)Removes a range of media items from the playlist. |
final void |
replaceMediaItems(Replaces the media items at the given range of the playlist. |
final void |
setAudioAttributes(Sets the attributes for audio playback, used by the underlying audio track. |
final void |
This method is deprecated. Use |
final void |
setDeviceMuted(boolean muted, @C.VolumeFlags int flags)Sets the mute state of the device. |
final void |
This method is deprecated. Use |
final void |
setDeviceVolume(int volume, @C.VolumeFlags int flags)Sets the volume of the device with volume flags. |
final void |
setMediaItems(List<MediaItem> mediaItems, boolean resetPosition)Clears the playlist and adds the specified |
final void |
setMediaItems(Clears the playlist and adds the specified |
final void |
setPlayWhenReady(boolean playWhenReady)Sets whether playback should proceed when |
final void |
setPlaybackParameters(PlaybackParameters playbackParameters)Attempts to set the playback parameters. |
final void |
setPlaylistMetadata(MediaMetadata mediaMetadata)Sets the playlist |
final void |
setRepeatMode(@Player.RepeatMode int repeatMode)Sets the |
final void |
setShuffleModeEnabled(boolean shuffleModeEnabled)Sets whether shuffling of media items is enabled. |
final void |
setTrackSelectionParameters(TrackSelectionParameters parameters)Sets the parameters constraining the track selection. |
final void |
setVideoSurface(@Nullable Surface surface)Sets the |
final void |
setVideoSurfaceHolder(@Nullable SurfaceHolder surfaceHolder)Sets the |
final void |
setVideoSurfaceView(@Nullable SurfaceView surfaceView)Sets the |
final void |
setVideoTextureView(@Nullable TextureView textureView)Sets the |
final void |
setVolume(float volume)Sets the audio volume, valid values are between 0 (silence) and 1 (unity gain, signal unchanged), inclusive. |
final void |
stop()Stops playback without resetting the playlist. |
final void |
unmute()If the audio volume is 0, sets the audio volume to a non-zero value decided by the Player to be the most appropriate. |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Inherited fields |
||
|---|---|---|
|
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Public fields
Protected fields
Protected constructors
Public methods
addListener
public final void addListener(Player.Listener listener)
Registers a listener to receive all events from the player.
The listener's methods will be called on the thread associated with getApplicationLooper.
This method can be called from any thread.
| Parameters | |
|---|---|
Player.Listener listener |
The listener to register. |
addMediaItems
public final void addMediaItems(int index, List<MediaItem> mediaItems)
Adds a list of media items at the given index of the playlist.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
int index |
The index at which to add the media items. If the index is larger than the size of the playlist, the media items are added to the end of the playlist. |
List<MediaItem> mediaItems |
The |
clearVideoSurface
public final void clearVideoSurface()
Clears any Surface, SurfaceHolder, SurfaceView or TextureView currently set on the player.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
clearVideoSurface
public final void clearVideoSurface(@Nullable Surface surface)
Clears the Surface onto which video is being rendered if it matches the one passed. Else does nothing.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
clearVideoSurfaceHolder
public final void clearVideoSurfaceHolder(@Nullable SurfaceHolder surfaceHolder)
Clears the SurfaceHolder that holds the Surface onto which video is being rendered if it matches the one passed. Else does nothing.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
@Nullable SurfaceHolder surfaceHolder |
The surface holder to clear. |
clearVideoSurfaceView
public final void clearVideoSurfaceView(@Nullable SurfaceView surfaceView)
Clears the SurfaceView onto which video is being rendered if it matches the one passed. Else does nothing.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
@Nullable SurfaceView surfaceView |
The texture view to clear. |
clearVideoTextureView
public final void clearVideoTextureView(@Nullable TextureView textureView)
Clears the TextureView onto which video is being rendered if it matches the one passed. Else does nothing.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
@Nullable TextureView textureView |
The texture view to clear. |
decreaseDeviceVolume
public final void decreaseDeviceVolume(@C.VolumeFlags int flags)
Decreases the volume of the device.
The getDeviceVolume device volume cannot be decreased below minVolume.
Note that this method affects the device volume. To change the volume of the current stream only, use setVolume.
This method must only be called if COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS is available.
| Parameters | |
|---|---|
@C.VolumeFlags int flags |
Either 0 or a bitwise combination of one or more |
getApplicationLooper
public final Looper getApplicationLooper()
Returns the Looper associated with the application thread that's used to access the player and on which player events are received.
This method can be called from any thread.
getAudioAttributes
public final AudioAttributes getAudioAttributes()
Returns the attributes for audio playback.
This method must only be called if COMMAND_GET_AUDIO_ATTRIBUTES is available.
| See also | |
|---|---|
onAudioAttributesChanged |
getAudioSessionId
public final int getAudioSessionId()
Returns the audio session identifier, or AUDIO_SESSION_ID_UNSET if not set.
| See also | |
|---|---|
onAudioSessionIdChanged |
getAvailableCommands
public final Player.Commands getAvailableCommands()
Returns the player's currently available Commands.
The returned Commands are not updated when available commands change. Use onAvailableCommandsChanged to get an update when the available commands change.
| Returns | |
|---|---|
Player.Commands |
The currently available |
| See also | |
|---|---|
onAvailableCommandsChanged |
getBufferedPosition
public final long getBufferedPosition()
Returns an estimate of the position in the current content or ad up to which data is buffered, in milliseconds.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getContentBufferedPosition
public final long getContentBufferedPosition()
If isPlayingAd returns true, returns an estimate of the content position in the current content up to which data is buffered, in milliseconds. If there is no ad playing, the returned position is the same as that returned by getBufferedPosition.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getContentPosition
public final long getContentPosition()
If isPlayingAd returns true, returns the content position that will be played once all ads in the ad group have finished playing, in milliseconds. If there is no ad playing, the returned position is the same as that returned by getCurrentPosition.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getCurrentAdGroupIndex
public final int getCurrentAdGroupIndex()
If isPlayingAd returns true, returns the index of the ad group in the period currently being played. Returns INDEX_UNSET otherwise.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getCurrentAdIndexInAdGroup
public final int getCurrentAdIndexInAdGroup()
If isPlayingAd returns true, returns the index of the ad in its ad group. Returns INDEX_UNSET otherwise.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getCurrentCues
public final CueGroup getCurrentCues()
Returns the current CueGroup.
This method must only be called if COMMAND_GET_TEXT is available.
| See also | |
|---|---|
onCues |
getCurrentMediaItemIndex
public final int getCurrentMediaItemIndex()
Returns the index of the current MediaItem in the timeline, or the prospective index if the current timeline is empty.
This method must only be called if COMMAND_GET_TIMELINE is available.
getCurrentPeriodIndex
public final int getCurrentPeriodIndex()
Returns the index of the period currently being played.
This method must only be called if COMMAND_GET_TIMELINE is available.
getCurrentPosition
public final long getCurrentPosition()
Returns the playback position in the current content or ad, in milliseconds, or the prospective position in milliseconds if the current timeline is empty.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getCurrentTimeline
public final Timeline getCurrentTimeline()
Returns the current Timeline. Never null, but may be empty.
This method must only be called if COMMAND_GET_TIMELINE is available.
| See also | |
|---|---|
onTimelineChanged |
getCurrentTracks
public final Tracks getCurrentTracks()
Returns the current tracks.
This method must only be called if COMMAND_GET_TRACKS is available.
| See also | |
|---|---|
onTracksChanged |
getDeviceVolume
public final int getDeviceVolume()
Gets the current volume of the device.
For devices with local playback, the volume returned by this method varies according to the current stream type. The stream type is determined by getStreamType.
For devices with remote playback, the volume of the remote device is returned.
Note that this method returns the volume of the device. To check the current stream volume, use getVolume.
This method must only be called if COMMAND_GET_DEVICE_VOLUME is available.
| See also | |
|---|---|
onDeviceVolumeChanged |
getDuration
public final long getDuration()
Returns the duration of the current content or ad in milliseconds, or TIME_UNSET if the duration is not known.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getMaxSeekToPreviousPosition
public final long getMaxSeekToPreviousPosition()
Returns the maximum position for which seekToPrevious seeks to the previous MediaItem, in milliseconds.
| Returns | |
|---|---|
long |
The maximum seek to previous position, in milliseconds. |
| See also | |
|---|---|
onMaxSeekToPreviousPositionChanged |
getMediaMetadata
public final MediaMetadata getMediaMetadata()
Returns the current combined MediaMetadata, or EMPTY if not supported.
This MediaMetadata is a combination of the MediaItem metadata, the static metadata in the media's Format, and any timed metadata that has been parsed from the media and output via onMetadata. If a field is populated in the mediaMetadata, it will be prioritised above the same field coming from static or timed metadata.
This method must only be called if COMMAND_GET_METADATA is available.
| See also | |
|---|---|
onMediaMetadataChanged |
getPlayWhenReady
public final boolean getPlayWhenReady()
Whether playback will proceed when getPlaybackState == STATE_READY.
| Returns | |
|---|---|
boolean |
Whether playback will proceed when ready. |
| See also | |
|---|---|
onPlayWhenReadyChanged |
getPlaybackParameters
public final PlaybackParameters getPlaybackParameters()
Returns the currently active playback parameters.
| See also | |
|---|---|
onPlaybackParametersChanged |
getPlaybackState
@Player.State
public final int getPlaybackState()
Returns the current playback state of the player.
| Returns | |
|---|---|
int |
The current |
| See also | |
|---|---|
onPlaybackStateChanged |
getPlaybackSuppressionReason
public final int getPlaybackSuppressionReason()
Returns the reason why playback is suppressed even though getPlayWhenReady is
true, or PLAYBACK_SUPPRESSION_REASON_NONE if playback is not suppressed.
| Returns | |
|---|---|
int |
The current |
| See also | |
|---|---|
onPlaybackSuppressionReasonChanged |
getPlayerError
public final @Nullable PlaybackException getPlayerError()
Returns the error that caused playback to fail. This is the same error that will have been reported via onPlayerError at the time of failure. It can be queried using this method until the player is re-prepared.
Note that this method will always return null if getPlaybackState is not STATE_IDLE.
| Returns | |
|---|---|
@Nullable PlaybackException |
The error, or |
| See also | |
|---|---|
onPlayerError |
getPlaylistMetadata
public final MediaMetadata getPlaylistMetadata()
Returns the playlist MediaMetadata, as set by setPlaylistMetadata, or EMPTY if not supported.
This method must only be called if COMMAND_GET_METADATA is available.
| See also | |
|---|---|
onPlaylistMetadataChanged |
getRepeatMode
@Player.RepeatMode
public final int getRepeatMode()
Returns the current RepeatMode used for playback.
| Returns | |
|---|---|
int |
The current repeat mode. |
| See also | |
|---|---|
onRepeatModeChanged |
getSeekBackIncrement
public final long getSeekBackIncrement()
Returns the seekBack increment.
| Returns | |
|---|---|
long |
The seek back increment, in milliseconds. |
| See also | |
|---|---|
onSeekBackIncrementChanged |
getSeekForwardIncrement
public final long getSeekForwardIncrement()
Returns the seekForward increment.
| Returns | |
|---|---|
long |
The seek forward increment, in milliseconds. |
| See also | |
|---|---|
onSeekForwardIncrementChanged |
getShuffleModeEnabled
public final boolean getShuffleModeEnabled()
Returns whether shuffling of media items is enabled.
| See also | |
|---|---|
onShuffleModeEnabledChanged |
getSurfaceSize
public final Size getSurfaceSize()
Gets the size of the surface on which the video is rendered.
| See also | |
|---|---|
onSurfaceSizeChanged |
getTotalBufferedDuration
public final long getTotalBufferedDuration()
Returns an estimate of the total buffered duration from the current position, in milliseconds. This includes pre-buffered data for subsequent ads and media items.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
getTrackSelectionParameters
public final TrackSelectionParameters getTrackSelectionParameters()
Returns the parameters constraining the track selection.
| See also | |
|---|---|
onTrackSelectionParametersChanged |
} |
getVideoSize
public final VideoSize getVideoSize()
Gets the size of the video.
The video's width and height are 0 if there is no supported video track or its size has not been determined yet.
| See also | |
|---|---|
onVideoSizeChanged |
getVolume
public final float getVolume()
Returns the audio volume, with 0 being silence and 1 being unity gain (signal unchanged).
This method must only be called if COMMAND_GET_VOLUME is available.
| Returns | |
|---|---|
float |
The linear gain applied to all audio channels. |
| See also | |
|---|---|
onVolumeChanged |
increaseDeviceVolume
public final void increaseDeviceVolume(@C.VolumeFlags int flags)
Increases the volume of the device.
The getDeviceVolume device volume cannot be increased above maxVolume, if defined.
Note that this method affects the device volume. To change the volume of the current stream only, use setVolume.
This method must only be called if COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS is available.
| Parameters | |
|---|---|
@C.VolumeFlags int flags |
Either 0 or a bitwise combination of one or more |
isDeviceMuted
public final boolean isDeviceMuted()
Gets whether the device is muted or not.
Note that this method returns the mute state of the device. To check if the current stream is muted, use getVolume() == 0.
This method must only be called if COMMAND_GET_DEVICE_VOLUME is available.
| See also | |
|---|---|
onDeviceVolumeChanged |
isLoading
public final boolean isLoading()
Whether the player is currently loading the source.
| Returns | |
|---|---|
boolean |
Whether the player is currently loading the source. |
| See also | |
|---|---|
onIsLoadingChanged |
isPlayingAd
public final boolean isPlayingAd()
Returns whether the player is currently playing an ad.
This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.
moveMediaItems
public final void moveMediaItems(int fromIndex, int toIndex, int newIndex)
Moves the media item range to the new index.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
int fromIndex |
The start of the range to move. If the index is larger than the size of the playlist, the request is ignored. |
int toIndex |
The first item not to be included in the range (exclusive). If the index is larger than the size of the playlist, items up to the end of the playlist are moved. |
int newIndex |
The new index of the first media item of the range. If the new index is larger than the size of the remaining playlist after removing the range, the range is moved to the end of the playlist. |
mute
public final void mute()
Sets the audio volume to 0.
This method must only be called if COMMAND_SET_VOLUME is available.
prepare
public final void prepare()
Prepares the player.
This method must only be called if COMMAND_PREPARE is available.
This will move the player out of idle state and the player will start loading media and acquire resources needed for playback.
release
public final void release()
Releases the player. This method must be called when the player is no longer required. The player must not be used after calling this method.
This method must only be called if COMMAND_RELEASE is available.
removeListener
public final void removeListener(Player.Listener listener)
Unregister a listener registered through addListener. The listener will no longer receive events.
| Parameters | |
|---|---|
Player.Listener listener |
The listener to unregister. |
removeMediaItems
public final void removeMediaItems(int fromIndex, int toIndex)
Removes a range of media items from the playlist.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
int fromIndex |
The index at which to start removing media items. If the index is larger than the size of the playlist, the request is ignored. |
int toIndex |
The index of the first item to be kept (exclusive). If the index is larger than the size of the playlist, media items up to the end of the playlist are removed. |
replaceMediaItems
public final void replaceMediaItems(
int fromIndex,
int toIndex,
List<MediaItem> mediaItems
)
Replaces the media items at the given range of the playlist.
Implementations of this method may attempt to seamlessly continue playback if the currently playing media item is replaced with a compatible one (e.g. same URL, only metadata has changed).
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
Note that it is possible to replace a range with an arbitrary number of new items, so that the number of removed items defined by fromIndex and toIndex does not have to match the number of added items defined by mediaItems. As result, it may also change the index of subsequent items not touched by this operation.
| Parameters | |
|---|---|
int fromIndex |
The start of the range. If the index is larger than the size of the playlist, the request is ignored. |
int toIndex |
The first item not to be included in the range (exclusive). If the index is larger than the size of the playlist, items up to the end of the playlist are replaced. |
List<MediaItem> mediaItems |
The |
setAudioAttributes
public final void setAudioAttributes(
AudioAttributes audioAttributes,
boolean handleAudioFocus
)
Sets the attributes for audio playback, used by the underlying audio track. If not set, the default audio attributes will be used. They are suitable for general media playback.
Setting the audio attributes during playback may introduce a short gap in audio output as the audio track is recreated. A new audio session id will also be generated.
If tunneling is enabled by the track selector, the specified audio attributes will be ignored, but they will take effect if audio is later played without tunneling.
If audio focus should be handled, the usage must be USAGE_MEDIA or USAGE_GAME. Other usages will throw an IllegalArgumentException.
This method must only be called if COMMAND_SET_AUDIO_ATTRIBUTES is available.
| Parameters | |
|---|---|
AudioAttributes audioAttributes |
The attributes to use for audio playback. |
boolean handleAudioFocus |
True if the player should handle audio focus, false otherwise. |
setDeviceMuted
public final void setDeviceMuted(boolean muted, @C.VolumeFlags int flags)
Sets the mute state of the device.
Note that this method affects the device volume. To mute just the current stream, use
setVolume(0) instead.
This method must only be called if COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS is available.
| Parameters | |
|---|---|
boolean muted |
Whether to set the device to be muted or not |
@C.VolumeFlags int flags |
Either 0 or a bitwise combination of one or more |
setDeviceVolume
public final void setDeviceVolume(int volume, @C.VolumeFlags int flags)
Sets the volume of the device with volume flags.
Note that this method affects the device volume. To change the volume of the current stream only, use setVolume.
This method must only be called if COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS is available.
| Parameters | |
|---|---|
int volume |
The volume to set. |
@C.VolumeFlags int flags |
Either 0 or a bitwise combination of one or more |
setMediaItems
public final void setMediaItems(List<MediaItem> mediaItems, boolean resetPosition)
Clears the playlist and adds the specified media items.
To replace a span of media items (possibly seamlessly) without clearing the playlist, use replaceMediaItems.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
List<MediaItem> mediaItems |
The new |
boolean resetPosition |
Whether the playback position should be reset to the default position in the first |
setMediaItems
public final void setMediaItems(
List<MediaItem> mediaItems,
int startIndex,
long startPositionMs
)
Clears the playlist and adds the specified media items.
To replace a span of media items (possibly seamlessly) without clearing the playlist, use replaceMediaItems.
This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
List<MediaItem> mediaItems |
The new |
int startIndex |
The |
long startPositionMs |
The position in milliseconds to start playback from. If |
| Throws | |
|---|---|
androidx.media3.common.IllegalSeekPositionException |
If the provided |
setPlayWhenReady
public final void setPlayWhenReady(boolean playWhenReady)
Sets whether playback should proceed when getPlaybackState == STATE_READY.
If the player is already in the ready state then this method pauses and resumes playback.
This method must only be called if COMMAND_PLAY_PAUSE is available.
| Parameters | |
|---|---|
boolean playWhenReady |
Whether playback should proceed when ready. |
setPlaybackParameters
public final void setPlaybackParameters(PlaybackParameters playbackParameters)
Attempts to set the playback parameters. Passing DEFAULT resets the player to the default, which means there is no speed or pitch adjustment.
Playback parameters changes may cause the player to buffer. onPlaybackParametersChanged will be called whenever the currently active playback parameters change.
This method must only be called if COMMAND_SET_SPEED_AND_PITCH is available.
| Parameters | |
|---|---|
PlaybackParameters playbackParameters |
The playback parameters. |
setPlaylistMetadata
public final void setPlaylistMetadata(MediaMetadata mediaMetadata)
Sets the playlist MediaMetadata.
This method must only be called if COMMAND_SET_PLAYLIST_METADATA is available.
setRepeatMode
public final void setRepeatMode(@Player.RepeatMode int repeatMode)
Sets the RepeatMode to be used for playback.
This method must only be called if COMMAND_SET_REPEAT_MODE is available.
| Parameters | |
|---|---|
@Player.RepeatMode int repeatMode |
The repeat mode. |
setShuffleModeEnabled
public final void setShuffleModeEnabled(boolean shuffleModeEnabled)
Sets whether shuffling of media items is enabled.
This method must only be called if COMMAND_SET_SHUFFLE_MODE is available.
| Parameters | |
|---|---|
boolean shuffleModeEnabled |
Whether shuffling is enabled. |
setTrackSelectionParameters
public final void setTrackSelectionParameters(TrackSelectionParameters parameters)
Sets the parameters constraining the track selection.
Unsupported parameters will be silently ignored.
Use getTrackSelectionParameters to retrieve the current parameters. For example, the following snippet restricts video to SD whilst keep other track selection parameters unchanged:
player.setTrackSelectionParameters(
player.getTrackSelectionParameters()
.buildUpon()
.setMaxVideoSizeSd()
.build())This method must only be called if COMMAND_SET_TRACK_SELECTION_PARAMETERS is available.
setVideoSurface
public final void setVideoSurface(@Nullable Surface surface)
Sets the Surface onto which video will be rendered. The caller is responsible for tracking the lifecycle of the surface, and must clear the surface by calling
setVideoSurface(null) if the surface is destroyed.
If the surface is held by a SurfaceView, TextureView or SurfaceHolder then it's recommended to use setVideoSurfaceView, setVideoTextureView or setVideoSurfaceHolder rather than this method, since passing the holder allows the player to track the lifecycle of the surface automatically.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
setVideoSurfaceHolder
public final void setVideoSurfaceHolder(@Nullable SurfaceHolder surfaceHolder)
Sets the SurfaceHolder that holds the Surface onto which video will be rendered. The player will track the lifecycle of the surface automatically.
The thread that calls the SurfaceHolder.Callback methods must be the thread associated with getApplicationLooper.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
@Nullable SurfaceHolder surfaceHolder |
The surface holder. |
setVideoSurfaceView
public final void setVideoSurfaceView(@Nullable SurfaceView surfaceView)
Sets the SurfaceView onto which video will be rendered. The player will track the lifecycle of the surface automatically.
The thread that calls the SurfaceHolder.Callback methods must be the thread associated with getApplicationLooper.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
@Nullable SurfaceView surfaceView |
The surface view. |
setVideoTextureView
public final void setVideoTextureView(@Nullable TextureView textureView)
Sets the TextureView onto which video will be rendered. The player will track the lifecycle of the surface automatically.
Consider using SurfaceView via setVideoSurfaceView instead of TextureView. SurfaceView generally causes lower battery consumption, and has better handling for HDR and secure content. See Choosing a surface type for more information.
The thread that calls the TextureView.SurfaceTextureListener methods must be the thread associated with getApplicationLooper.
This method must only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
@Nullable TextureView textureView |
The texture view. |
setVolume
public final void setVolume(float volume)
Sets the audio volume, valid values are between 0 (silence) and 1 (unity gain, signal unchanged), inclusive.
This method must only be called if COMMAND_SET_VOLUME is available.
| Parameters | |
|---|---|
float volume |
Linear output gain to apply to all audio channels. |
stop
public final void stop()
Stops playback without resetting the playlist. Use pause rather than this method if the intention is to pause playback.
Calling this method will cause the playback state to transition to STATE_IDLE and the player will release the loaded media and resources required for playback. The player instance can still be used by calling prepare again, and release must still be called on the player if it's no longer required.
Calling this method does not clear the playlist, reset the playback position or the playback error.
This method must only be called if COMMAND_STOP is available.
unmute
public final void unmute()
If the audio volume is 0, sets the audio volume to a non-zero value decided by the Player to be the most appropriate.
This method must only be called if COMMAND_SET_VOLUME is available.
Protected methods
getPlaceholderMediaItemData
@ForOverride
protected SimpleBasePlayer.MediaItemData getPlaceholderMediaItemData(MediaItem mediaItem)
Returns the placeholder MediaItemData used for a new MediaItem added to the playlist.
An implementation only needs to override this method if it can determine a more accurate placeholder state than the default.
| Returns | |
|---|---|
SimpleBasePlayer.MediaItemData |
The |
getPlaceholderState
@ForOverride
protected SimpleBasePlayer.State getPlaceholderState(SimpleBasePlayer.State suggestedPlaceholderState)
Returns the placeholder state used while a player method is handled asynchronously.
The suggestedPlaceholderState already contains the most likely state update, for example setting playWhenReady to true if player.setPlayWhenReady(true) is called, and an implementations only needs to override this method if it can determine a more accurate placeholder state.
| Parameters | |
|---|---|
SimpleBasePlayer.State suggestedPlaceholderState |
The suggested placeholder |
| Returns | |
|---|---|
SimpleBasePlayer.State |
The placeholder |
getState
@ForOverride
protected abstract SimpleBasePlayer.State getState()
Returns the current State of the player.
The State should include all available commands indicating which player methods are allowed to be called.
Note that this method won't be called while asynchronous handling of player methods is in progress. This means that the implementation doesn't need to handle state changes caused by these asynchronous operations until they are done and can return the currently known state directly. The placeholder state used while these asynchronous operations are in progress can be customized by overriding getPlaceholderState if required.
handleAddMediaItems
@ForOverride
protected ListenableFuture<Object> handleAddMediaItems(int index, List<MediaItem> mediaItems)
Handles calls to addMediaItem and addMediaItems.
Will only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
int index |
The index at which to add the items. The index is in the range 0 <= |
List<MediaItem> mediaItems |
The media items to add. |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleClearVideoOutput
@ForOverride
protected ListenableFuture<Object> handleClearVideoOutput(@Nullable Object videoOutput)
Handles calls to clear the video output.
Will only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
@Nullable Object videoOutput |
The video output to clear. If null any current output should be cleared. If non-null, the output should only be cleared if it matches the provided argument. This is either a |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleDecreaseDeviceVolume
@ForOverride
protected ListenableFuture<Object> handleDecreaseDeviceVolume(@C.VolumeFlags int flags)
Handles calls to decreaseDeviceVolume and decreaseDeviceVolume.
Will only be called if COMMAND_ADJUST_DEVICE_VOLUME or COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS is available.
| Parameters | |
|---|---|
@C.VolumeFlags int flags |
Either 0 or a bitwise combination of one or more |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleIncreaseDeviceVolume
@ForOverride
protected ListenableFuture<Object> handleIncreaseDeviceVolume(@C.VolumeFlags int flags)
Handles calls to increaseDeviceVolume and increaseDeviceVolume.
Will only be called if COMMAND_ADJUST_DEVICE_VOLUME or COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS is available.
| Parameters | |
|---|---|
@C.VolumeFlags int flags |
Either 0 or a bitwise combination of one or more |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleMoveMediaItems
@ForOverride
protected ListenableFuture<Object> handleMoveMediaItems(int fromIndex, int toIndex, int newIndex)
Handles calls to moveMediaItem and moveMediaItems.
Will only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
int fromIndex |
The start index of the items to move. The index is in the range 0 <= |
int toIndex |
The index of the first item not to be included in the move (exclusive). The index is in the range |
int newIndex |
The new index of the first moved item. The index is in the range |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handlePrepare
@ForOverride
protected ListenableFuture<Object> handlePrepare()
Handles calls to prepare.
Will only be called if COMMAND_PREPARE is available.
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleRelease
@ForOverride
protected ListenableFuture<Object> handleRelease()
Handles calls to release.
Will only be called if COMMAND_RELEASE is available.
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleRemoveMediaItems
@ForOverride
protected ListenableFuture<Object> handleRemoveMediaItems(int fromIndex, int toIndex)
Handles calls to removeMediaItem and removeMediaItems.
Will only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
int fromIndex |
The index at which to start removing media items. The index is in the range 0 <= |
int toIndex |
The index of the first item to be kept (exclusive). The index is in the range |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleReplaceMediaItems
@ForOverride
protected ListenableFuture<Object> handleReplaceMediaItems(
int fromIndex,
int toIndex,
List<MediaItem> mediaItems
)
Handles calls to replaceMediaItem and replaceMediaItems.
Will only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.
| Parameters | |
|---|---|
int fromIndex |
The start index of the items to replace. The index is in the range 0 <= |
int toIndex |
The index of the first item not to be replaced (exclusive). The index is in the range |
List<MediaItem> mediaItems |
The media items to replace the specified range with. |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSeek
@ForOverride
protected ListenableFuture<Object> handleSeek(
int mediaItemIndex,
long positionMs,
@Player.Command int seekCommand
)
Handles calls to seekTo and other seek operations (for example, seekToNext).
Will only be called if the appropriate Player.Command, for example COMMAND_SEEK_TO_MEDIA_ITEM or COMMAND_SEEK_TO_NEXT, is available.
| Parameters | |
|---|---|
int mediaItemIndex |
The media item index to seek to. If the original seek operation did not directly specify an index, this is the most likely implied index based on the available player state. If the implied action is to do nothing, this will be |
long positionMs |
The position in milliseconds to start playback from, or |
@Player.Command int seekCommand |
The |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSetAudioAttributes
@ForOverride
protected ListenableFuture<Object> handleSetAudioAttributes(
AudioAttributes audioAttributes,
boolean handleAudioFocus
)
Handles calls to set the audio attributes.
Will only be called if COMMAND_SET_AUDIO_ATTRIBUTES is available.
| Parameters | |
|---|---|
AudioAttributes audioAttributes |
The attributes to use for audio playback. |
boolean handleAudioFocus |
True if the player should handle audio focus, false otherwise. |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSetDeviceMuted
@ForOverride
protected ListenableFuture<Object> handleSetDeviceMuted(boolean muted, @C.VolumeFlags int flags)
Handles calls to setDeviceMuted and setDeviceMuted.
Will only be called if COMMAND_ADJUST_DEVICE_VOLUME or COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS is available.
| Parameters | |
|---|---|
boolean muted |
Whether the device was requested to be muted. |
@C.VolumeFlags int flags |
Either 0 or a bitwise combination of one or more |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSetDeviceVolume
@ForOverride
protected ListenableFuture<Object> handleSetDeviceVolume(
@IntRange(from = 0) int deviceVolume,
@C.VolumeFlags int flags
)
Handles calls to setDeviceVolume and setDeviceVolume.
Will only be called if COMMAND_SET_DEVICE_VOLUME or COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS is available.
| Parameters | |
|---|---|
@IntRange(from = 0) int deviceVolume |
The requested device volume. |
@C.VolumeFlags int flags |
Either 0 or a bitwise combination of one or more |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSetMediaItems
@ForOverride
protected ListenableFuture<Object> handleSetMediaItems(
List<MediaItem> mediaItems,
int startIndex,
long startPositionMs
)
Handles calls to setMediaItem and setMediaItems.
Will only be called if COMMAND_SET_MEDIA_ITEM or COMMAND_CHANGE_MEDIA_ITEMS is available. If only COMMAND_SET_MEDIA_ITEM is available, the list of media items will always contain exactly one item.
| Parameters | |
|---|---|
List<MediaItem> mediaItems |
The media items to add. |
int startIndex |
The index at which to start playback from, or |
long startPositionMs |
The position in milliseconds to start playback from, or |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSetPlayWhenReady
@ForOverride
protected ListenableFuture<Object> handleSetPlayWhenReady(boolean playWhenReady)
Handles calls to setPlayWhenReady, play and pause.
Will only be called if COMMAND_PLAY_PAUSE is available.
| Parameters | |
|---|---|
boolean playWhenReady |
The requested |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSetPlaybackParameters
@ForOverride
protected ListenableFuture<Object> handleSetPlaybackParameters(PlaybackParameters playbackParameters)
Handles calls to setPlaybackParameters or setPlaybackSpeed.
Will only be called if COMMAND_SET_SPEED_AND_PITCH is available.
| Parameters | |
|---|---|
PlaybackParameters playbackParameters |
The requested |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSetPlaylistMetadata
@ForOverride
protected ListenableFuture<Object> handleSetPlaylistMetadata(MediaMetadata playlistMetadata)
Handles calls to setPlaylistMetadata.
Will only be called if COMMAND_SET_PLAYLIST_METADATA is available.
| Parameters | |
|---|---|
MediaMetadata playlistMetadata |
The requested |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSetRepeatMode
@ForOverride
protected ListenableFuture<Object> handleSetRepeatMode(@Player.RepeatMode int repeatMode)
Handles calls to setRepeatMode.
Will only be called if COMMAND_SET_REPEAT_MODE is available.
| Parameters | |
|---|---|
@Player.RepeatMode int repeatMode |
The requested |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSetShuffleModeEnabled
@ForOverride
protected ListenableFuture<Object> handleSetShuffleModeEnabled(boolean shuffleModeEnabled)
Handles calls to setShuffleModeEnabled.
Will only be called if COMMAND_SET_SHUFFLE_MODE is available.
| Parameters | |
|---|---|
boolean shuffleModeEnabled |
Whether shuffle mode was requested to be enabled. |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSetTrackSelectionParameters
@ForOverride
protected ListenableFuture<Object> handleSetTrackSelectionParameters(
TrackSelectionParameters trackSelectionParameters
)
Handles calls to setTrackSelectionParameters.
Will only be called if COMMAND_SET_TRACK_SELECTION_PARAMETERS is available.
| Parameters | |
|---|---|
TrackSelectionParameters trackSelectionParameters |
The requested |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSetVideoOutput
@ForOverride
protected ListenableFuture<Object> handleSetVideoOutput(Object videoOutput)
Handles calls to set the video output.
Will only be called if COMMAND_SET_VIDEO_SURFACE is available.
| Parameters | |
|---|---|
Object videoOutput |
The requested video output. This is either a |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleSetVolume
@ForOverride
protected ListenableFuture<Object>handleSetVolume(@FloatRange(from = 0, to = 1.0) float volume)
handleSetVolume
@ForOverride
protected ListenableFuture<Object> handleSetVolume(
@FloatRange(from = 0, to = 1.0) float volume,
@C.VolumeOperationType int volumeOperationType
)
Handles calls to setVolume.
Will only be called if COMMAND_SET_VOLUME is available.
| Parameters | |
|---|---|
@FloatRange(from = 0, to = 1.0) float volume |
The requested audio volume, with 0 being silence and 1 being unity gain (signal unchanged). |
@C.VolumeOperationType int volumeOperationType |
The |
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
handleStop
@ForOverride
protected ListenableFuture<Object> handleStop()
Handles calls to stop.
Will only be called if COMMAND_STOP is available.
| Returns | |
|---|---|
ListenableFuture<Object> |
A |
invalidateState
protected final void invalidateState()
Invalidates the current state.
Triggers a call to getState and informs listeners if the state changed.
Note that this may not have an immediate effect while there are still player methods being handled asynchronously. The state will be invalidated automatically once these pending synchronous operations are finished and there is no need to call this method again.
seekTo
protected final void seekTo(
int mediaItemIndex,
long positionMs,
@Player.Command int seekCommand,
boolean isRepeatingCurrentItem
)
Seeks to a position in the specified MediaItem.
| Parameters | |
|---|---|
int mediaItemIndex |
The index of the |
long positionMs |
The seek position in the specified |
@Player.Command int seekCommand |
The |
boolean isRepeatingCurrentItem |
Whether this seeks repeats the current item. |
verifyApplicationThread
protected final void verifyApplicationThread()
Throws an IllegalStateException if the the thread calling this method does not match the Looper thread that was specified upon construction of this instance.
Subclasses can use this method to verify that their own defined methods are also accessed by the correct thread.