@UnstableApi
class CastPlayer : 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

Builder for CastPlayer.

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

Public constructors

CastPlayer(castContext: CastContext!)

This function 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 function 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!,
    seekBackIncrementMs: @IntRange(from = 1) Long,
    seekForwardIncrementMs: @IntRange(from = 1) Long
)

This function 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(
    context: Context?,
    castContext: CastContext!,
    mediaItemConverter: MediaItemConverter!,
    seekBackIncrementMs: @IntRange(from = 1) Long,
    seekForwardIncrementMs: @IntRange(from = 1) Long,
    maxSeekToPreviousPositionMs: @IntRange(from = 0) Long
)

This function 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 functions

Boolean

This function is deprecated.

Use getDeviceInfo instead, and check for PLAYBACK_TYPE_REMOTE.

Unit

This function is deprecated.

Use onDeviceInfoChanged instead, and check for PLAYBACK_TYPE_REMOTE.

Inherited Constants

From androidx.media3.common.Player
const Int

This property is deprecated.

Use COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS instead.

const Int

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

const Int

Command to change the media items in the playlist.

const Int

Command to get the player current AudioAttributes.

const Int

Command to get information about the currently playing MediaItem.

const Int

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

const Int

This property is deprecated.

Use COMMAND_GET_METADATA instead.

const Int

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

const Int

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

const Int

Command to get the information about the current timeline.

const Int

Command to get details of the current track selection.

const Int

Command to get the player volume.

const Int

Represents an invalid Command.

const Int

Command to start, pause or resume playback.

const Int

Command to prepare the player.

const Int

Command to release the player.

const Int

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

const Int

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

const Int

Command to seek anywhere inside the current MediaItem.

const Int

This property is deprecated.

Use COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM instead.

const Int

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

const Int

Command to seek anywhere in any MediaItem.

const Int

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

const Int

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

const Int

This property is deprecated.

Use COMMAND_SEEK_TO_NEXT_MEDIA_ITEM instead.

const Int

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

const Int

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

const Int

This property is deprecated.

Use COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM instead.

const Int

This property is deprecated.

Use COMMAND_SEEK_TO_MEDIA_ITEM instead.

const Int

Command to set the player's audio attributes.

const Int

This property is deprecated.

Use COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS instead.

const Int

Command to set the device volume with volume flags.

const Int

Command to set a MediaItem.

const Int

This property is deprecated.

Use COMMAND_SET_PLAYLIST_METADATA instead.

const Int

Command to set the playlist metadata.

const Int

Command to set the repeat mode.

const Int

Command to enable shuffling.

const Int

Command to set the playback speed and pitch.

const Int

Command to set the player's track selection parameters.

const Int

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

const Int

Command to set the player volume.

const Int

Command to stop playback.

const Int

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

const Int

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

const Int

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

const Int

Seek within the current period or to another period.

const Int

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

const Int

Discontinuity introduced by a skipped silence.

const Int

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

const Int

getAudioAttributes changed.

const Int

The audio session id was set.

const Int

isCommandAvailable changed for at least one Command.

const Int

getCurrentCues changed.

const Int

getDeviceInfo changed.

const Int

getDeviceVolume or isDeviceMuted changed.

const Int

isLoading ()} changed.

const Int

isPlaying changed.

const Int

getMaxSeekToPreviousPosition changed.

const Int

getCurrentMediaItem changed or the player started repeating the current item.

const Int

getMediaMetadata changed.

const Int

Metadata associated with the current playback time changed.

const Int

getPlaybackParameters changed.

const Int

getPlaybackState changed.

const Int

getPlaybackSuppressionReason changed.

const Int

getPlayerError changed.

const Int

getPlaylistMetadata changed.

const Int

getPlayWhenReady changed.

const Int

A position discontinuity occurred.

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

const Int

getRepeatMode changed.

const Int

getSeekBackIncrement changed.

const Int

getSeekForwardIncrement changed.

const Int

getShuffleModeEnabled changed.

const Int

Skipping silences in the audio stream is enabled or disabled.

const Int

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

const Int

getCurrentTimeline changed.

const Int

getCurrentTracks changed.

const Int

getTrackSelectionParameters changed.

const Int

getVideoSize changed.

const Int

getVolume changed.

const Int

Playback has automatically transitioned to the next media item.

const Int

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

const Int

The media item has been repeated.

const Int

A seek to another media item has occurred.

const Int

Playback is not suppressed.

const Int

Playback is suppressed because the player is currently scrubbing.

const Int

Playback is suppressed due to transient audio focus loss.

const 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).

const Int

This property is deprecated.

Use PLAYBACK_SUPPRESSION_REASON_UNSUITABLE_AUDIO_OUTPUT instead.

const Int

Playback has been paused to avoid becoming noisy.

const Int

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

const Int

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

const Int

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

const Int

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

const Int

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

const Int

Repeats the entire timeline infinitely.

const Int

Normal playback without repetition.

const Int

Repeats the currently playing MediaItem infinitely during ongoing playback.

const Int

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

const Int

The player has finished playing the media.

const Int

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

const Int

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

const Int

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

const Int

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

Inherited functions

From androidx.media3.common.ForwardingPlayer
Unit

Calls addListener on the delegate.

Unit
addMediaItem(mediaItem: MediaItem!)

Calls addMediaItem on the delegate.

Unit
addMediaItem(index: Int, mediaItem: MediaItem!)

Calls addMediaItem on the delegate.

Unit

Calls addMediaItems on the delegate.

Unit
addMediaItems(index: Int, mediaItems: (Mutable)List<MediaItem!>!)

Calls addMediaItems on the delegate.

Boolean

Calls canAdvertiseSession on the delegate and returns the result.

Unit

Calls clearMediaItems on the delegate.

Unit

Calls clearVideoSurface on the delegate.

Unit

Calls clearVideoSurface on the delegate.

Unit

Calls clearVideoSurfaceHolder on the delegate.

Unit

Calls clearVideoSurfaceView on the delegate.

Unit

Calls clearVideoTextureView on the delegate.

Unit

This function is deprecated.

Use decreaseDeviceVolume instead.

Unit

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.

Any?

Calls getCurrentManifest on the delegate and returns the result.

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 function 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!

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

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

Unit

This function is deprecated.

Use increaseDeviceVolume instead.

Unit

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 function is deprecated.

Use isCurrentMediaItemDynamic instead.

Boolean

This function is deprecated.

Use isCurrentMediaItemLive instead.

Boolean

This function 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.

Unit
moveMediaItem(currentIndex: Int, newIndex: Int)

Calls moveMediaItem on the delegate.

Unit
moveMediaItems(fromIndex: Int, toIndex: Int, newIndex: Int)

Calls moveMediaItems on the delegate.

Unit

Calls mute on the delegate.

Unit

Calls pause on the delegate.

Unit

Calls play on the delegate.

Unit

Calls prepare on the delegate.

Unit

Calls release on the delegate.

Unit

Calls removeListener on the delegate.

Unit

Calls removeMediaItem on the delegate.

Unit
removeMediaItems(fromIndex: Int, toIndex: Int)

Calls removeMediaItems on the delegate.

Unit
replaceMediaItem(index: Int, mediaItem: MediaItem!)

Calls replaceMediaItem on the delegate.

Unit
replaceMediaItems(
    fromIndex: Int,
    toIndex: Int,
    mediaItems: (Mutable)List<MediaItem!>!
)

Calls replaceMediaItems on the delegate.

Unit

Calls seekBack on the delegate.

Unit

Calls seekForward on the delegate.

Unit
seekTo(positionMs: Long)

Calls seekTo on the delegate.

Unit
seekTo(mediaItemIndex: Int, positionMs: Long)

Calls seekTo on the delegate.

Unit

Calls seekToDefaultPosition on the delegate.

Unit
seekToDefaultPosition(mediaItemIndex: Int)

Calls seekToDefaultPosition on the delegate.

Unit

Calls seekToNext on the delegate.

Unit

Calls seekToNextMediaItem on the delegate.

Unit

Calls seekToPrevious on the delegate.

Unit

Calls seekToPreviousMediaItem on the delegate.

Unit
setAudioAttributes(
    audioAttributes: AudioAttributes!,
    handleAudioFocus: Boolean
)

Calls setAudioAttributes on the delegate.

Unit

This function is deprecated.

Use setDeviceMuted instead.

Unit

Calls setDeviceMuted on the delegate.

Unit

This function is deprecated.

Use setDeviceVolume instead.

Unit
setDeviceVolume(volume: Int, @C.VolumeFlags flags: Int)

Calls setDeviceVolume on the delegate.

Unit
setMediaItem(mediaItem: MediaItem!)

Calls setMediaItem on the delegate.

Unit
setMediaItem(mediaItem: MediaItem!, resetPosition: Boolean)

Calls setMediaItem on the delegate.

Unit
setMediaItem(mediaItem: MediaItem!, startPositionMs: Long)

Calls setMediaItem on the delegate.

Unit

Calls setMediaItems on the delegate.

Unit
setMediaItems(
    mediaItems: (Mutable)List<MediaItem!>!,
    resetPosition: Boolean
)

Calls setMediaItems ()} on the delegate.

Unit
setMediaItems(
    mediaItems: (Mutable)List<MediaItem!>!,
    startIndex: Int,
    startPositionMs: Long
)

Calls setMediaItems on the delegate.

Unit
setPlayWhenReady(playWhenReady: Boolean)

Calls setPlayWhenReady on the delegate.

Unit

Calls setPlaybackParameters on the delegate.

Unit

Calls setPlaybackSpeed on the delegate.

Unit

Calls setPlaylistMetadata on the delegate.

Unit

Calls setRepeatMode on the delegate.

Unit
setShuffleModeEnabled(shuffleModeEnabled: Boolean)

Calls setShuffleModeEnabled on the delegate.

Unit

Calls setTrackSelectionParameters on the delegate.

Unit

Calls setVideoSurface on the delegate.

Unit

Calls setVideoSurfaceHolder on the delegate.

Unit

Calls setVideoSurfaceView on the delegate.

Unit

Calls setVideoTextureView on the delegate.

Unit
setVolume(volume: Float)

Calls setVolume on the delegate.

Unit

Calls stop on the delegate.

Unit

Calls unmute on the delegate.

From androidx.media3.common.Player
abstract Unit
setDeviceVolume(volume: @IntRange(from = 0) Int)

This function is deprecated.

Use setDeviceVolume instead.

abstract Unit
setDeviceVolume(volume: @IntRange(from = 0) Int, @C.VolumeFlags flags: Int)

Sets the volume of the device with volume flags.

abstract Unit
setPlaybackSpeed(speed: @FloatRange(from = 0, fromInclusive = false) Float)

Changes the rate at which playback occurs.

abstract Unit
setVolume(volume: @FloatRange(from = 0, to = 1.0) Float)

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

Constants

DEVICE_INFO_REMOTE_EMPTY

const val DEVICE_INFO_REMOTE_EMPTYDeviceInfo!

Same as DEVICE_INFO_REMOTE_EMPTY.

Public constructors

CastPlayer

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

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

CastPlayer(
    castContext: CastContext!,
    mediaItemConverter: MediaItemConverter!,
    seekBackIncrementMs: @IntRange(from = 1) Long,
    seekForwardIncrementMs: @IntRange(from = 1) Long
)

Creates a new cast player.

Parameters
castContext: CastContext!

The context from which the cast session is obtained.

mediaItemConverter: MediaItemConverter!

The MediaItemConverter to use.

seekBackIncrementMs: @IntRange(from = 1) Long

The seekBack increment, in milliseconds.

seekForwardIncrementMs: @IntRange(from = 1) Long

The seekForward increment, in milliseconds.

Throws
java.lang.IllegalArgumentException

If seekBackIncrementMs or seekForwardIncrementMs is non-positive.

CastPlayer

CastPlayer(
    context: Context?,
    castContext: CastContext!,
    mediaItemConverter: MediaItemConverter!,
    seekBackIncrementMs: @IntRange(from = 1) Long,
    seekForwardIncrementMs: @IntRange(from = 1) Long,
    maxSeekToPreviousPositionMs: @IntRange(from = 0) Long
)

Creates a new cast player.

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

seekBackIncrementMs: @IntRange(from = 1) Long

The seekBack increment, in milliseconds.

seekForwardIncrementMs: @IntRange(from = 1) Long

The seekForward increment, in milliseconds.

maxSeekToPreviousPositionMs: @IntRange(from = 0) Long

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 functions

isCastSessionAvailable

fun isCastSessionAvailable(): Boolean

Returns whether a cast session is available.

setSessionAvailabilityListener

fun setSessionAvailabilityListener(listener: SessionAvailabilityListener?): Unit

Sets a listener for updates on the cast session availability.

Parameters
listener: SessionAvailabilityListener?

The SessionAvailabilityListener, or null to clear the listener.