@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
@InlineMe(replacement = "this.decreaseDeviceVolume(0)")
decreaseDeviceVolume()

This method is deprecated.

Use decreaseDeviceVolume (int)} instead.

void

Decreases the volume of the device.

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

Gets the current volume of the device.

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

Returns the audio volume, with 0 being silence and 1 being unity gain (signal unchanged).

void
@InlineMe(replacement = "this.increaseDeviceVolume(0)")
increaseDeviceVolume()

This method is deprecated.

Use increaseDeviceVolume instead.

void

Increases the volume of the device.

boolean

Returns whether a cast session is available.

boolean

Gets whether the device is muted or not.

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
setAudioAttributes(
    AudioAttributes audioAttributes,
    boolean handleAudioFocus
)

This method is not supported and does nothing.

void
@InlineMe(replacement = "this.setDeviceMuted(muted, 0)")
setDeviceMuted(boolean muted)

This method is deprecated.

Use setDeviceMuted instead.

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

Sets the mute state of the device.

void
@InlineMe(replacement = "this.setDeviceVolume(volume, 0)")
setDeviceVolume(@IntRange(from = 0) int volume)

This method is deprecated.

Use setDeviceVolume instead.

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

Sets the volume of the device with volume flags.

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

Sets the playlist MediaMetadata.

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)

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

void

Stops playback without resetting the playlist.

Protected methods

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

Seeks to a position in the specified MediaItem.

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