Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

SessionCallback

abstract class SessionCallback
kotlin.Any
   ↳ androidx.media2.MediaSession2.SessionCallback

Callback to be called for all incoming commands from MediaController2s.

If it's not set, the session will accept all controllers and all incoming commands by default.

Summary

Public constructors

Callback to be called for all incoming commands from MediaController2s.

Public methods
open Unit
onRewind(@NonNull session: MediaSession2, controller: MediaSession2.ControllerInfo!)

Called when a controller called MediaController2#rewind()

open Unit
onMediaPrepared(@NonNull session: MediaSession2, @NonNull player: MediaPlayerConnector, @NonNull item: MediaItem2)

Called when the player is prepared, i.

open Unit
onSubscribeRoutesInfo(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo)

Called when a controller called MediaController2#subscribeRoutesInfo() Session app should notify the routes information by calling MediaSession2#notifyRoutesInfoChanged(ControllerInfo, List).

open Unit
onUnsubscribeRoutesInfo(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo)

Called when a controller called MediaController2#unsubscribeRoutesInfo()

open Unit
onFastForward(@NonNull session: MediaSession2, controller: MediaSession2.ControllerInfo!)

Called when a controller called MediaController2#fastForward()

open Unit
onPlayerStateChanged(@NonNull session: MediaSession2, @NonNull player: MediaPlayerConnector, state: Int)

Called to indicate that the state of the player has changed.

open Unit
onPlaylistMetadataChanged(@NonNull session: MediaSession2, @NonNull playlistAgent: MediaPlaylistAgent, @Nullable metadata: MediaMetadata2?)

Called when a playlist metadata is changed.

open Unit
onBufferingStateChanged(@NonNull session: MediaSession2, @NonNull player: MediaPlayerConnector, @NonNull item: MediaItem2, state: Int)

Called to report buffering events for a data source.

open Unit
onShuffleModeChanged(@NonNull session: MediaSession2, @NonNull playlistAgent: MediaPlaylistAgent, shuffleMode: Int)

Called when the shuffle mode is changed.

open Unit
onDisconnected(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo)

Called when a controller is disconnected

open Unit
onPrepareFromMediaId(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull mediaId: String, @Nullable extras: Bundle?)

Called when a controller requested to prepare for playing a specific mediaId through MediaController2#prepareFromMediaId(String, Bundle).

open Unit
onSelectRoute(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull route: Bundle)

Called when a controller called MediaController2#selectRoute(Bundle).

open SessionCommandGroup2?
onConnect(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo)

Called when a controller is created for this session.

open Unit
onRepeatModeChanged(@NonNull session: MediaSession2, @NonNull playlistAgent: MediaPlaylistAgent, repeatMode: Int)

Called when the repeat mode is changed.

open Boolean
onCommandRequest(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull command: SessionCommand2)

Called when a controller sent a command which will be sent directly to one of the following:

Return false here to reject the request and stop sending command.

open Unit
onCustomCommand(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull customCommand: SessionCommand2, @Nullable args: Bundle?, @Nullable cb: ResultReceiver?)

Called when a controller sent a custom command through MediaController2#sendCustomCommand(SessionCommand2, Bundle, ResultReceiver).

open Unit
onPlaylistChanged(@NonNull session: MediaSession2, @NonNull playlistAgent: MediaPlaylistAgent, @NonNull list: MutableList<MediaItem2!>, @Nullable metadata: MediaMetadata2?)

Called when a playlist is changed from the MediaPlaylistAgent.

open Unit
onSeekCompleted(@NonNull session: MediaSession2, @NonNull player: MediaPlayerConnector, position: Long)

Called to indicate that seekTo(long) is completed.

open Unit
onPrepareFromSearch(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull query: String, @Nullable extras: Bundle?)

Called when a controller requested to prepare playback from a search query through MediaController2#prepareFromSearch(String, Bundle).

open Unit
onPlayFromMediaId(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull mediaId: String, @Nullable extras: Bundle?)

Called when a controller requested to play a specific mediaId through MediaController2#playFromMediaId(String, Bundle).

open Unit
onCurrentMediaItemChanged(@NonNull session: MediaSession2, @NonNull player: MediaPlayerConnector, @Nullable item: MediaItem2?)

Called when the player's current playing item is changed

open Unit
onSetRating(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull mediaId: String, @NonNull rating: Rating2)

Called when a controller set rating of a media item through MediaController2#setRating(String, Rating2).

open Unit
onPlayFromSearch(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull query: String, @Nullable extras: Bundle?)

Called when a controller requested to begin playback from a search query through MediaController2#playFromSearch(String, Bundle)

open Unit
onPlayFromUri(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull uri: Uri, @Nullable extras: Bundle?)

Called when a controller requested to play a specific media item represented by a URI through MediaController2#playFromUri(Uri, Bundle)

open Unit
onPrepareFromUri(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull uri: Uri, @Nullable extras: Bundle?)

Called when a controller requested to prepare a specific media item represented by a URI through MediaController2#prepareFromUri(Uri, Bundle).

open Unit
onPlaybackSpeedChanged(@NonNull session: MediaSession2, @NonNull player: MediaPlayerConnector, speed: Float)

Called to indicate that the playback speed has changed.

Public constructors

<init>

SessionCallback()

Callback to be called for all incoming commands from MediaController2s.

If it's not set, the session will accept all controllers and all incoming commands by default.

Public methods

onRewind

open fun onRewind(@NonNull session: MediaSession2, controller: MediaSession2.ControllerInfo!): Unit

Called when a controller called MediaController2#rewind()

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information

onMediaPrepared

open fun onMediaPrepared(@NonNull session: MediaSession2, @NonNull player: MediaPlayerConnector, @NonNull item: MediaItem2): Unit

Called when the player is prepared, i.e. it is ready to play the content referenced by the given data source.

Parameters
session MediaSession2: the session for this event
player MediaSession2: the player for this event
item MediaSession2: the media item for which buffering is happening

onSubscribeRoutesInfo

open fun onSubscribeRoutesInfo(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo): Unit

Called when a controller called MediaController2#subscribeRoutesInfo() Session app should notify the routes information by calling MediaSession2#notifyRoutesInfoChanged(ControllerInfo, List).

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information

onUnsubscribeRoutesInfo

open fun onUnsubscribeRoutesInfo(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo): Unit

Called when a controller called MediaController2#unsubscribeRoutesInfo()

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information

onFastForward

open fun onFastForward(@NonNull session: MediaSession2, controller: MediaSession2.ControllerInfo!): Unit

Called when a controller called MediaController2#fastForward()

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information

onPlayerStateChanged

open fun onPlayerStateChanged(@NonNull session: MediaSession2, @NonNull player: MediaPlayerConnector, state: Int): Unit

Called to indicate that the state of the player has changed. See MediaPlayerConnector#getPlayerState() for polling the player state.

Parameters
session MediaSession2: the session for this event
player MediaSession2: the player for this event
state MediaSession2: the new state of the player.

onPlaylistMetadataChanged

open fun onPlaylistMetadataChanged(@NonNull session: MediaSession2, @NonNull playlistAgent: MediaPlaylistAgent, @Nullable metadata: MediaMetadata2?): Unit

Called when a playlist metadata is changed.

Parameters
session MediaSession2: the session for this event
playlistAgent MediaSession2: playlist agent for this event
metadata MediaSession2: new metadata

onBufferingStateChanged

open fun onBufferingStateChanged(@NonNull session: MediaSession2, @NonNull player: MediaPlayerConnector, @NonNull item: MediaItem2, state: Int): Unit

Called to report buffering events for a data source.

Parameters
session MediaSession2: the session for this event
player MediaSession2: the player for this event
item MediaSession2: the media item for which buffering is happening.
state MediaSession2: the new buffering state.

onShuffleModeChanged

open fun onShuffleModeChanged(@NonNull session: MediaSession2, @NonNull playlistAgent: MediaPlaylistAgent, shuffleMode: Int): Unit

Called when the shuffle mode is changed.

Parameters
session MediaSession2: the session for this event
playlistAgent MediaSession2: playlist agent for this event
shuffleMode MediaSession2: repeat mode

onDisconnected

open fun onDisconnected(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo): Unit

Called when a controller is disconnected

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information

onPrepareFromMediaId

open fun onPrepareFromMediaId(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull mediaId: String, @Nullable extras: Bundle?): Unit

Called when a controller requested to prepare for playing a specific mediaId through MediaController2#prepareFromMediaId(String, Bundle).

During the preparation, a session should not hold audio focus in order to allow other sessions play seamlessly. The state of playback should be updated to MediaPlayerConnector#PLAYER_STATE_PAUSED after the preparation is done.

The playback of the prepared content should start in the later calls of MediaSession2#play().

Override onPlayFromMediaId to handle requests for starting playback without preparation.

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information
mediaId MediaSession2: media id to prepare
extras MediaSession2: optional extra bundle

onSelectRoute

open fun onSelectRoute(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull route: Bundle): Unit

Called when a controller called MediaController2#selectRoute(Bundle).

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information
route MediaSession2: The route bundle from RouteInfo

onConnect

@Nullable open fun onConnect(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo): SessionCommandGroup2?

Called when a controller is created for this session. Return allowed commands for controller. By default it allows all connection requests and commands.

You can reject the connection by return null. In that case, controller receives MediaController2.ControllerCallback#onDisconnected(MediaController2) and cannot be usable.

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information.
Return
SessionCommandGroup2?: allowed commands. Can be null to reject connection.

onRepeatModeChanged

open fun onRepeatModeChanged(@NonNull session: MediaSession2, @NonNull playlistAgent: MediaPlaylistAgent, repeatMode: Int): Unit

Called when the repeat mode is changed.

Parameters
session MediaSession2: the session for this event
playlistAgent MediaSession2: playlist agent for this event
repeatMode MediaSession2: repeat mode

onCommandRequest

open fun onCommandRequest(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull command: SessionCommand2): Boolean

Called when a controller sent a command which will be sent directly to one of the following:

Return false here to reject the request and stop sending command.

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information.
command MediaSession2: a command. This method will be called for every single command.
Return
Boolean: true if you want to accept incoming command. false otherwise.

See Also

onCustomCommand

open fun onCustomCommand(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull customCommand: SessionCommand2, @Nullable args: Bundle?, @Nullable cb: ResultReceiver?): Unit

Called when a controller sent a custom command through MediaController2#sendCustomCommand(SessionCommand2, Bundle, ResultReceiver).

Interoperability: This would be also called by . If so, extra from sendCustomAction will be considered as args and customCommand would have null extra.

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information
customCommand MediaSession2: custom command.
args MediaSession2: optional arguments
cb MediaSession2: optional result receiver

onPlaylistChanged

open fun onPlaylistChanged(@NonNull session: MediaSession2, @NonNull playlistAgent: MediaPlaylistAgent, @NonNull list: MutableList<MediaItem2!>, @Nullable metadata: MediaMetadata2?): Unit

Called when a playlist is changed from the MediaPlaylistAgent.

This is called when the underlying agent has called PlaylistEventCallback#onPlaylistChanged(MediaPlaylistAgent, * List, MediaMetadata2).

Parameters
session MediaSession2: the session for this event
playlistAgent MediaSession2: playlist agent for this event
list MediaSession2: new playlist
metadata MediaSession2: new metadata

onSeekCompleted

open fun onSeekCompleted(@NonNull session: MediaSession2, @NonNull player: MediaPlayerConnector, position: Long): Unit

Called to indicate that seekTo(long) is completed.

Parameters
session MediaSession2: the session for this event.
player MediaSession2: the player that has completed seeking.
position MediaSession2: the previous seeking request.

See Also

onPrepareFromSearch

open fun onPrepareFromSearch(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull query: String, @Nullable extras: Bundle?): Unit

Called when a controller requested to prepare playback from a search query through MediaController2#prepareFromSearch(String, Bundle).

An empty query indicates that the app may prepare any music. The implementation should attempt to make a smart choice about what to play.

The state of playback should be updated to MediaPlayerConnector#PLAYER_STATE_PAUSED after the preparation is done. The playback of the prepared content should start in the later calls of MediaSession2#play().

Override onPlayFromSearch to handle requests for starting playback without preparation.

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information
query MediaSession2: query string. Can be empty to indicate any suggested media
extras MediaSession2: optional extra bundle

onPlayFromMediaId

open fun onPlayFromMediaId(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull mediaId: String, @Nullable extras: Bundle?): Unit

Called when a controller requested to play a specific mediaId through MediaController2#playFromMediaId(String, Bundle).

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information
mediaId MediaSession2: media id
extras MediaSession2: optional extra bundle

onCurrentMediaItemChanged

open fun onCurrentMediaItemChanged(@NonNull session: MediaSession2, @NonNull player: MediaPlayerConnector, @Nullable item: MediaItem2?): Unit

Called when the player's current playing item is changed

When it's called, you should invalidate previous playback information and wait for later callbacks.

Parameters
session MediaSession2: the controller for this event
player MediaSession2: the player for this event
item MediaSession2: new item

onSetRating

open fun onSetRating(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull mediaId: String, @NonNull rating: Rating2): Unit

Called when a controller set rating of a media item through MediaController2#setRating(String, Rating2).

To allow setting user rating for a MediaItem2, the media item's metadata should have Rating2 with the key MediaMetadata2#METADATA_KEY_USER_RATING, in order to provide possible rating style for controller. Controller will follow the rating style.

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information
mediaId MediaSession2: media id from the controller
rating MediaSession2: new rating from the controller

onPlayFromSearch

open fun onPlayFromSearch(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull query: String, @Nullable extras: Bundle?): Unit

Called when a controller requested to begin playback from a search query through MediaController2#playFromSearch(String, Bundle)

An empty query indicates that the app may play any music. The implementation should attempt to make a smart choice about what to play.

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information
query MediaSession2: query string. Can be empty to indicate any suggested media
extras MediaSession2: optional extra bundle

onPlayFromUri

open fun onPlayFromUri(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull uri: Uri, @Nullable extras: Bundle?): Unit

Called when a controller requested to play a specific media item represented by a URI through MediaController2#playFromUri(Uri, Bundle)

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information
uri MediaSession2: uri
extras MediaSession2: optional extra bundle

onPrepareFromUri

open fun onPrepareFromUri(@NonNull session: MediaSession2, @NonNull controller: MediaSession2.ControllerInfo, @NonNull uri: Uri, @Nullable extras: Bundle?): Unit

Called when a controller requested to prepare a specific media item represented by a URI through MediaController2#prepareFromUri(Uri, Bundle).

During the preparation, a session should not hold audio focus in order to allow other sessions play seamlessly. The state of playback should be updated to MediaPlayerConnector#PLAYER_STATE_PAUSED after the preparation is done.

The playback of the prepared content should start in the later calls of MediaSession2#play().

Override onPlayFromUri to handle requests for starting playback without preparation.

Parameters
session MediaSession2: the session for this event
controller MediaSession2: controller information
uri MediaSession2: uri
extras MediaSession2: optional extra bundle

onPlaybackSpeedChanged

open fun onPlaybackSpeedChanged(@NonNull session: MediaSession2, @NonNull player: MediaPlayerConnector, speed: Float): Unit

Called to indicate that the playback speed has changed.

Parameters
session MediaSession2: the session for this event
player MediaSession2: the player for this event
speed MediaSession2: the new playback speed.