MediaController

Added in 1.0.0
Deprecated in 1.3.0

class MediaController : Closeable

Known direct subclasses
MediaBrowser

This class is deprecated.

androidx.media2 is deprecated.


Allows an app to interact with an active MediaSession or a MediaSessionService which would provide MediaSession. Media buttons and other commands can be sent to the session.

MediaController objects are thread-safe.

Topics covered here:

  1. Controller Lifecycle
  2. Controlling the MediaSession in the same process
  3. Package Visibility Filter

Controller Lifecycle

When a controller is created with the SessionToken for a MediaSession (i.e. session token type is TYPE_SESSION), the controller will connect to the specific session.

When a controller is created with the SessionToken for a MediaSessionService (i.e. session token type is TYPE_SESSION_SERVICE or TYPE_LIBRARY_SERVICE), the controller binds to the service for connecting to a MediaSession in it. MediaSessionService will provide a session to connect.

When a controller connects to a session, onConnect will be called to either accept or reject the connection. Wait onConnected or onDisconnected for the result.

When the connected session is closed, the controller will receive onDisconnected.

When you're done, use close to clean up resources. This also helps session service to be destroyed when there's no controller associated with it.

Controlling the MediaSession in the same process

When you control the MediaSession and its SessionPlayer, it's recommended to use them directly rather than creating MediaController. However, if you need to use in the same process, be careful not to block session callback executor's thread. Here's an example code that would never return due to the thread issue.
// Code runs on the main thread.
MediaSession session = new MediaSession.Builder(context, player)
   .setSessionCallback(sessionCallback, Context.getMainExecutor(context)).build();
MediaController controller = new MediaController.Builder(context)
   .setSessionToken(session.getToken())
   .setControllerCallback(Context.getMainExecutor(context), controllerCallback)
   .build();

// This will hang and never return.
controller.play().get();
When a session gets a command from a controller, the session's onCommandRequest would be executed on the session's callback executor to decide whether to ignore or handle the incoming command. To do so, the session's callback executor shouldn't be blocked to handle the incoming calls. However, if you call get on the thread for the session callback executor, then your call wouldn't be executed and never return.

To avoid such issue, don't block the session callback executor's thread. Creating a dedicated thread for the session callback executor would be helpful. See newSingleThreadExecutor for creating a new thread.

Package Visibility Filter

The app targeting API level 30 or higher must include a <queries> element in their manifest to connect to a service component of another app like MediaSessionService, MediaLibraryService, or androidx.media.MediaBrowserServiceCompat). See the following example and this guide for more information.

<!-- As intent actions -->
<intent>
  <action android:name="androidx.media2.session.MediaSessionService" />
</intent>
<intent>
  <action android:name="androidx.media2.session.MediaLibraryService" />
</intent>
<intent>
  <action android:name="android.media.browse.MediaBrowserService" />
</intent>
<!-- Or, as a package name -->
<package android:name="package_name_of_the_other_app" />

Summary

Nested types

This class is deprecated.

androidx.media2 is deprecated.

This class is deprecated.

androidx.media2 is deprecated.

This class is deprecated.

androidx.media2 is deprecated.

Public functions

ListenableFuture<SessionResult!>
addPlaylistItem(index: @IntRange(from = 0) Int, mediaId: String)

Requests that the SessionPlayer associated with the connected MediaSession adds the media item to the playlist at the index with the media ID.

ListenableFuture<SessionResult!>
adjustVolume(direction: Int, flags: Int)

Requests that the connected MediaSession adjusts the volume of the output that is playing on.

Unit

Releases this object, and disconnects from the session.

ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession deselects the TrackInfo for the current media item.

ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession to fast forward playback.

SessionCommandGroup?

Gets the cached allowed commands from onAllowedCommandsChanged.

Long

Gets the position for how much has been buffered of the SessionPlayer associated with the connected MediaSession, or UNKNOWN_TIME if unknown or not connected.

Int

Gets the current buffering state of the SessionPlayer associated with the connected MediaSession.

SessionToken?

Returns the SessionToken of the connected session.

MediaItem?

Gets the current media item of the SessionPlayer associated with the connected MediaSession.

Int

Gets the current item index in the playlist of the SessionPlayer associated with the connected MediaSession.

Long

Gets the playback position of the SessionPlayer associated with the connected MediaSession.

Long

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

Int

Gets the next item index in the playlist of the SessionPlayer associated with the connected MediaSession.

MediaController.PlaybackInfo?

Get the current playback info for this session.

Float

Gets the playback speed to be used by the of the SessionPlayer associated with the connected MediaSession when playing.

Int

Gets the state of the SessionPlayer associated with the connected MediaSession.

(Mutable)List<MediaItem!>?

Gets the playlist of the SessionPlayer associated with the connected MediaSession.

MediaMetadata?

Gets the playlist metadata of the SessionPlayer associated with the connected MediaSession.

Int

Gets the previous item index in the playlist of the SessionPlayer associated with the connected MediaSession.

Int

Gets the repeat mode of the SessionPlayer associated with the connected MediaSession.

SessionPlayer.TrackInfo?
getSelectedTrack(trackType: Int)

Gets the currently selected track for the given track type of the SessionPlayer associated with the connected MediaSession.

PendingIntent?

Gets an intent for launching UI associated with this session if one exists.

Int

Gets the shuffle mode of the SessionPlayer associated with the connected MediaSession.

(Mutable)List<SessionPlayer.TrackInfo!>

Gets the full list of selected and unselected tracks that the media contains of the SessionPlayer associated with the connected MediaSession.

VideoSize

Gets the video size of the SessionPlayer associated with the connected MediaSession.

Boolean

Returns whether this class is connected to active MediaSession or not.

ListenableFuture<SessionResult!>
movePlaylistItem(
    fromIndex: @IntRange(from = 0) Int,
    toIndex: @IntRange(from = 0) Int
)

Requests that the SessionPlayer associated with the connected MediaSession moves the media item at fromIdx to toIdx in the playlist.

ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession pauses playback.

ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession starts or resumes playback.

ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession prepares the media items for playback.

ListenableFuture<SessionResult!>
removePlaylistItem(index: @IntRange(from = 0) Int)

Requests that the SessionPlayer associated with the connected MediaSession removes the media item at index in the playlist.

ListenableFuture<SessionResult!>
replacePlaylistItem(index: @IntRange(from = 0) Int, mediaId: String)

Requests that the SessionPlayer associated with the connected MediaSession replaces the media item at index in the playlist with the media ID.

ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession to rewind playback.

ListenableFuture<SessionResult!>
seekTo(position: Long)

Requests that the SessionPlayer associated with the connected MediaSession seeks to the specified position.

ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession selects the TrackInfo for the current media item.

ListenableFuture<SessionResult!>

Sends a custom command to the session

ListenableFuture<SessionResult!>
setMediaItem(mediaId: String)

Requests that the SessionPlayer associated with the connected MediaSession sets a MediaItem for playback.

ListenableFuture<SessionResult!>
setMediaUri(uri: Uri, extras: Bundle?)

Requests that the connected MediaSession sets a specific Uri for playback.

ListenableFuture<SessionResult!>
setPlaybackSpeed(playbackSpeed: Float)

Requests that the SessionPlayer associated with the connected MediaSession sets the playback speed.

ListenableFuture<SessionResult!>
setPlaylist(list: (Mutable)List<String!>, metadata: MediaMetadata?)

Requests that the SessionPlayer associated with the connected MediaSession sets the playlist with the list of media IDs.

ListenableFuture<SessionResult!>
setRating(mediaId: String, rating: Rating)

Requests that the connected MediaSession rates the media.

ListenableFuture<SessionResult!>
setRepeatMode(repeatMode: Int)

Requests that the SessionPlayer associated with the connected MediaSession sets the repeat mode.

ListenableFuture<SessionResult!>
setShuffleMode(shuffleMode: Int)

Requests that the SessionPlayer associated with the connected MediaSession sets the shuffle mode.

ListenableFuture<SessionResult!>
setSurface(surface: Surface?)

Requests that the SessionPlayer associated with the connected MediaSession sets the Surface to be used as the sink for the video portion of the media.

ListenableFuture<SessionResult!>
setVolumeTo(value: Int, flags: Int)

Requests that the connected MediaSession sets the volume of the output that is playing on.

ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession skips forward within the current media item.

ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession skips backward within the current media item.

ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession skips to the next item in the playlist.

ListenableFuture<SessionResult!>
skipToPlaylistItem(index: @IntRange(from = 0) Int)

Requests that the SessionPlayer associated with the connected MediaSession skips to the item in the playlist at the index.

ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession skips to the previous item in the playlist.

ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession updates the playlist metadata while keeping the playlist as-is.

Public functions

addPlaylistItem

Added in 1.0.0
Deprecated in 1.3.0
fun addPlaylistItem(index: @IntRange(from = 0) Int, mediaId: String): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession adds the media item to the playlist at the index with the media ID. Index equals to or greater than the current playlist size (e.g. MAX_VALUE) will add the item at the end of the playlist.

If index is less than or equal to the current index of the playlist, the current index of the playlist will be increased correspondingly.

On success, a SessionResult would be returned with item added.

Parameters
index: @IntRange(from = 0) Int

the index you want to add

mediaId: String

the non-empty media id of the new item

adjustVolume

Added in 1.0.0
Deprecated in 1.3.0
fun adjustVolume(direction: Int, flags: Int): ListenableFuture<SessionResult!>

Requests that the connected MediaSession adjusts the volume of the output that is playing on. The direction must be one of ADJUST_LOWER, ADJUST_RAISE, or ADJUST_SAME.

The command will be ignored if the session does not support VOLUME_CONTROL_RELATIVE or VOLUME_CONTROL_ABSOLUTE.

If the session is local playback, this changes the device's volume with the stream that session's player is using. Flags will be specified for the AudioManager.

If the session is remote player (i.e. session has set volume provider), its volume provider will receive this request instead.

Parameters
direction: Int

the direction to adjust the volume in

flags: Int

flags from AudioManager to include with the volume request for local playback

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

See also
getPlaybackInfo

close

Added in 1.0.0
Deprecated in 1.3.0
fun close(): Unit

Releases this object, and disconnects from the session. After this, callbacks wouldn't be received.

deselectTrack

Added in 1.1.0
Deprecated in 1.3.0
fun deselectTrack(trackInfo: SessionPlayer.TrackInfo): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession deselects the TrackInfo for the current media item.

Generally, a track should already be selected in order to be deselected and audio and video tracks should not be deselected.

The types of tracks supported may vary based on players.

Note: getSelectedTrack returns the currently selected track per track type that can be deselected, but the list may be invalidated when onTracksChanged is called.

Parameters
trackInfo: SessionPlayer.TrackInfo

track to be deselected

Returns
ListenableFuture<SessionResult!>

a ListenableFuture which represents the pending completion of the command

fastForward

Added in 1.0.0
Deprecated in 1.3.0
fun fastForward(): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession to fast forward playback.

The implementation may be different depending on the players. For example, it can be implemented by seeking forward once, series of seeking forward, or increasing playback speed. If you need full control, then use seekTo or setPlaybackSpeed directly.

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

See also
onFastForward

getAllowedCommands

Added in 1.1.0
Deprecated in 1.3.0
fun getAllowedCommands(): SessionCommandGroup?

Gets the cached allowed commands from onAllowedCommandsChanged. If it is not connected yet, it returns null.

Returns
SessionCommandGroup?

the allowed commands

getBufferedPosition

Added in 1.0.0
Deprecated in 1.3.0
fun getBufferedPosition(): Long

Gets the position for how much has been buffered of the SessionPlayer associated with the connected MediaSession, or UNKNOWN_TIME if unknown or not connected.

Returns
Long

buffering position in ms, or UNKNOWN_TIME if unknown or not connected

getBufferingState

Added in 1.0.0
Deprecated in 1.3.0
fun getBufferingState(): Int

Gets the current buffering state of the SessionPlayer associated with the connected MediaSession.

The position is the relative position based on the getStartPosition. So the position 0 means the start position of the MediaItem.

Returns
Int

the buffering state, or BUFFERING_STATE_UNKNOWN if unknown or not connected

getConnectedToken

Added in 1.0.0
Deprecated in 1.3.0
fun getConnectedToken(): SessionToken?

Returns the SessionToken of the connected session. If it is not connected yet, it returns null.

This may differ from the SessionToken from the constructor. For example, if the controller is created with the token for MediaSessionService, this would return token for the MediaSession in the service.

Returns
SessionToken?

SessionToken of the connected session, or null if not connected

getCurrentMediaItem

Added in 1.0.0
Deprecated in 1.3.0
fun getCurrentMediaItem(): MediaItem?

Gets the current media item of the SessionPlayer associated with the connected MediaSession. This can be currently playing or would be played with later play. This value may be updated when onCurrentMediaItemChanged or onPlaylistChanged is called.

Returns
MediaItem?

the current media item. Can be null only when media item or playlist hasn't been set or the controller is not connected.

getCurrentMediaItemIndex

Added in 1.0.0
Deprecated in 1.3.0
fun getCurrentMediaItemIndex(): Int

Gets the current item index in the playlist of the SessionPlayer associated with the connected MediaSession. The value would be updated when onCurrentMediaItemChanged or onPlaylistChanged is called.

Returns
Int

the index of current item in playlist, or INVALID_ITEM_INDEX if current media item does not exist or playlist hasn't been set

getCurrentPosition

Added in 1.0.0
Deprecated in 1.3.0
fun getCurrentPosition(): Long

Gets the playback position of the SessionPlayer associated with the connected MediaSession.

The position is the relative position based on the getStartPosition. So the position 0 means the start position of the MediaItem.

Returns
Long

the current playback position in ms, or UNKNOWN_TIME if unknown or not connected

getDuration

Added in 1.0.0
Deprecated in 1.3.0
fun getDuration(): Long

Gets the duration of the current media item, or UNKNOWN_TIME if unknown or not connected. If the current MediaItem has either start or end position, then duration would be adjusted accordingly instead of returning the whole size of the MediaItem.

Returns
Long

the duration in ms, or UNKNOWN_TIME if unknonw or not connected.

getNextMediaItemIndex

Added in 1.0.0
Deprecated in 1.3.0
fun getNextMediaItemIndex(): Int

Gets the next item index in the playlist of the SessionPlayer associated with the connected MediaSession. This value would be updated when onCurrentMediaItemChanged or onPlaylistChanged is called.

Interoperability: When connected to android.support.v4.media.session.MediaSessionCompat, this will always return INVALID_ITEM_INDEX..

Returns
Int

the index of next item in playlist, or INVALID_ITEM_INDEX if next media item does not exist or playlist hasn't been set

getPlaybackInfo

Added in 1.0.0
Deprecated in 1.3.0
fun getPlaybackInfo(): MediaController.PlaybackInfo?

Get the current playback info for this session. If it is not connected yet, it returns null.

Returns
MediaController.PlaybackInfo?

the current playback info or null

getPlaybackSpeed

Added in 1.0.0
Deprecated in 1.3.0
fun getPlaybackSpeed(): Float

Gets the playback speed to be used by the of the SessionPlayer associated with the connected MediaSession when playing. A value of 1.0f is the default playback value, and a negative value indicates reverse playback.

Note that it may differ from the speed set in setPlaybackSpeed.

Returns
Float

speed the playback speed, or 0f if unknown or not connected

getPlayerState

Added in 1.0.0
Deprecated in 1.3.0
fun getPlayerState(): Int

Gets the state of the SessionPlayer associated with the connected MediaSession. If it is not connected yet, it returns PLAYER_STATE_IDLE.

Returns
Int

the player state

getPlaylist

Added in 1.0.0
Deprecated in 1.3.0
fun getPlaylist(): (Mutable)List<MediaItem!>?

Gets the playlist of the SessionPlayer associated with the connected MediaSession. It can be null if the playlist hasn't been set or it's reset by setMediaItem.

This list may differ from the list that was specified with setPlaylist depending on the SessionPlayer implementation.

Returns
(Mutable)List<MediaItem!>?

playlist, or null if the playlist hasn't been set or the controller isn't connected

getPlaylistMetadata

Added in 1.0.0
Deprecated in 1.3.0
fun getPlaylistMetadata(): MediaMetadata?

Gets the playlist metadata of the SessionPlayer associated with the connected MediaSession.

Returns
MediaMetadata?

metadata of the playlist, or null if none is set or the controller is not connected

getPreviousMediaItemIndex

Added in 1.0.0
Deprecated in 1.3.0
fun getPreviousMediaItemIndex(): Int

Gets the previous item index in the playlist of the SessionPlayer associated with the connected MediaSession. This value would be updated when onCurrentMediaItemChanged or onPlaylistChanged is called.

Interoperability: When connected to android.support.v4.media.session.MediaSessionCompat, this will always return INVALID_ITEM_INDEX.

Returns
Int

the index of previous item in playlist, or INVALID_ITEM_INDEX if previous media item does not exist or playlist hasn't been set

getRepeatMode

Added in 1.0.0
Deprecated in 1.3.0
fun getRepeatMode(): Int

Gets the repeat mode of the SessionPlayer associated with the connected MediaSession. If it is not connected yet, it returns REPEAT_MODE_NONE.

Returns
Int

repeat mode

getSelectedTrack

Added in 1.1.0
Deprecated in 1.3.0
fun getSelectedTrack(trackType: Int): SessionPlayer.TrackInfo?

Gets the currently selected track for the given track type of the SessionPlayer associated with the connected MediaSession. If it is not connected yet, it returns null.

The returned value can be outdated after onTracksChanged, onTrackSelected, or onTrackDeselected is called.

Parameters
trackType: Int

type of selected track

Returns
SessionPlayer.TrackInfo?

selected track info

getSessionActivity

Added in 1.0.0
Deprecated in 1.3.0
fun getSessionActivity(): PendingIntent?

Gets an intent for launching UI associated with this session if one exists. If it is not connected yet, it returns null.

Returns
PendingIntent?

a PendingIntent to launch UI or null

getShuffleMode

Added in 1.0.0
Deprecated in 1.3.0
fun getShuffleMode(): Int

Gets the shuffle mode of the SessionPlayer associated with the connected MediaSession. If it is not connected yet, it returns SHUFFLE_MODE_NONE.

Returns
Int

the shuffle mode

getTracks

Added in 1.1.0
Deprecated in 1.3.0
fun getTracks(): (Mutable)List<SessionPlayer.TrackInfo!>

Gets the full list of selected and unselected tracks that the media contains of the SessionPlayer associated with the connected MediaSession. The order of the list is irrelevant as different players expose tracks in different ways, but the tracks will generally be ordered based on track type.

The types of tracks supported may vary based on player implementation.

Returns
(Mutable)List<SessionPlayer.TrackInfo!>

list of tracks. The total number of tracks is the size of the list. If empty, an empty list would be returned.

getVideoSize

Added in 1.1.0
Deprecated in 1.3.0
fun getVideoSize(): VideoSize

Gets the video size of the SessionPlayer associated with the connected MediaSession. If it is not connected yet, it returns new VideoSize(0, 0).

Returns
VideoSize

the size of the video. The width and height of size could be 0 if there is no video or the size has not been determined yet.

isConnected

Added in 1.0.0
Deprecated in 1.3.0
fun isConnected(): Boolean

Returns whether this class is connected to active MediaSession or not.

movePlaylistItem

Added in 1.1.0
Deprecated in 1.3.0
fun movePlaylistItem(
    fromIndex: @IntRange(from = 0) Int,
    toIndex: @IntRange(from = 0) Int
): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession moves the media item at fromIdx to toIdx in the playlist.

On success, a SessionResult would be returned with item set.

Parameters
fromIndex: @IntRange(from = 0) Int

the media item's initial index in the playlist

toIndex: @IntRange(from = 0) Int

the media item's target index in the playlist

pause

Added in 1.0.0
Deprecated in 1.3.0
fun pause(): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession pauses playback.

On success, this transfers the player state to PLAYER_STATE_PAUSED and a SessionResult would be returned with the current media item when the command was completed. If it is called in PLAYER_STATE_IDLE or PLAYER_STATE_ERROR, it whould be ignored and a SessionResult would be returned with RESULT_ERROR_INVALID_STATE.

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

play

Added in 1.0.0
Deprecated in 1.3.0
fun play(): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession starts or resumes playback.

On success, this transfers the player state to PLAYER_STATE_PLAYING and a SessionResult would be returned with the current media item when the command was completed. If the player state is PLAYER_STATE_IDLE, the session would also call prepare and then play to start playback. If you want to have finer grained control of the playback start, call prepare manually before this. Calling prepare in advance would help this method to start playback faster and also help to take audio focus at the last moment.

Interoperability: When connected to android.support.v4.media.session.MediaSessionCompat, then this will be grouped together with previously called setMediaUri. See setMediaUri for details.

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

See also
prepare
setMediaUri

prepare

Added in 1.0.0
Deprecated in 1.3.0
fun prepare(): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession prepares the media items for playback. During this time, the player may allocate resources required to play, such as audio and video decoders. Before calling this API, sets media item(s) through either setMediaItem or setPlaylist.

On success, this transfers the player state from PLAYER_STATE_IDLE to PLAYER_STATE_PAUSED and a SessionResult would be returned with the prepared media item when the command completed. If it's not called in PLAYER_STATE_IDLE, it would be ignored and SessionResult would be returned with RESULT_ERROR_INVALID_STATE.

Playback can be started without this. But this provides finer grained control of playback start. See play for details.

Interoperability: When connected to android.support.v4.media.session.MediaSessionCompat, then this call may be grouped together with previously called setMediaUri. See setMediaUri for details.

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

See also
play
setMediaUri

removePlaylistItem

Added in 1.0.0
Deprecated in 1.3.0
fun removePlaylistItem(index: @IntRange(from = 0) Int): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession removes the media item at index in the playlist.

On success, a SessionResult would be returned with item removed.

Parameters
index: @IntRange(from = 0) Int

the media item you want to add

replacePlaylistItem

Added in 1.0.0
Deprecated in 1.3.0
fun replacePlaylistItem(index: @IntRange(from = 0) Int, mediaId: String): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession replaces the media item at index in the playlist with the media ID.

On success, a SessionResult would be returned with item set.

Parameters
index: @IntRange(from = 0) Int

the index of the item to replace

mediaId: String

the non-empty media id of the new item

rewind

Added in 1.0.0
Deprecated in 1.3.0
fun rewind(): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession to rewind playback.

The implementation may be different depending on the players. For example, it can be implemented by seeking backward once, series of seeking backward, or decreasing playback speed. If you need full control, then use seekTo or setPlaybackSpeed directly.

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

See also
onRewind

seekTo

Added in 1.0.0
Deprecated in 1.3.0
fun seekTo(position: Long): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession seeks to the specified position.

The position is the relative position based on the getStartPosition. So calling seekTo with 0 means the seek to the start position.

On success, a SessionResult would be returned with the current media item when the command completed. If it's called in PLAYER_STATE_IDLE, it is ignored and a SessionResult would be returned with RESULT_ERROR_INVALID_STATE.

Parameters
position: Long

the new playback position in ms. The value should be in the range of start and end positions defined in MediaItem.

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

selectTrack

Added in 1.1.0
Deprecated in 1.3.0
fun selectTrack(trackInfo: SessionPlayer.TrackInfo): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession selects the TrackInfo for the current media item.

Generally one track will be selected for each track type.

The types of tracks supported may vary based on players.

Note: getTracks returns the list of tracks that can be selected, but the list may be invalidated when onTracksChanged is called.

Parameters
trackInfo: SessionPlayer.TrackInfo

track to be selected

Returns
ListenableFuture<SessionResult!>

a ListenableFuture which represents the pending completion of the command

sendCustomCommand

Added in 1.0.0
Deprecated in 1.3.0
fun sendCustomCommand(command: SessionCommand, args: Bundle?): ListenableFuture<SessionResult!>

Sends a custom command to the session

Interoperability: When connected to android.support.v4.media.session.MediaSessionCompat, getResultCode will return the custom result code from the onReceiveResult instead of the standard result codes defined in the SessionResult.

A command is not accepted if it is not a custom command.

Parameters
command: SessionCommand

custom command

args: Bundle?

optional argument

setMediaItem

Added in 1.0.0
Deprecated in 1.3.0
fun setMediaItem(mediaId: String): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession sets a MediaItem for playback. Use this, setMediaUri, or setPlaylist to specify which items to play. If you want to change current item in the playlist, use one of skipToPlaylistItem, skipToNextPlaylistItem, or skipToPreviousPlaylistItem instead of this method.

This can be called multiple times in any states other than PLAYER_STATE_ERROR. This would override previous call of this, setMediaUri, or setPlaylist.

The onPlaylistChanged and/or onCurrentMediaItemChanged would be called when it's completed.

On success, a SessionResult would be returned with item set.

Parameters
mediaId: String

the non-empty media id of the item to play

setMediaUri

Added in 1.1.0
Deprecated in 1.3.0
fun setMediaUri(uri: Uri, extras: Bundle?): ListenableFuture<SessionResult!>

Requests that the connected MediaSession sets a specific Uri for playback. Use this, setMediaItem, or setPlaylist to specify which items to play.

This can be called multiple times in any states other than PLAYER_STATE_ERROR. This would override previous call of this, setMediaItem, or setPlaylist.

The onPlaylistChanged and/or onCurrentMediaItemChanged would be called when it's completed.

On success, a SessionResult would be returned with item set.

Interoperability: When connected to android.support.v4.media.session.MediaSessionCompat, this call will be grouped together with later prepare or play, depending on the Uri pattern as follows:

Uri patterns Following API calls Method
androidx://media2-session/setMediaUri?uri=[uri] prepare prepareFromUri
play playFromUri
androidx://media2-session/setMediaUri?id=[mediaId] prepare prepareFromMediaId
play playFromMediaId
androidx://media2-session/setMediaUri?query=[query] prepare prepareFromSearch
play playFromSearch
Does not match with any pattern above prepare prepareFromUri
play playFromUri

Returned ListenableFuture will return RESULT_SUCCESS when it's handled together with prepare or play. If this API is called multiple times without prepare or play, then RESULT_INFO_SKIPPED will be returned for previous calls.

Parameters
uri: Uri

the Uri of the item to play

setPlaybackSpeed

Added in 1.0.0
Deprecated in 1.3.0
fun setPlaybackSpeed(playbackSpeed: Float): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession sets the playback speed. The default playback speed is 1.0f is the default, and negative values indicate reverse playback and 0.0f is not allowed.

The supported playback speed range depends on the player, so it is recommended to query the actual speed of the player via getPlaybackSpeed after the operation completes. In particular, please note that the player may not support reverse playback.

On success, a SessionResult would be returned with the current media item when the command completed.

Parameters
playbackSpeed: Float

the requested playback speed

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

Throws
java.lang.IllegalArgumentException

if the speed is equal to zero.

setPlaylist

Added in 1.0.0
Deprecated in 1.3.0
fun setPlaylist(list: (Mutable)List<String!>, metadata: MediaMetadata?): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession sets the playlist with the list of media IDs. Use this, setMediaUri, or setMediaItem to specify which items to play.

All media IDs in the list shouldn't be an empty string.

This can be called multiple times in any states other than PLAYER_STATE_ERROR. This would override previous call of this, setMediaItem, or setMediaUri.

The onPlaylistChanged and/or onCurrentMediaItemChanged would be called when it's completed. The current item would be the first item in the playlist.

Parameters
list: (Mutable)List<String!>

list of media id. Shouldn't contain an empty id

metadata: MediaMetadata?

metadata of the playlist

Throws
java.lang.IllegalArgumentException

if the list is null or contains any empty string.

setRating

Added in 1.0.0
Deprecated in 1.3.0
fun setRating(mediaId: String, rating: Rating): ListenableFuture<SessionResult!>

Requests that the connected MediaSession rates the media. This will cause the rating to be set for the current user. The rating style must follow the user rating style from the session.You can get the rating style from the session through the getRating with the key METADATA_KEY_USER_RATING.

If the user rating was null, the media item does not accept setting user rating.

Parameters
mediaId: String

the non-empty media id

rating: Rating

the rating to set

setRepeatMode

Added in 1.0.0
Deprecated in 1.3.0
fun setRepeatMode(repeatMode: Int): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession sets the repeat mode.

On success, a SessionResult would be returned with the current media item when the command completed.

Parameters
repeatMode: Int

repeat mode

Returns
ListenableFuture<SessionResult!>

a ListenableFuture which represents the pending completion of the command

setShuffleMode

Added in 1.0.0
Deprecated in 1.3.0
fun setShuffleMode(shuffleMode: Int): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession sets the shuffle mode.

On success, a SessionResult would be returned with the current media item when the command completed.

Parameters
shuffleMode: Int

the shuffle mode

Returns
ListenableFuture<SessionResult!>

a ListenableFuture which represents the pending completion of the command

setSurface

Added in 1.1.0
Deprecated in 1.3.0
fun setSurface(surface: Surface?): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession sets the Surface to be used as the sink for the video portion of the media.

A null surface will reset any Surface and result in only the audio track being played.

On success, a SessionResult is returned with the current media item when the command completed.

Parameters
surface: Surface?

the Surface to be used for the video portion of the media

Returns
ListenableFuture<SessionResult!>

a ListenableFuture which represents the pending completion of the command

setVolumeTo

Added in 1.0.0
Deprecated in 1.3.0
fun setVolumeTo(value: Int, flags: Int): ListenableFuture<SessionResult!>

Requests that the connected MediaSession sets the volume of the output that is playing on. The command will be ignored if it does not support VOLUME_CONTROL_ABSOLUTE.

If the session is local playback, this changes the device's volume with the stream that session's player is using. Flags will be specified for the AudioManager.

If the session is remote player (i.e. session has set volume provider), its volume provider will receive this request instead.

Parameters
value: Int

the value to set it to, between 0 and the reported max

flags: Int

flags from AudioManager to include with the volume request for local playback

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

See also
getPlaybackInfo

skipBackward

Added in 1.0.0
Deprecated in 1.3.0
fun skipBackward(): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession skips forward within the current media item.

The implementation may be different depending on the players. For example, it can be implemented by seeking backward once with the fixed amount of seconds, or seeking backward to the nearest bookmark. If you need full control, then use seekTo directly.

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

See also
onSkipBackward

skipForward

Added in 1.0.0
Deprecated in 1.3.0
fun skipForward(): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession skips backward within the current media item.

The implementation may be different depending on the players. For example, it can be implemented by seeking forward once with the fixed amount of seconds, or seeking forward to the nearest bookmark. If you need full control, then use seekTo directly.

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

See also
onSkipForward

skipToNextPlaylistItem

Added in 1.0.0
Deprecated in 1.3.0
fun skipToNextPlaylistItem(): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession skips to the next item in the playlist.

On success, a SessionResult would be returned with the current media item when the command completed.

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

skipToPlaylistItem

Added in 1.0.0
Deprecated in 1.3.0
fun skipToPlaylistItem(index: @IntRange(from = 0) Int): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession skips to the item in the playlist at the index.

On success, a SessionResult would be returned with the current media item when the command completed.

Parameters
index: @IntRange(from = 0) Int

The index of the item you want to play in the playlist

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

skipToPreviousPlaylistItem

Added in 1.0.0
Deprecated in 1.3.0
fun skipToPreviousPlaylistItem(): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession skips to the previous item in the playlist.

On success, a SessionResult would be returned with the current media item when the command completed.

Returns
ListenableFuture<SessionResult!>

a ListenableFuture representing the pending completion of the command

updatePlaylistMetadata

Added in 1.0.0
Deprecated in 1.3.0
fun updatePlaylistMetadata(metadata: MediaMetadata?): ListenableFuture<SessionResult!>

Requests that the SessionPlayer associated with the connected MediaSession updates the playlist metadata while keeping the playlist as-is.

On success, a SessionResult would be returned with the current media item when the command completed.

Parameters
metadata: MediaMetadata?

metadata of the playlist