@UnstableApi
public final class CastPlayer extends BasePlayer


Player implementation that communicates with a Cast receiver app.

The behavior of this class depends on the underlying Cast session, which is obtained from the injected CastContext. To keep track of the session, isCastSessionAvailable can be queried and SessionAvailabilityListener can be implemented and attached to the player.

If no session is available, the player state will remain unchanged and calls to methods that alter it will be ignored. Querying the player state is possible even when no session is available, in which case, the last observed receiver app state is reported.

Methods should be called on the application's main thread.

Summary

Constants

static final DeviceInfo

A remoteDeviceInfo with a null routingControllerId.

static final float
static final float

Public constructors

CastPlayer(CastContext castContext)

Creates a new cast player.

CastPlayer(
    CastContext castContext,
    MediaItemConverter mediaItemConverter
)

Creates a new cast player.

CastPlayer(
    CastContext castContext,
    MediaItemConverter mediaItemConverter,
    @IntRange(from = 1) long seekBackIncrementMs,
    @IntRange(from = 1) long seekForwardIncrementMs
)

Creates a new cast player.

CastPlayer(
    @Nullable Context context,
    CastContext castContext,
    MediaItemConverter mediaItemConverter,
    @IntRange(from = 1) long seekBackIncrementMs,
    @IntRange(from = 1) long seekForwardIncrementMs,
    @IntRange(from = 0) long maxSeekToPreviousPositionMs
)

Creates a new cast player.

Public methods

void

Registers a listener to receive all events from the player.

void
addMediaItems(int index, List<MediaItem> mediaItems)

Adds a list of media items at the given index of the playlist.

void

This method is not supported and does nothing.

void

This method is not supported and does nothing.

void

This method is not supported and does nothing.

void

This method is not supported and does nothing.

void

This method is not supported and does nothing.

void

This method is deprecated.

Use decreaseDeviceVolume instead.

void

This method is not supported and does nothing.

Looper

Returns the Looper associated with the application thread that's used to access the player and on which player events are received.

AudioAttributes

This method is not supported and returns DEFAULT.

Player.Commands

Returns the player's currently available Commands.

long

Returns an estimate of the position in the current content or ad up to which data is buffered, in milliseconds.

long

If isPlayingAd returns true, returns an estimate of the content position in the current content up to which data is buffered, in milliseconds.

long

If isPlayingAd returns true, returns the content position that will be played once all ads in the ad group have finished playing, in milliseconds.

int

If isPlayingAd returns true, returns the index of the ad group in the period currently being played.

int

If isPlayingAd returns true, returns the index of the ad in its ad group.

CueGroup

This method is not supported and returns an empty CueGroup.

int

Returns the index of the current MediaItem in the timeline, or the prospective index if the current timeline is empty.

int

Returns the index of the period currently being played.

long

Returns the playback position in the current content or ad, in milliseconds, or the prospective position in milliseconds if the current timeline is empty.

Timeline

Returns the current Timeline.

Tracks

Returns the current tracks.

DeviceInfo

Returns a DeviceInfo describing the receiver device.

int

This method is not supported and always returns 0.

long

Returns the duration of the current content or ad in milliseconds, or TIME_UNSET if the duration is not known.

@Nullable MediaQueueItem
getItem(int periodId)

Returns the item that corresponds to the period with the given id, or null if no media queue or period with id periodId exist.

long

Returns the maximum position for which seekToPrevious seeks to the previous , in milliseconds.

MediaMetadata

Returns the current combined MediaMetadata, or EMPTY if not supported.

MediaMetadata
boolean

Whether playback will proceed when getPlaybackState == STATE_READY.

PlaybackParameters

Returns the currently active playback parameters.

int

Returns the current playback state of the player.

int

Returns the reason why playback is suppressed even though getPlayWhenReady is true, or PLAYBACK_SUPPRESSION_REASON_NONE if playback is not suppressed.

@Nullable PlaybackException

Returns the error that caused playback to fail.

MediaMetadata

Returns the playlist MediaMetadata, as set by setPlaylistMetadata, or EMPTY if not supported.

int

Returns the current RepeatMode used for playback.

long

Returns the seekBack increment.

long

Returns the seekForward increment.

boolean

Returns whether shuffling of media items is enabled.

Size

This method is not supported and returns UNKNOWN.

long

Returns an estimate of the total buffered duration from the current position, in milliseconds.

TrackSelectionParameters

Returns the parameters constraining the track selection.

VideoSize

This method is not supported and returns UNKNOWN.

float

This method is not supported and returns 1.

void

This method is deprecated.

Use increaseDeviceVolume instead.

void

This method is not supported and does nothing.

boolean

Returns whether a cast session is available.

boolean

This method is not supported and always returns false.

boolean

Whether the player is currently loading the source.

boolean

Returns whether the player is currently playing an ad.

void
moveMediaItems(int fromIndex, int toIndex, int newIndex)

Moves the media item range to the new index.

void

Prepares the player.

void

Releases the player.

void

Unregister a listener registered through addListener.

void
removeMediaItems(int fromIndex, int toIndex)

Removes a range of media items from the playlist.

void
replaceMediaItems(
    int fromIndex,
    int toIndex,
    List<MediaItem> mediaItems
)

Replaces the media items at the given range of the playlist.

void
@VisibleForTesting(otherwise = 4)
seekTo(
    int mediaItemIndex,
    long positionMs,
    @Player.Command int seekCommand,
    boolean isRepeatingCurrentItem
)

Seeks to a position in the specified MediaItem.

void
setAudioAttributes(
    AudioAttributes audioAttributes,
    boolean handleAudioFocus
)

This method is not supported and does nothing.

void
setDeviceMuted(boolean muted)

This method is deprecated.

Use setDeviceMuted instead.

void
setDeviceMuted(boolean muted, @C.VolumeFlags int flags)

This method is not supported and does nothing.

void
setDeviceVolume(int volume)

This method is deprecated.

Use setDeviceVolume instead.

void
setDeviceVolume(int volume, @C.VolumeFlags int flags)

This method is not supported and does nothing.

void
setMediaItems(List<MediaItem> mediaItems, boolean resetPosition)

Clears the playlist and adds the specified media items.

void
setMediaItems(
    List<MediaItem> mediaItems,
    int startIndex,
    long startPositionMs
)

Clears the playlist and adds the specified media items.

void
setPlayWhenReady(boolean playWhenReady)

Sets whether playback should proceed when getPlaybackState == STATE_READY.

void

Attempts to set the playback parameters.

void

This method is not supported and does nothing.

void

Sets the RepeatMode to be used for playback.

void

Sets a listener for updates on the cast session availability.

void
setShuffleModeEnabled(boolean shuffleModeEnabled)

Sets whether shuffling of media items is enabled.

void

Sets the parameters constraining the track selection.

void

This method is not supported and does nothing.

void

This method is not supported and does nothing.

void

This method is not supported and does nothing.

void

This method is not supported and does nothing.

void
setVolume(float volume)

This method is not supported and does nothing.

void

Stops playback without resetting the playlist.

Inherited Constants

From androidx.media3.common.Player
static final int

This field is deprecated.

Use COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS instead.

static final int

Command to increase and decrease the device volume and mute it with volume flags.

static final int

Command to change the media items in the playlist.

static final int

Command to get the player current AudioAttributes.

static final int

Command to get information about the currently playing MediaItem.

static final int

Command to get the device volume and whether it is muted.

static final int

This field is deprecated.

Use COMMAND_GET_METADATA instead.

static final int

Command to get metadata related to the playlist and current MediaItem.

static final int

Command to get the text that should currently be displayed by the player.

static final int

Command to get the information about the current timeline.

static final int

Command to get details of the current track selection.

static final int

Command to get the player volume.

static final int

Represents an invalid Command.

static final int

Command to start, pause or resume playback.

static final int

Command to prepare the player.

static final int

Command to release the player.

static final int

Command to seek back by a fixed increment inside the current MediaItem.

static final int

Command to seek forward by a fixed increment inside the current MediaItem.

static final int

Command to seek anywhere inside the current MediaItem.

static final int

This field is deprecated.

Use COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM instead.

static final int

Command to seek to the default position of the current MediaItem.

static final int

Command to seek anywhere in any MediaItem.

static final int

Command to seek to a later position in the current MediaItem or the default position of the next MediaItem.

static final int

Command to seek to the default position of the next MediaItem.

static final int

This field is deprecated.

Use COMMAND_SEEK_TO_NEXT_MEDIA_ITEM instead.

static final int

Command to seek to an earlier position in the current MediaItem or the default position of the previous MediaItem.

static final int

Command to seek to the default position of the previous MediaItem.

static final int

This field is deprecated.

Use COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM instead.

static final int

This field is deprecated.

Use COMMAND_SEEK_TO_MEDIA_ITEM instead.

static final int

Command to set the player's audio attributes.

static final int

This field is deprecated.

Use COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS instead.

static final int

Command to set the device volume with volume flags.

static final int

Command to set a MediaItem.

static final int

This field is deprecated.

Use COMMAND_SET_PLAYLIST_METADATA instead.

static final int

Command to set the playlist metadata.

static final int

Command to set the repeat mode.

static final int

Command to enable shuffling.

static final int

Command to set the playback speed and pitch.

static final int

Command to set the player's track selection parameters.

static final int

Command to set and clear the surface on which to render the video.

static final int

Command to set the player volume.

static final int

Command to stop playback.

static final int

Automatic playback transition from one period in the timeline to the next.

static final int

Discontinuity introduced internally (e.g. by the source).

static final int

Discontinuity caused by the removal of the current period from the Timeline.

static final int

Seek within the current period or to another period.

static final int

Seek adjustment due to being unable to seek to the requested position or because the seek was permitted to be inexact.

static final int

Discontinuity introduced by a skipped silence.

static final int

Discontinuity introduced by a skipped period (for instance a skipped ad).

static final int

getAudioAttributes changed.

static final int

The audio session id was set.

static final int

isCommandAvailable changed for at least one Command.

static final int

getCurrentCues changed.

static final int

getDeviceInfo changed.

static final int

getDeviceVolume changed.

static final int

isLoading ()} changed.

static final int

isPlaying changed.

static final int

getMaxSeekToPreviousPosition changed.

static final int

getCurrentMediaItem changed or the player started repeating the current item.

static final int

getMediaMetadata changed.

static final int

Metadata associated with the current playback time changed.

static final int

getPlaybackParameters changed.

static final int

getPlaybackState changed.

static final int

getPlaybackSuppressionReason changed.

static final int

getPlayerError changed.

static final int

getPlaylistMetadata changed.

static final int

getPlayWhenReady changed.

static final int

A position discontinuity occurred.

static final int

A frame is rendered for the first time since setting the surface, or since the renderer was reset, or since the stream being rendered was changed.

static final int

getRepeatMode changed.

static final int

getSeekBackIncrement changed.

static final int

getSeekForwardIncrement changed.

static final int

getShuffleModeEnabled changed.

static final int

Skipping silences in the audio stream is enabled or disabled.

static final int

The size of the surface onto which the video is being rendered changed.

static final int

getCurrentTimeline changed.

static final int

getCurrentTracks changed.

static final int

getTrackSelectionParameters changed.

static final int

getVideoSize changed.

static final int

getVolume changed.

static final int

Playback has automatically transitioned to the next media item.

static final int

The current media item has changed because of a change in the playlist.

static final int

The media item has been repeated.

static final int

A seek to another media item has occurred.

static final int

Playback is not suppressed.

static final int

Playback is suppressed due to transient audio focus loss.

static final int

Playback is suppressed due to attempt to play on an unsuitable audio output (e.g. attempt to play on built-in speaker on a Wear OS device).

static final int

This field is deprecated.

Use PLAYBACK_SUPPRESSION_REASON_UNSUITABLE_AUDIO_OUTPUT instead.

static final int

Playback has been paused to avoid becoming noisy.

static final int

Playback has been paused because of a loss of audio focus.

static final int

Playback has been paused at the end of a media item.

static final int

Playback has been started or paused because of a remote change.

static final int

Playback has been paused because playback has been suppressed too long.

static final int

Playback has been started or paused by a call to setPlayWhenReady.

static final int

Repeats the entire timeline infinitely.

static final int

Normal playback without repetition.

static final int

Repeats the currently playing MediaItem infinitely during ongoing playback.

static final int

The player is not able to immediately play the media, but is doing work toward being able to do so.

static final int

The player has finished playing the media.

static final int

The player is idle, meaning it holds only limited resources.

static final int

The player is able to immediately play from its current position.

static final int

Timeline changed as a result of a change of the playlist items or the order of the items.

static final int

Timeline changed as a result of a source update (e.g. result of a dynamic update by the played media).

Inherited methods

From androidx.media3.common.BasePlayer
final void

Adds a media item to the end of the playlist.

final void
addMediaItem(int index, MediaItem mediaItem)

Adds a media item at the given index of the playlist.

final void

Adds a list of media items to the end of the playlist.

final boolean

Returns whether the player can be used to advertise a media session.

final void

Clears the playlist.

final int

Returns an estimate of the percentage in the current content or ad up to which data is buffered, or 0 if no estimate is available.

final long

If isPlayingAd returns true, returns the duration of the current content in milliseconds, or TIME_UNSET if the duration is not known.

final long

Returns the offset of the current playback position from the live edge in milliseconds, or TIME_UNSET if the current MediaItemisn't live or the offset is unknown.

final @Nullable Object

Returns the current manifest.

final @Nullable MediaItem

Returns the currently playing MediaItem.

final int

This method is deprecated.

Use getCurrentMediaItemIndex instead.

final MediaItem
getMediaItemAt(int index)

Returns the MediaItem at the given index.

final int

Returns the number of media items in the playlist.

final int

Returns the index of the MediaItem that will be played if seekToNextMediaItem is called, which may depend on the current repeat mode and whether shuffle mode is enabled.

final int

This method is deprecated.

Use getNextMediaItemIndex instead.

final int

Returns the index of the MediaItem that will be played if seekToPreviousMediaItem is called, which may depend on the current repeat mode and whether shuffle mode is enabled.

final int

This method is deprecated.

Use getPreviousMediaItemIndex instead.

final boolean

This method is deprecated.

Use hasNextMediaItem instead.

final boolean

Returns whether a next MediaItem exists, which may depend on the current repeat mode and whether shuffle mode is enabled.

final boolean

This method is deprecated.

Use hasNextMediaItem instead.

final boolean

Returns whether a previous media item exists, which may depend on the current repeat mode and whether shuffle mode is enabled.

final boolean

Returns whether the provided Command is available.

final boolean

Returns whether the current MediaItem is dynamic (may change when the Timeline is updated), or false if the Timeline is empty.

final boolean

Returns whether the current MediaItem is live, or false if the Timeline is empty.

final boolean

Returns whether the current MediaItem is seekable, or false if the is empty.

final boolean

This method is deprecated.

Use isCurrentMediaItemDynamic instead.

final boolean

This method is deprecated.

Use isCurrentMediaItemLive instead.

final boolean

This method is deprecated.

Use isCurrentMediaItemSeekable instead.

final boolean

Returns whether the player is playing, i.e. getCurrentPosition is advancing.

final void
moveMediaItem(int currentIndex, int newIndex)

Moves the media item at the current index to the new index.

final void

This method is deprecated.

Use seekToNextMediaItem instead.

final void

Pauses playback.

final void

Resumes playback as soon as getPlaybackState == STATE_READY.

final void
removeMediaItem(int index)

Removes the media item at the given index of the playlist.

final void
replaceMediaItem(int index, MediaItem mediaItem)

Replaces the media item at the given index of the playlist.

final void

Seeks back in the current MediaItem by getSeekBackIncrement milliseconds.

final void

Seeks forward in the current MediaItem by getSeekForwardIncrement milliseconds.

final void
seekTo(long positionMs)

Seeks to a position specified in milliseconds in the current MediaItem.

final void
seekTo(int mediaItemIndex, long positionMs)

Seeks to a position specified in milliseconds in the specified MediaItem.

final void

Seeks to the default position associated with the current MediaItem.

final void
seekToDefaultPosition(int mediaItemIndex)

Seeks to the default position associated with the specified MediaItem.

final void

Seeks to a later position in the current or next MediaItem (if available).

final void

Seeks to the default position of the next MediaItem, which may depend on the current repeat mode and whether shuffle mode is enabled.

final void

This method is deprecated.

Use seekToNextMediaItem instead.

final void

Seeks to an earlier position in the current or previous MediaItem (if available).

final void

Seeks to the default position of the previous MediaItem, which may depend on the current repeat mode and whether shuffle mode is enabled.

final void

This method is deprecated.

Use seekToPreviousMediaItem instead.

final void

Clears the playlist, adds the specified MediaItem and resets the position to the default position.

final void
setMediaItem(MediaItem mediaItem, boolean resetPosition)

Clears the playlist and adds the specified MediaItem.

final void
setMediaItem(MediaItem mediaItem, long startPositionMs)

Clears the playlist and adds the specified MediaItem.

final void

Clears the playlist, adds the specified media items and resets the position to the default position.

final void
setPlaybackSpeed(float speed)

Changes the rate at which playback occurs.

From androidx.media3.common.Player
abstract void
setPlaybackSpeed(@FloatRange(from = 0, fromInclusive = false) float speed)

Changes the rate at which playback occurs.

Constants

DEVICE_INFO_REMOTE_EMPTY

public static final DeviceInfo DEVICE_INFO_REMOTE_EMPTY

A remoteDeviceInfo with a null routingControllerId.

MAX_SPEED_SUPPORTED

public static final float MAX_SPEED_SUPPORTED = 2.0f

MIN_SPEED_SUPPORTED

public static final float MIN_SPEED_SUPPORTED = 0.5f

Public fields

availableCommands

public Player.Commands availableCommands

currentTracks

public Tracks currentTracks

deviceInfo

public DeviceInfo deviceInfo

mediaMetadata

public MediaMetadata mediaMetadata

playbackState

@Player.State
public int playbackState

Public constructors

CastPlayer

public CastPlayer(CastContext castContext)

Creates a new cast player.

The returned player uses a DefaultMediaItemConverter and

mediaItemConverter is set to a DefaultMediaItemConverter, seekBackIncrementMs is set to DEFAULT_SEEK_BACK_INCREMENT_MS and seekForwardIncrementMs is set to DEFAULT_SEEK_FORWARD_INCREMENT_MS.

Parameters
CastContext castContext

The context from which the cast session is obtained.

CastPlayer

public CastPlayer(
    CastContext castContext,
    MediaItemConverter mediaItemConverter
)

Creates a new cast player.

seekBackIncrementMs is set to DEFAULT_SEEK_BACK_INCREMENT_MS and seekForwardIncrementMs is set to DEFAULT_SEEK_FORWARD_INCREMENT_MS.

Parameters
CastContext castContext

The context from which the cast session is obtained.

MediaItemConverter mediaItemConverter

The MediaItemConverter to use.

CastPlayer

public CastPlayer(
    CastContext castContext,
    MediaItemConverter mediaItemConverter,
    @IntRange(from = 1) long seekBackIncrementMs,
    @IntRange(from = 1) long seekForwardIncrementMs
)

Creates a new cast player.

Parameters
CastContext castContext

The context from which the cast session is obtained.

MediaItemConverter mediaItemConverter

The MediaItemConverter to use.

@IntRange(from = 1) long seekBackIncrementMs

The seekBack increment, in milliseconds.

@IntRange(from = 1) long seekForwardIncrementMs

The seekForward increment, in milliseconds.

Throws
java.lang.IllegalArgumentException

If seekBackIncrementMs or seekForwardIncrementMs is non-positive.

CastPlayer

public CastPlayer(
    @Nullable Context context,
    CastContext castContext,
    MediaItemConverter mediaItemConverter,
    @IntRange(from = 1) long seekBackIncrementMs,
    @IntRange(from = 1) long seekForwardIncrementMs,
    @IntRange(from = 0) long maxSeekToPreviousPositionMs
)

Creates a new cast player.

Parameters
@Nullable Context context

A Context used to populate getDeviceInfo. If null, getDeviceInfo will always return DEVICE_INFO_REMOTE_EMPTY.

CastContext castContext

The context from which the cast session is obtained.

MediaItemConverter mediaItemConverter

The MediaItemConverter to use.

@IntRange(from = 1) long seekBackIncrementMs

The seekBack increment, in milliseconds.

@IntRange(from = 1) long seekForwardIncrementMs

The seekForward increment, in milliseconds.

@IntRange(from = 0) long maxSeekToPreviousPositionMs

The maximum position for which seekToPrevious seeks to the previous MediaItem, in milliseconds.

Throws
java.lang.IllegalArgumentException

If seekBackIncrementMs or seekForwardIncrementMs is non-positive, or if maxSeekToPreviousPositionMs is negative.

Public methods

addListener

public 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 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 media items to add.

clearVideoSurface

public void clearVideoSurface()

This method is not supported and does nothing.

clearVideoSurface

public void clearVideoSurface(@Nullable Surface surface)

This method is not supported and does nothing.

clearVideoSurfaceHolder

public void clearVideoSurfaceHolder(@Nullable SurfaceHolder surfaceHolder)

This method is not supported and does nothing.

clearVideoSurfaceView

public void clearVideoSurfaceView(@Nullable SurfaceView surfaceView)

This method is not supported and does nothing.

clearVideoTextureView

public void clearVideoTextureView(@Nullable TextureView textureView)

This method is not supported and does nothing.

decreaseDeviceVolume

public void decreaseDeviceVolume()

decreaseDeviceVolume

public void decreaseDeviceVolume(@C.VolumeFlags int flags)

This method is not supported and does nothing.

getApplicationLooper

public 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 AudioAttributes getAudioAttributes()

This method is not supported and returns DEFAULT.

getAvailableCommands

public 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 Commands.

getBufferedPosition

public 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 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 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 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 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 CueGroup getCurrentCues()

This method is not supported and returns an empty CueGroup.

getCurrentMediaItemIndex

public 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 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 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 Timeline getCurrentTimeline()

Returns the current Timeline. Never null, but may be empty.

This method must only be called if COMMAND_GET_TIMELINE is available.

getCurrentTracks

public Tracks getCurrentTracks()

Returns the current tracks.

This method must only be called if COMMAND_GET_TRACKS is available.

See also
onTracksChanged

getDeviceInfo

public DeviceInfo getDeviceInfo()

Returns a DeviceInfo describing the receiver device. Returns DEVICE_INFO_REMOTE_EMPTY if no Context was provided at construction, or if the Cast RoutingController could not be identified.

getDeviceVolume

public int getDeviceVolume()

This method is not supported and always returns 0.

getDuration

public 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.

getItem

public @Nullable MediaQueueItem getItem(int periodId)

Returns the item that corresponds to the period with the given id, or null if no media queue or period with id periodId exist.

Parameters
int periodId

The id of the period (getCurrentTimeline) that corresponds to the item to get.

Returns
@Nullable MediaQueueItem

The item that corresponds to the period with the given id, or null if no media queue or period with id periodId exist.

getMaxSeekToPreviousPosition

public long getMaxSeekToPreviousPosition()

Returns the maximum position for which seekToPrevious seeks to the previous , in milliseconds.

Returns
long

The maximum seek to previous position, in milliseconds.

getMediaMetadata

public 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.

getMediaMetadataInternal

public MediaMetadata getMediaMetadataInternal()

getPlayWhenReady

public boolean getPlayWhenReady()

Whether playback will proceed when getPlaybackState == STATE_READY.

Returns
boolean

Whether playback will proceed when ready.

getPlaybackParameters

public PlaybackParameters getPlaybackParameters()

Returns the currently active playback parameters.

getPlaybackState

@Player.State
public int getPlaybackState()

Returns the current playback state of the player.

Returns
int

The current playback state.

getPlaybackSuppressionReason

@Player.PlaybackSuppressionReason
public 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 PlaybackSuppressionReason.

getPlayerError

public @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 null.

See also
onPlayerError

getPlaylistMetadata

public 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.

getRepeatMode

@Player.RepeatMode
public int getRepeatMode()

Returns the current RepeatMode used for playback.

Returns
int

The current repeat mode.

getSeekBackIncrement

public long getSeekBackIncrement()

Returns the seekBack increment.

Returns
long

The seek back increment, in milliseconds.

getSeekForwardIncrement

public long getSeekForwardIncrement()

Returns the seekForward increment.

Returns
long

The seek forward increment, in milliseconds.

getShuffleModeEnabled

public boolean getShuffleModeEnabled()

Returns whether shuffling of media items is enabled.

getSurfaceSize

public Size getSurfaceSize()

This method is not supported and returns UNKNOWN.

getTotalBufferedDuration

public 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 TrackSelectionParameters getTrackSelectionParameters()

Returns the parameters constraining the track selection.

getVideoSize

public VideoSize getVideoSize()

This method is not supported and returns UNKNOWN.

getVolume

public float getVolume()

This method is not supported and returns 1.

increaseDeviceVolume

public void increaseDeviceVolume()

increaseDeviceVolume

public void increaseDeviceVolume(@C.VolumeFlags int flags)

This method is not supported and does nothing.

isCastSessionAvailable

public boolean isCastSessionAvailable()

Returns whether a cast session is available.

isDeviceMuted

public boolean isDeviceMuted()

This method is not supported and always returns false.

isLoading

public boolean isLoading()

Whether the player is currently loading the source.

Returns
boolean

Whether the player is currently loading the source.

isPlayingAd

public 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 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.

prepare

public 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 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 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 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 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 media items to replace the range with.

seekTo

@VisibleForTesting(otherwise = 4)
public 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 MediaItem. 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 INDEX_UNSET.

long positionMs

The seek position in the specified MediaItem in milliseconds, or TIME_UNSET to seek to the media item's default position. If the original seek operation did not directly specify a position, this is the most likely implied position based on the available player state.

@Player.Command int seekCommand

The Player.Command used to trigger the seek.

boolean isRepeatingCurrentItem

Whether this seeks repeats the current item.

setAudioAttributes

public void setAudioAttributes(
    AudioAttributes audioAttributes,
    boolean handleAudioFocus
)

This method is not supported and does nothing.

setDeviceMuted

public void setDeviceMuted(boolean muted)

setDeviceMuted

public void setDeviceMuted(boolean muted, @C.VolumeFlags int flags)

This method is not supported and does nothing.

setDeviceVolume

public void setDeviceVolume(int volume)

setDeviceVolume

public void setDeviceVolume(int volume, @C.VolumeFlags int flags)

This method is not supported and does nothing.

setMediaItems

public 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 media items.

boolean resetPosition

Whether the playback position should be reset to the default position in the first Timeline.Window. If false, playback will start from the position defined by getCurrentMediaItemIndex and getCurrentPosition.

setMediaItems

public 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 media items.

int startIndex

The MediaItem index to start playback from. If INDEX_UNSET is passed, the current position is not reset.

long startPositionMs

The position in milliseconds to start playback from. If TIME_UNSET is passed, the default position of the given MediaItem is used. In any case, if startIndex is set to INDEX_UNSET, this parameter is ignored and the position is not reset at all.

Throws
androidx.media3.common.IllegalSeekPositionException

If the provided startIndex is not within the bounds of the list of media items.

setPlayWhenReady

public 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 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 void setPlaylistMetadata(MediaMetadata mediaMetadata)

This method is not supported and does nothing.

setRepeatMode

public 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.

setSessionAvailabilityListener

public void setSessionAvailabilityListener(
    @Nullable SessionAvailabilityListener listener
)

Sets a listener for updates on the cast session availability.

Parameters
@Nullable SessionAvailabilityListener listener

The SessionAvailabilityListener, or null to clear the listener.

setShuffleModeEnabled

public 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 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 void setVideoSurface(@Nullable Surface surface)

This method is not supported and does nothing.

setVideoSurfaceHolder

public void setVideoSurfaceHolder(@Nullable SurfaceHolder surfaceHolder)

This method is not supported and does nothing.

setVideoSurfaceView

public void setVideoSurfaceView(@Nullable SurfaceView surfaceView)

This method is not supported and does nothing.

setVideoTextureView

public void setVideoTextureView(@Nullable TextureView textureView)

This method is not supported and does nothing.

setVolume

public void setVolume(float volume)

This method is not supported and does nothing.

stop

public 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.