Stay organized with collections Save and categorize content based on your preferences.

SimpleBasePlayer

@UnstableApi
abstract class SimpleBasePlayer : BasePlayer

Known direct subclasses
LegacyMediaPlayerWrapper

A Player wrapper for the legacy Android platform MediaPlayer.


A base implementation for Player that reduces the number of methods to implement to a minimum.

Implementation notes:

  • Subclasses must override getState to populate the current player state on request.
  • The State should set the available commands to indicate which Player methods are supported.
  • All setter-like player methods (for example, setPlayWhenReady) forward to overridable methods (for example, handleSetPlayWhenReady) that can be used to handle these requests. These methods return a ListenableFuture to indicate when the request has been handled and is fully reflected in the values returned from getState. This class will automatically request a state update once the request is done. If the state changes can be handled synchronously, these methods can return Guava's immediateVoidFuture.
  • Subclasses can manually trigger state updates with invalidateState, for example if something changes independent of Player method calls.
This base class handles various aspects of the player implementation to simplify the subclass:
  • The State can only be created with allowed combinations of state values, avoiding any invalid player states.
  • Only functionality that is declared as available needs to be implemented. Other methods are automatically ignored.
  • Listener handling and informing listeners of state changes is handled automatically.
  • The base class provides a framework for asynchronous handling of method calls. It changes the visible playback state immediately to the most likely outcome to ensure the user-visible state changes look like synchronous operations. The state is then updated again once the asynchronous method calls have been fully handled.

Summary

Nested types

protected class SimpleBasePlayer.State

An immutable state description of the player.

class SimpleBasePlayer.State.Builder

A builder for State objects.

Protected constructors

SimpleBasePlayer(applicationLooper: Looper!)

Creates the base class.

SimpleBasePlayer(applicationLooper: Looper!, clock: Clock!)

Creates the base class.

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

Clears any Surface, SurfaceHolder, SurfaceView or TextureView currently set on the player.

Unit

Clears the Surface onto which video is being rendered if it matches the one passed.

Unit

Clears the SurfaceHolder that holds the Surface onto which video is being rendered if it matches the one passed.

Unit

Clears the SurfaceView onto which video is being rendered if it matches the one passed.

Unit

Clears the TextureView onto which video is being rendered if it matches the one passed.

Unit

Decreases the volume of the device.

AudioAttributes!

Returns the attributes for audio playback.

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!

Returns the current 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!

Gets the device information.

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.

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.

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.

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!

Gets the size of the surface on which the video is rendered.

Long

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

TrackSelectionParameters!

Returns the parameters constraining the track selection.

VideoSize!

Gets the size of the video.

Float

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

Unit

Increases the volume of the device.

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.

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
seekTo(mediaItemIndex: Int, positionMs: Long)

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

Unit

Sets the mute state of the device.

Unit

Sets the volume of the device.

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

Clears the playlist and adds the specified MediaItems.

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

Clears the playlist and adds the specified MediaItems.

Unit
setPlayWhenReady(playWhenReady: Boolean)

Sets whether playback should proceed when getPlaybackState == STATE_READY.

Unit

Attempts to set the playback parameters.

Unit

Sets the playlist MediaMetadata.

Unit
setRepeatMode(repeatMode: Int)

Sets the RepeatMode to be used for playback.

Unit
setShuffleModeEnabled(shuffleModeEnabled: Boolean)

Sets whether shuffling of media items is enabled.

Unit

Sets the parameters constraining the track selection.

Unit

Sets the Surface onto which video will be rendered.

Unit

Sets the SurfaceHolder that holds the Surface onto which video will be rendered.

Unit

Sets the SurfaceView onto which video will be rendered.

Unit

Sets the TextureView onto which video will be rendered.

Unit
setVolume(volume: Float)

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

Unit

Stops playback without resetting the playlist.

Unit
stop(reset: Boolean)

This function is deprecated.

Use stop and clearMediaItems (if reset is true) or just stop (if reset is false).

Protected functions

SimpleBasePlayer.State!

Returns the placeholder state used while a player method is handled asynchronously.

ListenableFuture<Any!>!

Handles calls to set playWhenReady.

Unit

Invalidates the current state.

Public properties

Looper!

Inherited Constants

From androidx.media3.common.Player
const Int

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

const Int

Command to change the MediaItems in the playlist.

const Int

Command to get the player current AudioAttributes.

const Int

Command to get the currently playing MediaItem.

const Int

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

const Int

Command to get the MediaItems metadata.

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 seek back by a fixed increment into the current MediaItem.

const Int

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

const Int

Command to seek anywhere into 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 or 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 or 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 device volume and mute it.

const Int

Command to set a MediaItem.

const Int

Command to set the MediaItems 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 or release the player.

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 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 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 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 MediaItemisCurrentMediaItemLive isn'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.