public abstract class

MediaSession.SessionCallback

extends java.lang.Object

 java.lang.Object

↳androidx.media2.MediaSession.SessionCallback

Subclasses:

MediaLibraryService.MediaLibrarySession.MediaLibrarySessionCallback

Overview

Callback to be called for all incoming commands from MediaControllers.

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

Summary

Constructors
publicSessionCallback()

Methods
public intonCommandRequest(MediaSession session, MediaSession.ControllerInfo controller, SessionCommand command)

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

public SessionCommandGrouponConnect(MediaSession session, MediaSession.ControllerInfo controller)

Called when a controller is created for this session.

public MediaItemonCreateMediaItem(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String mediaId)

Called when a controller has sent a command with a MediaItem to add a new media item to this session.

public MediaSession.SessionResultonCustomCommand(MediaSession session, MediaSession.ControllerInfo controller, SessionCommand customCommand, Bundle args)

Called when a controller sent a custom command through MediaController.sendCustomCommand(SessionCommand, Bundle).

public voidonDisconnected(MediaSession session, MediaSession.ControllerInfo controller)

Called when a controller is disconnected

public intonFastForward(MediaSession session, MediaSession.ControllerInfo controller)

Called when a controller called MediaController.fastForward().

public intonPlayFromMediaId(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String mediaId, Bundle extras)

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

public intonPlayFromSearch(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String query, Bundle extras)

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

public intonPlayFromUri(MediaSession session, MediaSession.ControllerInfo controller, Uri uri, Bundle extras)

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

public intonPrepareFromMediaId(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String mediaId, Bundle extras)

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

public intonPrepareFromSearch(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String query, Bundle extras)

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

public intonPrepareFromUri(MediaSession session, MediaSession.ControllerInfo controller, Uri uri, Bundle extras)

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

public intonRewind(MediaSession session, MediaSession.ControllerInfo controller)

Called when a controller called MediaController.rewind().

public intonSetRating(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String mediaId, Rating rating)

Called when a controller set rating of a media item through MediaController.setRating(String, Rating).

public intonSkipBackward(MediaSession session, MediaSession.ControllerInfo controller)

Called when a controller called MediaController.skipBackward().

public intonSkipForward(MediaSession session, MediaSession.ControllerInfo controller)

Called when a controller called MediaController.skipForward().

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public SessionCallback()

Methods

public SessionCommandGroup onConnect(MediaSession session, MediaSession.ControllerInfo controller)

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 MediaController.ControllerCallback.onDisconnected(MediaController) and cannot be usable.

Parameters:

session: the session for this event
controller: controller information.

Returns:

allowed commands. Can be null to reject connection.

public void onDisconnected(MediaSession session, MediaSession.ControllerInfo controller)

Called when a controller is disconnected

Parameters:

session: the session for this event
controller: controller information

public int onCommandRequest(MediaSession session, MediaSession.ControllerInfo controller, SessionCommand command)

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

Return RESULT_CODE_SUCCESS to proceed the command. If something else is returned, command wouldn't be sent and the controller would receive the code with it.

Parameters:

session: the session for this event
controller: controller information.
command: a command. This method will be called for every single command.

Returns:

RESULT_CODE_SUCCESS if you want to proceed with incoming command. Another code for ignore.

See also: SessionCommand.COMMAND_CODE_PLAYER_PLAY, SessionCommand.COMMAND_CODE_PLAYER_PAUSE, SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_NEXT_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PREVIOUS_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_PREPARE, SessionCommand.COMMAND_CODE_PLAYER_SEEK_TO, SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_SET_SHUFFLE_MODE, SessionCommand.COMMAND_CODE_PLAYER_SET_REPEAT_MODE, SessionCommand.COMMAND_CODE_PLAYER_ADD_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_REMOVE_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_REPLACE_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_GET_PLAYLIST, SessionCommand.COMMAND_CODE_PLAYER_SET_PLAYLIST, SessionCommand.COMMAND_CODE_PLAYER_GET_PLAYLIST_METADATA, SessionCommand.COMMAND_CODE_PLAYER_UPDATE_LIST_METADATA, SessionCommand.COMMAND_CODE_VOLUME_SET_VOLUME, SessionCommand.COMMAND_CODE_VOLUME_ADJUST_VOLUME

public MediaItem onCreateMediaItem(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String mediaId)

Called when a controller has sent a command with a MediaItem to add a new media item to this session. Being specific, this will be called for following APIs.

  1. MediaController.addPlaylistItem(int, String)
  2. MediaController.replacePlaylistItem(int, String)
  3. MediaController.setPlaylist(List, MediaMetadata)
  4. MediaController.setMediaItem(String)
Override this to translate incoming mediaId to a MediaItem to be understood by your player. For example, a player may only understand FileMediaItem, UriMediaItem, and CallbackMediaItem. Check the documentation of the player that you're using.

If the given media ID is valid, you should return the media item with the given media ID. If the ID doesn't match, an java.lang.RuntimeException will be thrown. You may return null if the given item is invalid. Here's the behavior when it happens.

Controller command Behavior when null is returned
addPlaylistItem Ignore
replacePlaylistItem Ignore
setPlaylist Ignore null items, and build a list with non-null items. Call SessionPlayer.setPlaylist(List, MediaMetadata) with the list
setMediaItem Ignore

This will be called on the same thread where MediaSession.SessionCallback.onCommandRequest(MediaSession, MediaSession.ControllerInfo, SessionCommand) and commands with the media controller will be executed.

Default implementation returns the null.

Parameters:

session: the session for this event
controller: controller information
mediaId: non-empty media id for creating item with

Returns:

translated media item for player with the mediaId. Can be null to ignore.

See also: MediaMetadata.METADATA_KEY_MEDIA_ID

public int onSetRating(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String mediaId, Rating rating)

Called when a controller set rating of a media item through MediaController.setRating(String, Rating).

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

Parameters:

session: the session for this event
controller: controller information
mediaId: non-empty media id
rating: new rating from the controller

See also: SessionCommand.COMMAND_CODE_SESSION_SET_RATING

public MediaSession.SessionResult onCustomCommand(MediaSession session, MediaSession.ControllerInfo controller, SessionCommand customCommand, Bundle args)

Called when a controller sent a custom command through MediaController.sendCustomCommand(SessionCommand, Bundle).

Interoperability: This would be also called by #sendCustomAction(String, Bundle, CustomActionCallback). If so, extra from sendCustomAction will be considered as args and customCommand would have null extra.

Parameters:

session: the session for this event
controller: controller information
customCommand: custom command.
args: optional arguments

Returns:

result of handling custom command. A runtime exception will be thrown if null is returned.

See also: SessionCommand.COMMAND_CODE_CUSTOM

public int onPlayFromMediaId(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String mediaId, Bundle extras)

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

Parameters:

session: the session for this event
controller: controller information
mediaId: non-empty media id
extras: optional extra bundle

See also: SessionCommand.COMMAND_CODE_SESSION_PLAY_FROM_MEDIA_ID

public int onPlayFromSearch(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String query, Bundle extras)

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

Parameters:

session: the session for this event
controller: controller information
query: non-empty search query.
extras: optional extra bundle

See also: SessionCommand.COMMAND_CODE_SESSION_PLAY_FROM_SEARCH

public int onPlayFromUri(MediaSession session, MediaSession.ControllerInfo controller, Uri uri, Bundle extras)

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

Parameters:

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

See also: SessionCommand.COMMAND_CODE_SESSION_PLAY_FROM_URI

public int onPrepareFromMediaId(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String mediaId, Bundle extras)

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

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

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

Override MediaSession.SessionCallback.onPlayFromMediaId(MediaSession, MediaSession.ControllerInfo, String, Bundle) to handle requests for starting playback without preparation.

Parameters:

session: the session for this event
controller: controller information
mediaId: non-empty media id
extras: optional extra bundle

See also: SessionCommand.COMMAND_CODE_SESSION_PREPARE_FROM_MEDIA_ID

public int onPrepareFromSearch(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String query, Bundle extras)

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

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

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

Override MediaSession.SessionCallback.onPlayFromSearch(MediaSession, MediaSession.ControllerInfo, String, Bundle) to handle requests for starting playback without preparation.

Parameters:

session: the session for this event
controller: controller information
query: non-empty search query
extras: optional extra bundle

See also: SessionCommand.COMMAND_CODE_SESSION_PREPARE_FROM_SEARCH

public int onPrepareFromUri(MediaSession session, MediaSession.ControllerInfo controller, Uri uri, Bundle extras)

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

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

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

Override MediaSession.SessionCallback.onPlayFromUri(MediaSession, MediaSession.ControllerInfo, Uri, Bundle) to handle requests for starting playback without preparation.

Parameters:

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

See also: SessionCommand.COMMAND_CODE_SESSION_PREPARE_FROM_URI

public int onFastForward(MediaSession session, MediaSession.ControllerInfo controller)

Called when a controller called MediaController.fastForward().

It's recommended to increase the playback speed when this method is called.

Parameters:

session: the session for this event
controller: controller information

See also: SessionCommand.COMMAND_CODE_SESSION_FAST_FORWARD

public int onRewind(MediaSession session, MediaSession.ControllerInfo controller)

Called when a controller called MediaController.rewind().

It's recommended to decrease the playback speed when this method is called.

Parameters:

session: the session for this event
controller: controller information

See also: SessionCommand.COMMAND_CODE_SESSION_REWIND

public int onSkipForward(MediaSession session, MediaSession.ControllerInfo controller)

Called when a controller called MediaController.skipForward().

It's recommended to seek forward within the current media item when this method is called.

Parameters:

session: the session for this event
controller: controller information

See also: SessionCommand.COMMAND_CODE_SESSION_SKIP_FORWARD

public int onSkipBackward(MediaSession session, MediaSession.ControllerInfo controller)

Called when a controller called MediaController.skipBackward().

It's recommended to seek backward within the current media item when this method is called.

Parameters:

session: the session for this event
controller: controller information

See also: SessionCommand.COMMAND_CODE_SESSION_SKIP_BACKWARD