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

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

Creates a new cast player.

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.

Public functions

Unit

Registers a listener to receive all events from the player.

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

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

Unit

This method is not supported and does nothing.

Unit

This method is not supported and does nothing.

Unit

This method is not supported and does nothing.

Unit

This method is not supported and does nothing.

Unit

This method is not supported and does nothing.

Unit

This function is deprecated.

Use decreaseDeviceVolume instead.

Unit

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.

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.

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.

MediaQueueItem?
getItem(periodId: Int)

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

Whether playback will proceed when getPlaybackState == STATE_READY.

PlaybackParameters!

Returns the currently active playback parameters.

Int

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

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.

Unit

This function is deprecated.

Use increaseDeviceVolume instead.

Unit

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.

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

Moves the media item range to the new index.

Unit

Prepares the player.

Unit

Releases the player.

Unit

Unregister a listener registered through addListener.

Unit
removeMediaItems(fromIndex: Int, toIndex: Int)

Removes a range of media items from the playlist.

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

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

Unit
@VisibleForTesting(otherwise = 4)
seekTo(
    mediaItemIndex: Int,
    positionMs: Long,
    @Player.Command seekCommand: Int,
    isRepeatingCurrentItem: Boolean
)

Seeks to a position in the specified MediaItem.

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

This method is not supported and does nothing.

Unit

This function is deprecated.

Use setDeviceMuted instead.

Unit

This method is not supported and does nothing.

Unit

This function is deprecated.

Use setDeviceVolume instead.

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

This method is not supported and does nothing.

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

Clears the playlist and adds the specified media items.

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

Clears the playlist and adds the specified media items.

Unit
setPlayWhenReady(playWhenReady: Boolean)

Sets whether playback should proceed when getPlaybackState == STATE_READY.

Unit

Attempts to set the playback parameters.

Unit

This method is not supported and does nothing.

Unit

Sets the RepeatMode to be used for playback.

Unit

Sets a listener for updates on the cast session availability.

Unit
setShuffleModeEnabled(shuffleModeEnabled: Boolean)

Sets whether shuffling of media items is enabled.

Unit

Sets the parameters constraining the track selection.

Unit

This method is not supported and does nothing.

Unit

This method is not supported and does nothing.

Unit

This method is not supported and does nothing.

Unit

This method is not supported and does nothing.

Unit
setVolume(volume: Float)

This method is not supported and does nothing.

Unit

Stops playback without resetting the playlist.

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 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 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.BasePlayer
Unit
addMediaItem(mediaItem: MediaItem!)

Adds a media item to the end of the playlist.

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

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

Unit

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

Boolean

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

Unit

Clears the playlist.

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.

Long

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

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.

Any?

Returns the current manifest.

MediaItem?

Returns the currently playing MediaItem.

Int

This function is deprecated.

Use getCurrentMediaItemIndex instead.

MediaItem!

Returns the MediaItem at the given index.

Int

Returns the number of media items in the playlist.

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.

Int

This function is deprecated.

Use getNextMediaItemIndex instead.

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.

Int

This function is deprecated.

Use getPreviousMediaItemIndex instead.

Boolean

This function is deprecated.

Use hasNextMediaItem instead.

Boolean

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

Boolean

This function is deprecated.

Use hasNextMediaItem instead.

Boolean

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

Boolean

Returns whether the provided Command is available.

Boolean

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

Boolean

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

Boolean

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

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

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

Unit
moveMediaItem(currentIndex: Int, newIndex: Int)

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

Unit

This function is deprecated.

Use seekToNextMediaItem instead.

Unit

Pauses playback.

Unit

Resumes playback as soon as getPlaybackState == STATE_READY.

Unit

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

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

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

Unit

Seeks back in the current MediaItem by getSeekBackIncrement milliseconds.

Unit

Seeks forward in the current MediaItem by getSeekForwardIncrement milliseconds.

Unit
seekTo(positionMs: Long)

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

Unit
seekTo(mediaItemIndex: Int, positionMs: Long)

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

Unit

Seeks to the default position associated with the current MediaItem.

Unit
seekToDefaultPosition(mediaItemIndex: Int)

Seeks to the default position associated with the specified MediaItem.

Unit

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

Unit

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

Unit

This function is deprecated.

Use seekToNextMediaItem instead.

Unit

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

Unit

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

Unit

This function is deprecated.

Use seekToPreviousMediaItem instead.

Unit
setMediaItem(mediaItem: MediaItem!)

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

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

Clears the playlist and adds the specified MediaItem.

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

Clears the playlist and adds the specified MediaItem.

Unit

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

Unit

Changes the rate at which playback occurs.

From androidx.media3.common.Player
abstract Unit
setPlaybackSpeed(speed: @FloatRange(from = 0, fromInclusive = false) Float)

Changes the rate at which playback occurs.

Constants

DEVICE_INFO_REMOTE_EMPTY

const val DEVICE_INFO_REMOTE_EMPTYDeviceInfo!

A remoteDeviceInfo with a null routingControllerId.

MAX_SPEED_SUPPORTED

const val MAX_SPEED_SUPPORTED = 2.0f: Float

MIN_SPEED_SUPPORTED

const val MIN_SPEED_SUPPORTED = 0.5f: Float

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

addListener

fun addListener(listener: Player.Listener!): Unit

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
listener: Player.Listener!

The listener to register.

addMediaItems

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

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
index: Int

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.

mediaItems: (Mutable)List<MediaItem!>!

The media items to add.

clearVideoSurface

fun clearVideoSurface(): Unit

This method is not supported and does nothing.

clearVideoSurface

fun clearVideoSurface(surface: Surface?): Unit

This method is not supported and does nothing.

clearVideoSurfaceHolder

fun clearVideoSurfaceHolder(surfaceHolder: SurfaceHolder?): Unit

This method is not supported and does nothing.

clearVideoSurfaceView

fun clearVideoSurfaceView(surfaceView: SurfaceView?): Unit

This method is not supported and does nothing.

clearVideoTextureView

fun clearVideoTextureView(textureView: TextureView?): Unit

This method is not supported and does nothing.

decreaseDeviceVolume

fun decreaseDeviceVolume(): Unit

decreaseDeviceVolume

fun decreaseDeviceVolume(@C.VolumeFlags flags: Int): Unit

This method is not supported and does nothing.

getApplicationLooper

fun getApplicationLooper(): Looper!

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

fun getAudioAttributes(): AudioAttributes!

This method is not supported and returns DEFAULT.

getBufferedPosition

fun getBufferedPosition(): Long

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

fun getContentBufferedPosition(): Long

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

fun getContentPosition(): 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. 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

fun getCurrentAdGroupIndex(): Int

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

fun getCurrentAdIndexInAdGroup(): Int

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

fun getCurrentCues(): CueGroup!

This method is not supported and returns an empty CueGroup.

getCurrentMediaItemIndex

fun getCurrentMediaItemIndex(): Int

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

fun getCurrentPeriodIndex(): Int

Returns the index of the period currently being played.

This method must only be called if COMMAND_GET_TIMELINE is available.

getCurrentPosition

fun getCurrentPosition(): 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.

This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.

getCurrentTimeline

fun getCurrentTimeline(): Timeline!

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

This method must only be called if COMMAND_GET_TIMELINE is available.

getDeviceVolume

fun getDeviceVolume(): Int

This method is not supported and always returns 0.

getDuration

fun getDuration(): Long

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

fun getItem(periodId: Int): MediaQueueItem?

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
periodId: Int

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

Returns
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

fun getMaxSeekToPreviousPosition(): Long

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

Returns
Long

The maximum seek to previous position, in milliseconds.

getMediaMetadataInternal

fun getMediaMetadataInternal(): MediaMetadata!

getPlayWhenReady

fun getPlayWhenReady(): Boolean

Whether playback will proceed when getPlaybackState == STATE_READY.

Returns
Boolean

Whether playback will proceed when ready.

getPlaybackParameters

fun getPlaybackParameters(): PlaybackParameters!

Returns the currently active playback parameters.

getPlaybackSuppressionReason

@Player.PlaybackSuppressionReason
fun getPlaybackSuppressionReason(): Int

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

fun getPlayerError(): PlaybackException?

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
PlaybackException?

The error, or null.

See also
onPlayerError

getPlaylistMetadata

fun getPlaylistMetadata(): MediaMetadata!

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
fun getRepeatMode(): Int

Returns the current RepeatMode used for playback.

Returns
Int

The current repeat mode.

getSeekBackIncrement

fun getSeekBackIncrement(): Long

Returns the seekBack increment.

Returns
Long

The seek back increment, in milliseconds.

getSeekForwardIncrement

fun getSeekForwardIncrement(): Long

Returns the seekForward increment.

Returns
Long

The seek forward increment, in milliseconds.

getShuffleModeEnabled

fun getShuffleModeEnabled(): Boolean

Returns whether shuffling of media items is enabled.

getSurfaceSize

fun getSurfaceSize(): Size!

This method is not supported and returns UNKNOWN.

getTotalBufferedDuration

fun getTotalBufferedDuration(): Long

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

fun getTrackSelectionParameters(): TrackSelectionParameters!

Returns the parameters constraining the track selection.

getVideoSize

fun getVideoSize(): VideoSize!

This method is not supported and returns UNKNOWN.

getVolume

fun getVolume(): Float

This method is not supported and returns 1.

increaseDeviceVolume

fun increaseDeviceVolume(): Unit

increaseDeviceVolume

fun increaseDeviceVolume(@C.VolumeFlags flags: Int): Unit

This method is not supported and does nothing.

isCastSessionAvailable

fun isCastSessionAvailable(): Boolean

Returns whether a cast session is available.

isDeviceMuted

fun isDeviceMuted(): Boolean

This method is not supported and always returns false.

isLoading

fun isLoading(): Boolean

Whether the player is currently loading the source.

Returns
Boolean

Whether the player is currently loading the source.

isPlayingAd

fun isPlayingAd(): Boolean

Returns whether the player is currently playing an ad.

This method must only be called if COMMAND_GET_CURRENT_MEDIA_ITEM is available.

moveMediaItems

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

Moves the media item range to the new index.

This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.

Parameters
fromIndex: Int

The start of the range to move. If the index is larger than the size of the playlist, the request is ignored.

toIndex: Int

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.

newIndex: Int

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

fun prepare(): Unit

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

fun release(): Unit

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

fun removeListener(listener: Player.Listener!): Unit

Unregister a listener registered through addListener. The listener will no longer receive events.

Parameters
listener: Player.Listener!

The listener to unregister.

removeMediaItems

fun removeMediaItems(fromIndex: Int, toIndex: Int): Unit

Removes a range of media items from the playlist.

This method must only be called if COMMAND_CHANGE_MEDIA_ITEMS is available.

Parameters
fromIndex: Int

The index at which to start removing media items. If the index is larger than the size of the playlist, the request is ignored.

toIndex: Int

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

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

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
fromIndex: Int

The start of the range. If the index is larger than the size of the playlist, the request is ignored.

toIndex: Int

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.

mediaItems: (Mutable)List<MediaItem!>!

The media items to replace the range with.

seekTo

@VisibleForTesting(otherwise = 4)
fun seekTo(
    mediaItemIndex: Int,
    positionMs: Long,
    @Player.Command seekCommand: Int,
    isRepeatingCurrentItem: Boolean
): Unit

Seeks to a position in the specified MediaItem.

Parameters
mediaItemIndex: Int

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.

positionMs: Long

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 seekCommand: Int

The Player.Command used to trigger the seek.

isRepeatingCurrentItem: Boolean

Whether this seeks repeats the current item.

setAudioAttributes

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

This method is not supported and does nothing.

setDeviceMuted

fun setDeviceMuted(muted: Boolean): Unit

setDeviceMuted

fun setDeviceMuted(muted: Boolean, @C.VolumeFlags flags: Int): Unit

This method is not supported and does nothing.

setDeviceVolume

fun setDeviceVolume(volume: Int): Unit

setDeviceVolume

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

This method is not supported and does nothing.

setMediaItems

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

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
mediaItems: (Mutable)List<MediaItem!>!

The new media items.

resetPosition: Boolean

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

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

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
mediaItems: (Mutable)List<MediaItem!>!

The new media items.

startIndex: Int

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

startPositionMs: Long

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

fun setPlayWhenReady(playWhenReady: Boolean): Unit

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
playWhenReady: Boolean

Whether playback should proceed when ready.

setPlaybackParameters

fun setPlaybackParameters(playbackParameters: PlaybackParameters!): Unit

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

fun setPlaylistMetadata(mediaMetadata: MediaMetadata!): Unit

This method is not supported and does nothing.

setRepeatMode

fun setRepeatMode(@Player.RepeatMode repeatMode: Int): Unit

Sets the RepeatMode to be used for playback.

This method must only be called if COMMAND_SET_REPEAT_MODE is available.

Parameters
@Player.RepeatMode repeatMode: Int

The repeat mode.

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.

setShuffleModeEnabled

fun setShuffleModeEnabled(shuffleModeEnabled: Boolean): Unit

Sets whether shuffling of media items is enabled.

This method must only be called if COMMAND_SET_SHUFFLE_MODE is available.

Parameters
shuffleModeEnabled: Boolean

Whether shuffling is enabled.

setTrackSelectionParameters

fun setTrackSelectionParameters(parameters: TrackSelectionParameters!): Unit

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

fun setVideoSurface(surface: Surface?): Unit

This method is not supported and does nothing.

setVideoSurfaceHolder

fun setVideoSurfaceHolder(surfaceHolder: SurfaceHolder?): Unit

This method is not supported and does nothing.

setVideoSurfaceView

fun setVideoSurfaceView(surfaceView: SurfaceView?): Unit

This method is not supported and does nothing.

setVideoTextureView

fun setVideoTextureView(textureView: TextureView?): Unit

This method is not supported and does nothing.

setVolume

fun setVolume(volume: Float): Unit

This method is not supported and does nothing.

stop

fun stop(): Unit

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.

Public properties

availableCommands

val availableCommandsPlayer.Commands!

currentTracks

val currentTracksTracks!

deviceInfo

val deviceInfoDeviceInfo!

mediaMetadata

val mediaMetadataMediaMetadata!

playbackState

@Player.State
val playbackStateInt