MediaSessionCompat.Callback

public abstract class MediaSessionCompat.Callback


Receives transport controls, media buttons, and commands from controllers and the system. The callback may be set using setCallback.

Don't reuse the callback among the sessions. Callbacks keep internal reference to the session when it's set, so it may misbehave.

Summary

Public constructors

Public methods

void

Called when a MediaControllerCompat wants to add a QueueItem with the given description at the end of the play queue.

void
onAddQueueItem(MediaDescriptionCompat description, int index)

Called when a MediaControllerCompat wants to add a QueueItem with the given description at the specified position in the play queue.

void
onCommand(String command, Bundle extras, ResultReceiver cb)

Called when a controller has sent a custom command to this session.

void
onCustomAction(String action, Bundle extras)

Called when a MediaControllerCompat wants a PlaybackStateCompat.CustomAction to be performed.

void

Override to handle requests to fast forward.

boolean
onMediaButtonEvent(Intent mediaButtonEvent)

Override to handle media button events.

void

Override to handle requests to pause playback.

void

Override to handle requests to begin playback.

void
onPlayFromMediaId(String mediaId, Bundle extras)

Override to handle requests to play a specific mediaId that was provided by your app.

void
onPlayFromSearch(String query, Bundle extras)

Override to handle requests to begin playback from a search query.

void
onPlayFromUri(Uri uri, Bundle extras)

Override to handle requests to play a specific media item represented by a URI.

void

Override to handle requests to prepare playback.

void
onPrepareFromMediaId(String mediaId, Bundle extras)

Override to handle requests to prepare for playing a specific mediaId that was provided by your app.

void

Override to handle requests to prepare playback from a search query.

void
onPrepareFromUri(Uri uri, Bundle extras)

Override to handle requests to prepare a specific media item represented by a URI.

void

Called when a MediaControllerCompat wants to remove the first occurrence of the specified QueueItem with the given description in the play queue.

void

This method is deprecated.

onRemoveQueueItem will be called instead.

void

Override to handle requests to rewind.

void
onSeekTo(long pos)

Override to handle requests to seek to a specific position in ms.

void
onSetCaptioningEnabled(boolean enabled)

Override to handle requests to enable/disable captioning.

void
onSetPlaybackSpeed(float speed)

Override to handle the playback speed change.

void

Override to handle the item being rated.

void
onSetRating(RatingCompat rating, Bundle extras)

Override to handle the item being rated.

void
onSetRepeatMode(int repeatMode)

Override to handle the setting of the repeat mode.

void
onSetShuffleMode(int shuffleMode)

Override to handle the setting of the shuffle mode.

void

Override to handle requests to skip to the next media item.

void

Override to handle requests to skip to the previous media item.

void

Override to handle requests to play an item with a given id from the play queue.

void

Override to handle requests to stop playback.

Public constructors

Callback

Added in 1.1.0
public Callback()

Public methods

onAddQueueItem

Added in 1.1.0
public void onAddQueueItem(MediaDescriptionCompat description)

Called when a MediaControllerCompat wants to add a QueueItem with the given description at the end of the play queue.

Parameters
MediaDescriptionCompat description

The MediaDescriptionCompat for creating the QueueItem to be inserted.

onAddQueueItem

Added in 1.1.0
public void onAddQueueItem(MediaDescriptionCompat description, int index)

Called when a MediaControllerCompat wants to add a QueueItem with the given description at the specified position in the play queue.

Parameters
MediaDescriptionCompat description

The MediaDescriptionCompat for creating the QueueItem to be inserted.

int index

The index at which the created QueueItem is to be inserted.

onCommand

Added in 1.1.0
public void onCommand(String command, Bundle extras, ResultReceiver cb)

Called when a controller has sent a custom command to this session. The owner of the session may handle custom commands but is not required to.

Parameters
String command

The command name.

Bundle extras

Optional parameters for the command, may be null.

ResultReceiver cb

A result receiver to which a result may be sent by the command, may be null.

onCustomAction

Added in 1.1.0
public void onCustomAction(String action, Bundle extras)

Called when a MediaControllerCompat wants a PlaybackStateCompat.CustomAction to be performed.

Parameters
String action

The action that was originally sent in the PlaybackStateCompat.CustomAction.

Bundle extras

Optional extras specified by the MediaControllerCompat.

onFastForward

Added in 1.1.0
public void onFastForward()

Override to handle requests to fast forward.

onMediaButtonEvent

Added in 1.1.0
public boolean onMediaButtonEvent(Intent mediaButtonEvent)

Override to handle media button events.

The double tap of KEYCODE_MEDIA_PLAY_PAUSE or KEYCODE_HEADSETHOOK will call the onSkipToNext by default. If the current SDK level is 27 or higher, the default double tap handling is done by framework so this method would do nothing for it.

Parameters
Intent mediaButtonEvent

The media button event intent.

Returns
boolean

True if the event was handled, false otherwise.

onPause

Added in 1.1.0
public void onPause()

Override to handle requests to pause playback.

onPlay

Added in 1.1.0
public void onPlay()

Override to handle requests to begin playback.

onPlayFromMediaId

Added in 1.1.0
public void onPlayFromMediaId(String mediaId, Bundle extras)

Override to handle requests to play a specific mediaId that was provided by your app.

onPlayFromSearch

Added in 1.1.0
public void onPlayFromSearch(String query, Bundle extras)

Override to handle requests to begin playback from a search query. An empty query indicates that the app may play any music. The implementation should attempt to make a smart choice about what to play.

onPlayFromUri

Added in 1.1.0
public void onPlayFromUri(Uri uri, Bundle extras)

Override to handle requests to play a specific media item represented by a URI.

onPrepare

Added in 1.1.0
public void onPrepare()

Override to handle requests to prepare playback. Override onPlay to handle requests for starting playback.

onPrepareFromMediaId

Added in 1.1.0
public void onPrepareFromMediaId(String mediaId, Bundle extras)

Override to handle requests to prepare for playing a specific mediaId that was provided by your app. Override onPlayFromMediaId to handle requests for starting playback.

onPrepareFromSearch

Added in 1.1.0
public void onPrepareFromSearch(String query, Bundle extras)

Override to handle requests to prepare playback from a search query. An empty query indicates that the app may prepare any music. The implementation should attempt to make a smart choice about what to play. Override onPlayFromSearch to handle requests for starting playback.

onPrepareFromUri

Added in 1.1.0
public void onPrepareFromUri(Uri uri, Bundle extras)

Override to handle requests to prepare a specific media item represented by a URI. Override onPlayFromUri to handle requests for starting playback.

onRemoveQueueItem

Added in 1.1.0
public void onRemoveQueueItem(MediaDescriptionCompat description)

Called when a MediaControllerCompat wants to remove the first occurrence of the specified QueueItem with the given description in the play queue.

Parameters
MediaDescriptionCompat description

The MediaDescriptionCompat for denoting the QueueItem to be removed.

onRemoveQueueItemAt

Added in 1.1.0
Deprecated in 1.1.0
public void onRemoveQueueItemAt(int index)

Called when a MediaControllerCompat wants to remove a QueueItem at the specified position in the play queue.

Parameters
int index

The index of the element to be removed.

onRewind

Added in 1.1.0
public void onRewind()

Override to handle requests to rewind.

onSeekTo

Added in 1.1.0
public void onSeekTo(long pos)

Override to handle requests to seek to a specific position in ms.

Parameters
long pos

New position to move to, in milliseconds.

onSetCaptioningEnabled

Added in 1.1.0
public void onSetCaptioningEnabled(boolean enabled)

Override to handle requests to enable/disable captioning.

Parameters
boolean enabled

true to enable captioning, false to disable.

onSetPlaybackSpeed

Added in 1.2.0
public void onSetPlaybackSpeed(float speed)

Override to handle the playback speed change. To update the new playback speed, create a new PlaybackStateCompat by using setState, and set it with setPlaybackState.

A value of 1.0f is the default playback value, and a negative value indicates reverse playback. The speed will not be equal to zero.

Parameters
float speed

the playback speed

onSetRating

Added in 1.1.0
public void onSetRating(RatingCompat rating)

Override to handle the item being rated.

Parameters
RatingCompat rating

The rating being set.

onSetRating

Added in 1.1.0
public void onSetRating(RatingCompat rating, Bundle extras)

Override to handle the item being rated.

Parameters
RatingCompat rating

The rating being set.

Bundle extras

The extras can include information about the media item being rated.

onSetRepeatMode

Added in 1.1.0
public void onSetRepeatMode(int repeatMode)

Override to handle the setting of the repeat mode.

You should call setRepeatMode before end of this method in order to notify the change to the MediaControllerCompat, or getRepeatMode could return an invalid value.

Parameters
int repeatMode

The repeat mode which is one of followings: REPEAT_MODE_NONE, REPEAT_MODE_ONE, REPEAT_MODE_ALL, REPEAT_MODE_GROUP

onSetShuffleMode

Added in 1.1.0
public void onSetShuffleMode(int shuffleMode)

Override to handle the setting of the shuffle mode.

You should call setShuffleMode before the end of this method in order to notify the change to the MediaControllerCompat, or getShuffleMode could return an invalid value.

Parameters
int shuffleMode

The shuffle mode which is one of followings: SHUFFLE_MODE_NONE, SHUFFLE_MODE_ALL, SHUFFLE_MODE_GROUP

onSkipToNext

Added in 1.1.0
public void onSkipToNext()

Override to handle requests to skip to the next media item.

onSkipToPrevious

Added in 1.1.0
public void onSkipToPrevious()

Override to handle requests to skip to the previous media item.

onSkipToQueueItem

Added in 1.1.0
public void onSkipToQueueItem(long id)

Override to handle requests to play an item with a given id from the play queue.

onStop

Added in 1.1.0
public void onStop()

Override to handle requests to stop playback.