public abstract class SessionPlayer
extends Object implements Closeable

   ↳ androidx.media2.common.SessionPlayer

Base interface for all media players that want media session.

APIs that return ListenableFuture should be the asynchronous calls and shouldn't block the calling thread. This guarantees the APIs are safe to be called on the main thread.

Topics covered here are:

  1. Best practices
  2. Player states
  3. Invalid method calls

Best practices

Here are best practices when implementing/using SessionPlayer:
  • When updating UI, you should respond to SessionPlayer.PlayerCallback invocations instead of SessionPlayer.PlayerResult objects since the player can be controlled by others.
  • When a SessionPlayer object is no longer being used, call close() as soon as possible to release the resources used by the internal player engine associated with the SessionPlayer. For example, if a player uses hardware decoder, other player instances may fallback to software decoders or fail to play. You cannot use SessionPlayer instance after you call close(). There is no way to reuse the instance.
  • The current playback position can be retrieved with a call to getCurrentPosition(), which is helpful for applications such as a music player that need to keep track of the playback progress.
  • The playback position can be adjusted with a call to seekTo(long). Although the asynchronous seekTo(long) call returns right away, the actual seek operation may take a while to finish, especially for audio/video being streamed.
  • You can call seekTo(long) from the PLAYER_STATE_PAUSED. In these cases, if you are playing a video stream and the requested position is valid, one video frame may be displayed.

Player states

The playback control of audio/video files is managed as a state machine. The SessionPlayer defines four states:
  1. PLAYER_STATE_IDLE: Initial state after the instantiation.

    While in this state, you should call setMediaItem(MediaItem) or setPlaylist(List, MediaMetadata). Check returned ListenableFuture for potential error.

    Calling prepare() transfers this object to PLAYER_STATE_PAUSED.

  2. PLAYER_STATE_PAUSED: State when the audio/video playback is paused.

    Call play() to resume or start playback from the position where it paused.

  3. PLAYER_STATE_PLAYING: State when the player plays the media item.

    In this state, SessionPlayer.PlayerCallback.onBufferingStateChanged(SessionPlayer, MediaItem, int) will be called regularly to tell the buffering status.

    Playback state would remain PLAYER_STATE_PLAYING when the currently playing media item is changed.

    When the playback reaches the end of stream, the behavior depends on repeat mode, set by setRepeatMode(int). If the repeat mode was set to REPEAT_MODE_NONE, the player will transfer to the PLAYER_STATE_PAUSED. Otherwise, the SessionPlayer object remains in the PLAYER_STATE_PLAYING and playback will be ongoing.

  4. PLAYER_STATE_ERROR: State when the playback failed and player cannot be recovered by itself.

    In general, playback might fail due to various reasons such as unsupported audio/video format, poorly interleaved audio/video, resolution too high, streaming timeout, and others. In addition, due to programming errors, a playback control operation might be performed from an invalid state. In these cases the player may transition to this state.

Subclasses may have extra methods to reset the player state to PLAYER_STATE_IDLE from other states. Take a look at documentations of specific subclass that you're interested in.

Invalid method calls

The only method you safely call from the PLAYER_STATE_ERROR is close(). Any other methods might return meaningless data.

Subclasses of the SessionPlayer may have extra methods that are safe to be called in the error state and/or provide a method to recover from the error state. Take a look at documentations of specific subclass that you're interested in.

Most methods can be called from any non-Error state. In case they're called in invalid state, the implementation should ignore and would return SessionPlayer.PlayerResult with SessionPlayer.PlayerResult.RESULT_ERROR_INVALID_STATE. The following table lists the methods that aren't guaranteed to successfully running if they're called from the associated invalid states.

Method Name Invalid States
setAudioAttributes {Paused, Playing}
prepare {Paused, Playing}
play {Idle}
pause {Idle}
seekTo {Idle}


Nested classes

class SessionPlayer.PlayerCallback

A callback class to receive notifications for events on the session player. 

class SessionPlayer.PlayerResult

Result class of the asynchronous APIs. 

class SessionPlayer.TrackInfo

Class for the player to return each audio/video/subtitle track's metadata. 



Buffering state indicating the player is buffering but enough has been buffered for this player to be able to play the content.


Buffering state indicating the player is buffering, but the player is currently starved for data, and cannot play.


Buffering state indicating the player is done buffering, and the remainder of the content is available for playback.


Buffering state is unknown.


Media item index is invalid.


State when the player is in error state and cannot be recovered self.


State when the player is idle, and needs configuration to start playback.


State when the player's playback is paused


State when the player's playback is ongoing


Playing media list will be repeated.


Playback of the playing media group will be repeated.


Playback will be stopped at the end of the playing media list.


Playback of the current playing media item will be repeated.


Media list will be played in shuffled order.


Media group will be played in shuffled order.


Media list will be played in order.


Value indicating the time is unknown

Public constructors


Public methods

abstract ListenableFuture<SessionPlayer.PlayerResult> addPlaylistItem(int index, MediaItem item)

Adds the media item to the playlist at the index.

void close()

Removes all existing references to callbacks and executors.

ListenableFuture<SessionPlayer.PlayerResult> deselectTrack(SessionPlayer.TrackInfo trackInfo)

Deselects the SessionPlayer.TrackInfo for the current media item.

abstract AudioAttributesCompat getAudioAttributes()

Gets the AudioAttributesCompat that media player has.

abstract long getBufferedPosition()

Gets the position for how much has been buffered, or UNKNOWN_TIME if unknown.

abstract int getBufferingState()

Returns the current buffering state of the player.

abstract MediaItem getCurrentMediaItem()

Gets the current media item, which is currently playing or would be played with later play().

abstract int getCurrentMediaItemIndex()

Gets the index of current media item in playlist.

abstract long getCurrentPosition()

Gets the current playback position.

abstract long getDuration()

Gets the duration of the current media item, or UNKNOWN_TIME if unknown.

abstract int getNextMediaItemIndex()

Gets the next item index in the playlist.

abstract float getPlaybackSpeed()

Gets the actual playback speed to be used by the player when playing.

abstract int getPlayerState()

Gets the current player state.

abstract List<MediaItem> getPlaylist()

Gets the playlist.

abstract MediaMetadata getPlaylistMetadata()

Gets the playlist metadata.

abstract int getPreviousMediaItemIndex()

Gets the previous item index in the playlist.

abstract int getRepeatMode()

Gets the repeat mode.

SessionPlayer.TrackInfo getSelectedTrack(int trackType)

Gets currently selected track's SessionPlayer.TrackInfo for the given track type.

abstract int getShuffleMode()

Gets the shuffle mode.

List<SessionPlayer.TrackInfo> getTracks()

Gets the full list of selected and unselected tracks that the media contains.

VideoSize getVideoSize()

Gets the size of the video.

ListenableFuture<SessionPlayer.PlayerResult> movePlaylistItem(int fromIndex, int toIndex)

Moves the media item at fromIdx to toIdx in the playlist.

abstract ListenableFuture<SessionPlayer.PlayerResult> pause()

Pauses playback.

abstract ListenableFuture<SessionPlayer.PlayerResult> play()

Starts or resumes playback.

abstract ListenableFuture<SessionPlayer.PlayerResult> prepare()

Prepares the media items for playback.

final void registerPlayerCallback(Executor executor, SessionPlayer.PlayerCallback callback)

Register SessionPlayer.PlayerCallback to listen changes.

abstract ListenableFuture<SessionPlayer.PlayerResult> removePlaylistItem(int index)

Removes the media item from the playlist

The implementation must notify registered callbacks with SessionPlayer.PlayerCallback.onPlaylistChanged(SessionPlayer, List, MediaMetadata) when it's completed.

abstract ListenableFuture<SessionPlayer.PlayerResult> replacePlaylistItem(int index, MediaItem item)

Replaces the media item at index in the playlist.

abstract ListenableFuture<SessionPlayer.PlayerResult> seekTo(long position)

Seeks to the specified position.

ListenableFuture<SessionPlayer.PlayerResult> selectTrack(SessionPlayer.TrackInfo trackInfo)

Selects the SessionPlayer.TrackInfo for the current media item.

abstract ListenableFuture<SessionPlayer.PlayerResult> setAudioAttributes(AudioAttributesCompat attributes)

Sets the AudioAttributesCompat to be used during the playback of the media.

abstract ListenableFuture<SessionPlayer.PlayerResult> setMediaItem(MediaItem item)

Sets a MediaItem for playback.

abstract ListenableFuture<SessionPlayer.PlayerResult> setPlaybackSpeed(float playbackSpeed)

Sets the playback speed.

abstract ListenableFuture<SessionPlayer.PlayerResult> setPlaylist(List<MediaItem> list, MediaMetadata metadata)

Sets a list of MediaItem with metadata.

abstract ListenableFuture<SessionPlayer.PlayerResult> setRepeatMode(int