@UnstableApi
public final class CastPlayer extends ForwardingPlayer


Player implementation that can control playback both on the local device, and on a remote Cast device.

See RemoteCastPlayer for a Player that only supports playback on Cast receivers.

This class works by delegating playback to a dedicated player depending on Cast session availability. When a Cast session becomes available or unavailable, the following steps take place:

Summary

Nested types

public final class CastPlayer.Builder

Builder for CastPlayer.

Callback for moving state across players when transferring playback upon Cast session availability changes.

Public constructors

CastPlayer(CastContext castContext)

This method is deprecated.

Use RemoteCastPlayer.Builder to create a Player for playback exclusively on Cast receivers, or Builder for a Player that works both on Cast receivers and locally.

CastPlayer(
    CastContext castContext,
    MediaItemConverter mediaItemConverter
)

This method is deprecated.

Use RemoteCastPlayer.Builder to create a Player for playback exclusively on Cast receivers, or Builder for a Player that works both on Cast receivers and locally.

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

This method is deprecated.

Use RemoteCastPlayer.Builder to create a Player for playback exclusively on Cast receivers, or Builder for a Player that works both on Cast receivers and locally.

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

This method is deprecated.

Use RemoteCastPlayer.Builder to create a Player for playback exclusively on Cast receivers, or Builder for a Player that works both on Cast receivers and locally.

Public methods

boolean

This method is deprecated.

Use getDeviceInfo instead, and check for PLAYBACK_TYPE_REMOTE.

void

This method is deprecated.

Use onDeviceInfoChanged instead, and check for PLAYBACK_TYPE_REMOTE.

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 or isDeviceMuted 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 because the player is currently scrubbing.

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.ForwardingPlayer
void

Calls addListener on the delegate.

void

Calls addMediaItem on the delegate.

void
addMediaItem(int index, MediaItem mediaItem)

Calls addMediaItem on the delegate.

void

Calls addMediaItems on the delegate.

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

Calls addMediaItems on the delegate.

boolean

Calls canAdvertiseSession on the delegate and returns the result.

void

Calls clearMediaItems on the delegate.

void

Calls clearVideoSurface on the delegate.

void

Calls clearVideoSurface on the delegate.

void

Calls clearVideoSurfaceHolder on the delegate.

void

Calls clearVideoSurfaceView on the delegate.

void

Calls clearVideoTextureView on the delegate.

void

This method is deprecated.

Use decreaseDeviceVolume instead.

void

Calls decreaseDeviceVolume on the delegate.

Looper

Calls getApplicationLooper on the delegate and returns the result.

AudioAttributes

Calls getAudioAttributes on the delegate and returns the result.

int

Calls getAudioSessionId on the delegate and returns the result.

Player.Commands

Calls getAvailableCommands on the delegate and returns the result.

int

Calls getBufferedPercentage on the delegate and returns the result.

long

Calls getBufferedPosition on the delegate and returns the result.

long

Calls getContentBufferedPosition on the delegate and returns the result.

long

Calls getContentDuration on the delegate and returns the result.

long

Calls getContentPosition on the delegate and returns the result.

int

Calls getCurrentAdGroupIndex on the delegate and returns the result.

int

Calls getCurrentAdIndexInAdGroup on the delegate and returns the result.

CueGroup

Calls getCurrentCues on the delegate and returns the result.

long

Calls getCurrentLiveOffset on the delegate and returns the result.

@Nullable Object

Calls getCurrentManifest on the delegate and returns the result.

@Nullable MediaItem

Calls getCurrentMediaItem on the delegate and returns the result.

int

Calls getCurrentMediaItemIndex on the delegate and returns the result.

int

Calls getCurrentPeriodIndex on the delegate and returns the result.

long

Calls getCurrentPosition on the delegate and returns the result.

Timeline

Calls getCurrentTimeline on the delegate and returns the result.

Tracks

Calls getCurrentTracks on the delegate and returns the result.

int

This method is deprecated.

Use getCurrentMediaItemIndex instead.

DeviceInfo

Calls getDeviceInfo on the delegate and returns the result.

int

Calls getDeviceVolume on the delegate and returns the result.

long

Calls getDuration on the delegate and returns the result.

long

Calls getMaxSeekToPreviousPosition on the delegate and returns the result.

MediaItem
getMediaItemAt(int index)

Calls getMediaItemAt on the delegate and returns the result.

int

Calls getMediaItemCount on the delegate and returns the result.

MediaMetadata

Calls getMediaMetadata on the delegate and returns the result.

int

Calls getNextMediaItemIndex on the delegate and returns the result.

int

This method is deprecated.

Use getNextMediaItemIndex instead.

boolean

Calls getPlayWhenReady on the delegate and returns the result.

PlaybackParameters

Calls getPlaybackParameters on the delegate and returns the result.

int

Calls getPlaybackState on the delegate and returns the result.

int

Calls getPlaybackSuppressionReason on the delegate and returns the result.

@Nullable PlaybackException

Calls getPlayerError on the delegate and returns the result.

MediaMetadata

Calls getPlaylistMetadata on the delegate and returns the result.

int

Calls getPreviousMediaItemIndex on the delegate and returns the result.

int

This method is deprecated.

Use getPreviousMediaItemIndex instead.

int

Calls getRepeatMode on the delegate and returns the result.

long

Calls getSeekBackIncrement on the delegate and returns the result.

long

Calls getSeekForwardIncrement on the delegate and returns the result.

boolean

Calls getShuffleModeEnabled on the delegate and returns the result.

Size

Calls getSurfaceSize on the delegate and returns the result.

long

Calls getTotalBufferedDuration on the delegate and returns the result.

TrackSelectionParameters

Calls getTrackSelectionParameters on the delegate and returns the result.

VideoSize

Calls getVideoSize on the delegate and returns the result.

float

Calls getVolume on the delegate and returns the result.

Player

Returns the Player to which operations are forwarded.

boolean

Calls hasNextMediaItem on the delegate and returns the result.

boolean

Calls hasPreviousMediaItem on the delegate and returns the result.

void

This method is deprecated.

Use increaseDeviceVolume instead.

void

Calls increaseDeviceVolume on the delegate.

boolean

Calls isCommandAvailable on the delegate and returns the result.

boolean

Calls isCurrentMediaItemDynamic on the delegate and returns the result.

boolean

Calls isCurrentMediaItemLive on the delegate and returns the result.

boolean

Calls isCurrentMediaItemSeekable on the delegate and returns the result.

boolean

This method is deprecated.

Use isCurrentMediaItemDynamic instead.

boolean

This method is deprecated.

Use isCurrentMediaItemLive instead.

boolean

This method is deprecated.

Use isCurrentMediaItemSeekable instead.

boolean

Calls isDeviceMuted on the delegate and returns the result.

boolean

Calls isLoading on the delegate and returns the result.

boolean

Calls isPlaying on the delegate and returns the result.

boolean

Calls isPlayingAd on the delegate and returns the result.

void
moveMediaItem(int currentIndex, int newIndex)

Calls moveMediaItem on the delegate.

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

Calls moveMediaItems on the delegate.

void

Calls mute on the delegate.

void

Calls pause on the delegate.

void

Calls play on the delegate.

void

Calls prepare on the delegate.

void

Calls release on the delegate.

void

Calls removeListener on the delegate.

void
removeMediaItem(int index)

Calls removeMediaItem on the delegate.

void
removeMediaItems(int fromIndex, int toIndex)

Calls removeMediaItems on the delegate.

void
replaceMediaItem(int index, MediaItem mediaItem)

Calls replaceMediaItem on the delegate.

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

Calls replaceMediaItems on the delegate.

void

Calls seekBack on the delegate.

void

Calls seekForward on the delegate.

void
seekTo(long positionMs)

Calls seekTo on the delegate.

void
seekTo(int mediaItemIndex, long positionMs)

Calls seekTo on the delegate.

void

Calls seekToDefaultPosition on the delegate.

void
seekToDefaultPosition(int mediaItemIndex)

Calls seekToDefaultPosition on the delegate.

void

Calls seekToNext on the delegate.

void

Calls seekToNextMediaItem on the delegate.

void

Calls seekToPrevious on the delegate.

void

Calls seekToPreviousMediaItem on the delegate.

void
setAudioAttributes(
    AudioAttributes audioAttributes,
    boolean handleAudioFocus
)

Calls setAudioAttributes on the delegate.

void
setDeviceMuted(boolean muted)

This method is deprecated.

Use setDeviceMuted instead.

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

Calls setDeviceMuted on the delegate.

void
setDeviceVolume(int volume)

This method is deprecated.

Use setDeviceVolume instead.

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

Calls setDeviceVolume on the delegate.

void

Calls setMediaItem on the delegate.

void
setMediaItem(MediaItem mediaItem, boolean resetPosition)

Calls setMediaItem on the delegate.

void
setMediaItem(MediaItem mediaItem, long startPositionMs)

Calls setMediaItem on the delegate.

void

Calls setMediaItems on the delegate.

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

Calls setMediaItems ()} on the delegate.

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

Calls setMediaItems on the delegate.

void
setPlayWhenReady(boolean playWhenReady)

Calls setPlayWhenReady on the delegate.

void

Calls setPlaybackParameters on the delegate.

void
setPlaybackSpeed(float speed)

Calls setPlaybackSpeed on the delegate.

void

Calls setPlaylistMetadata on the delegate.

void

Calls setRepeatMode on the delegate.

void
setShuffleModeEnabled(boolean shuffleModeEnabled)

Calls setShuffleModeEnabled on the delegate.

void

Calls setTrackSelectionParameters on the delegate.

void

Calls setVideoSurface on the delegate.

void

Calls setVideoSurfaceHolder on the delegate.

void

Calls setVideoSurfaceView on the delegate.

void

Calls setVideoTextureView on the delegate.

void
setVolume(float volume)

Calls setVolume on the delegate.

void

Calls stop on the delegate.

void

Calls unmute on the delegate.

From androidx.media3.common.Player
abstract void
setDeviceVolume(@IntRange(from = 0) int volume)

This method is deprecated.

Use setDeviceVolume instead.

abstract void
setDeviceVolume(@IntRange(from = 0) int volume, @C.VolumeFlags int flags)

Sets the volume of the device with volume flags.

abstract void
setPlaybackSpeed(@FloatRange(from = 0, fromInclusive = false) float speed)

Changes the rate at which playback occurs.

abstract void
setVolume(@FloatRange(from = 0, to = 1.0) float volume)

Sets the audio volume, valid values are between 0 (silence) and 1 (unity gain, signal unchanged), inclusive.

Constants

DEVICE_INFO_REMOTE_EMPTY

public static final DeviceInfo DEVICE_INFO_REMOTE_EMPTY

Same as DEVICE_INFO_REMOTE_EMPTY.

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

isCastSessionAvailable

public boolean isCastSessionAvailable()

Returns whether a cast session is available.

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.