Register now for Android Dev Summit 2019!

MediaController.ControllerCallback

public static abstract class MediaController.ControllerCallback
extends Object

java.lang.Object
   ↳ androidx.media2.session.MediaController.ControllerCallback


Interface for listening to change in activeness of the MediaSession. It's active if and only if it has set a player.

Summary

Public constructors

MediaController.ControllerCallback()

Public methods

void onAllowedCommandsChanged(MediaController controller, SessionCommandGroup commands)

Called when the allowed commands are changed by session.

void onBufferingStateChanged(MediaController controller, MediaItem item, int state)

Called to report buffering events for a media item.

void onConnected(MediaController controller, SessionCommandGroup allowedCommands)

Called when the controller is successfully connected to the session.

void onCurrentMediaItemChanged(MediaController controller, MediaItem item)

Called when the player's current item is changed.

SessionResult onCustomCommand(MediaController controller, SessionCommand command, Bundle args)

Called when the session sent a custom command.

void onDisconnected(MediaController controller)

Called when the session refuses the controller or the controller is disconnected from the session.

void onPlaybackCompleted(MediaController controller)

Called when the playback is completed.

void onPlaybackInfoChanged(MediaController controller, MediaController.PlaybackInfo info)

Called when the session has changed anything related with the MediaController.PlaybackInfo.

void onPlaybackSpeedChanged(MediaController controller, float speed)

Called when playback speed is changed.

void onPlayerStateChanged(MediaController controller, int state)

Called when the player state is changed.

void onPlaylistChanged(MediaController controller, List<MediaItem> list, MediaMetadata metadata)

Called when a playlist is changed.

void onPlaylistMetadataChanged(MediaController controller, MediaMetadata metadata)

Called when a playlist metadata is changed.

void onRepeatModeChanged(MediaController controller, int repeatMode)

Called when the repeat mode is changed.

void onSeekCompleted(MediaController controller, long position)

Called to indicate that seeking is completed.

int onSetCustomLayout(MediaController controller, List<MediaSession.CommandButton> layout)

Called when the session set the custom layout through the MediaSession.setCustomLayout(MediaSession.ControllerInfo, List).

void onShuffleModeChanged(MediaController controller, int shuffleMode)

Called when the shuffle mode is changed.

Inherited methods

Public constructors

MediaController.ControllerCallback

public MediaController.ControllerCallback ()

Public methods

onAllowedCommandsChanged

public void onAllowedCommandsChanged (MediaController controller, 
                SessionCommandGroup commands)

Called when the allowed commands are changed by session.

Parameters
controller MediaController: the controller for this event

commands SessionCommandGroup: newly allowed commands

onBufferingStateChanged

public void onBufferingStateChanged (MediaController controller, 
                MediaItem item, 
                int state)

Called to report buffering events for a media item.

Use MediaController.getBufferedPosition() for current buffering position.

Parameters
controller MediaController: the controller for this event

item MediaItem: the media item for which buffering is happening.

state int: the new buffering state.

onConnected

public void onConnected (MediaController controller, 
                SessionCommandGroup allowedCommands)

Called when the controller is successfully connected to the session. The controller becomes available afterwards.

Parameters
controller MediaController: the controller for this event

allowedCommands SessionCommandGroup: commands that's allowed by the session.

onCurrentMediaItemChanged

public void onCurrentMediaItemChanged (MediaController controller, 
                MediaItem item)

Called when the player's current item is changed. It's also called after MediaController.setPlaylist(List, MediaMetadata) or MediaController.setMediaItem(String).

When it's called, you should invalidate previous playback information and wait for later callbacks. Also, current, previous, and next media item indices may need to be updated.

Parameters
controller MediaController: the controller for this event

item MediaItem: new current media item

onCustomCommand

public SessionResult onCustomCommand (MediaController controller, 
                SessionCommand command, 
                Bundle args)

Called when the session sent a custom command. Returns a SessionResult for session to get notification back. If the null is returned, SessionResult.RESULT_ERROR_UNKNOWN will be returned.

Default implementation returns SessionResult.RESULT_ERROR_NOT_SUPPORTED.

Parameters
controller MediaController: the controller for this event

Returns
SessionResult result of handling custom command

onDisconnected

public void onDisconnected (MediaController controller)

Called when the session refuses the controller or the controller is disconnected from the session. The controller becomes unavailable afterwards and the callback wouldn't be called.

It will be also called after the MediaController.close(), so you can put clean up code here. You don't need to call MediaController.close() after this.

Parameters
controller MediaController: the controller for this event

onPlaybackCompleted

public void onPlaybackCompleted (MediaController controller)

Called when the playback is completed.

Parameters
controller MediaController: the controller for this event

onPlaybackInfoChanged

public void onPlaybackInfoChanged (MediaController controller, 
                MediaController.PlaybackInfo info)

Called when the session has changed anything related with the MediaController.PlaybackInfo.

Interoperability: When connected to MediaSessionCompat, this may be called when the session changes playback info by calling MediaSessionCompat.setPlaybackToLocal(int) or MediaSessionCompat.setPlaybackToRemote(VolumeProviderCompat)}. Specifically:

  • Prior to API 21, this will always be called whenever any of those two methods is called.
  • From API 21 to 22, this is called only when the playback type is changed from local to remote (i.e. not from remote to local).
  • From API 23, this is called only when the playback type is changed.

Parameters
controller MediaController: the controller for this event

info MediaController.PlaybackInfo: new playback info

onPlaybackSpeedChanged

public void onPlaybackSpeedChanged (MediaController controller, 
                float speed)

Called when playback speed is changed.

Parameters
controller MediaController: the controller for this event

speed float: speed

onPlayerStateChanged

public void onPlayerStateChanged (MediaController controller, 
                int state)

Called when the player state is changed.

Parameters
controller MediaController: the controller for this event

state int: the new player state

onPlaylistChanged

public void onPlaylistChanged (MediaController controller, 
                List<MediaItem> list, 
                MediaMetadata metadata)

Called when a playlist is changed. It's also called after MediaController.setPlaylist(List, MediaMetadata) or MediaController.setMediaItem(String).

When it's called, current, previous, and next media item indices may need to be updated.

Parameters
controller MediaController: the controller for this event

list List: new playlist

metadata MediaMetadata: new metadata

onPlaylistMetadataChanged

public void onPlaylistMetadataChanged (MediaController controller, 
                MediaMetadata metadata)

Called when a playlist metadata is changed.

Parameters
controller MediaController: the controller for this event

metadata MediaMetadata: new metadata

onRepeatModeChanged

public void onRepeatModeChanged (MediaController controller, 
                int repeatMode)

Called when the repeat mode is changed.

Parameters
controller MediaController: the controller for this event

repeatMode int: repeat mode

onSeekCompleted

public void onSeekCompleted (MediaController controller, 
                long position)

Called to indicate that seeking is completed.

Parameters
controller MediaController: the controller for this event.

position long: the previous seeking request.

onSetCustomLayout

public int onSetCustomLayout (MediaController controller, 
                List<MediaSession.CommandButton> layout)

Called when the session set the custom layout through the MediaSession.setCustomLayout(MediaSession.ControllerInfo, List).

Can be called before onConnected(MediaController, SessionCommandGroup) is called.

Default implementation returns SessionResult.RESULT_ERROR_NOT_SUPPORTED.

Parameters
controller MediaController: the controller for this event

Returns
int

onShuffleModeChanged

public void onShuffleModeChanged (MediaController controller, 
                int shuffleMode)

Called when the shuffle mode is changed.

Parameters
controller MediaController: the controller for this event

shuffleMode int: repeat mode