public abstract class

MediaSessionCompat.Callback

extends java.lang.Object

 java.lang.Object

↳androidx.media3.session.legacy.MediaSessionCompat.Callback

Overview

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

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

Summary

Constructors
publicCallback()

Methods
public voidonAddQueueItem(MediaDescriptionCompat description)

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

public voidonAddQueueItem(MediaDescriptionCompat description, int index)

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

public voidonCommand(java.lang.String command, Bundle extras, ResultReceiver cb)

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

public voidonCustomAction(java.lang.String action, Bundle extras)

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

public voidonFastForward()

Override to handle requests to fast forward.

public booleanonMediaButtonEvent(Intent mediaButtonEvent)

Override to handle media button events.

public voidonPause()

Override to handle requests to pause playback.

public voidonPlay()

Override to handle requests to begin playback.

public voidonPlayFromMediaId(java.lang.String mediaId, Bundle extras)

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

public voidonPlayFromSearch(java.lang.String query, Bundle extras)

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

public voidonPlayFromUri(Uri uri, Bundle extras)

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

public voidonPrepare()

Override to handle requests to prepare playback.

public voidonPrepareFromMediaId(java.lang.String mediaId, Bundle extras)

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

public voidonPrepareFromSearch(java.lang.String query, Bundle extras)

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

public voidonPrepareFromUri(Uri uri, Bundle extras)

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

public voidonRemoveQueueItem(MediaDescriptionCompat description)

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

public voidonRemoveQueueItemAt(int index)

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

public voidonRewind()

Override to handle requests to rewind.

public voidonSeekTo(long pos)

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

public voidonSetCaptioningEnabled(boolean enabled)

Override to handle requests to enable/disable captioning.

public voidonSetPlaybackSpeed(float speed)

Override to handle the playback speed change.

public voidonSetRating(RatingCompat rating)

Override to handle the item being rated.

public voidonSetRating(RatingCompat rating, Bundle extras)

Override to handle the item being rated.

public voidonSetRepeatMode(int repeatMode)

Override to handle the setting of the repeat mode.

public voidonSetShuffleMode(int shuffleMode)

Override to handle the setting of the shuffle mode.

public voidonSkipToNext()

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

public voidonSkipToPrevious()

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

public voidonSkipToQueueItem(long id)

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

public voidonStop()

Override to handle requests to stop playback.

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

Constructors

public Callback()

Methods

public void onCommand(java.lang.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:

command: The command name.
extras: Optional parameters for the command, may be null.
cb: A result receiver to which a result may be sent by the command, may be null.

public boolean onMediaButtonEvent(Intent mediaButtonEvent)

Override to handle media button events.

The double tap of or will call the MediaSessionCompat.Callback.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:

mediaButtonEvent: The media button event intent.

Returns:

True if the event was handled, false otherwise.

public void onPrepare()

Override to handle requests to prepare playback. Override MediaSessionCompat.Callback.onPlay() to handle requests for starting playback.

public void onPrepareFromMediaId(java.lang.String mediaId, Bundle extras)

Override to handle requests to prepare for playing a specific mediaId that was provided by your app. Override MediaSessionCompat.Callback.onPlayFromMediaId(String, Bundle) to handle requests for starting playback.

public void onPrepareFromSearch(java.lang.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 MediaSessionCompat.Callback.onPlayFromSearch(String, Bundle) to handle requests for starting playback.

public void onPrepareFromUri(Uri uri, Bundle extras)

Override to handle requests to prepare a specific media item represented by a URI. Override MediaSessionCompat.Callback.onPlayFromUri(Uri, Bundle) to handle requests for starting playback.

public void onPlay()

Override to handle requests to begin playback.

public void onPlayFromMediaId(java.lang.String mediaId, Bundle extras)

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

public void onPlayFromSearch(java.lang.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.

public void onPlayFromUri(Uri uri, Bundle extras)

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

public void onSkipToQueueItem(long id)

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

public void onPause()

Override to handle requests to pause playback.

public void onSkipToNext()

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

public void onSkipToPrevious()

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

public void onFastForward()

Override to handle requests to fast forward.

public void onRewind()

Override to handle requests to rewind.

public void onStop()

Override to handle requests to stop playback.

public void onSeekTo(long pos)

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

Parameters:

pos: New position to move to, in milliseconds.

public void onSetRating(RatingCompat rating)

Override to handle the item being rated.

Parameters:

rating: The rating being set.

public void onSetRating(RatingCompat rating, Bundle extras)

Override to handle the item being rated.

Parameters:

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

public void onSetPlaybackSpeed(float speed)

Override to handle the playback speed change. To update the new playback speed, create a new PlaybackStateCompat by using PlaybackStateCompat.Builder.setState(int, long, float), and set it with MediaSessionCompat.setPlaybackState(PlaybackStateCompat).

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:

speed: the playback speed

See also: MediaSessionCompat.setPlaybackState(PlaybackStateCompat), PlaybackStateCompat.Builder.setState(int, long, float)

public void onSetCaptioningEnabled(boolean enabled)

Override to handle requests to enable/disable captioning.

Parameters:

enabled: true to enable captioning, false to disable.

public void onSetRepeatMode(int repeatMode)

Override to handle the setting of the repeat mode.

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

Parameters:

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

public void onSetShuffleMode(int shuffleMode)

Override to handle the setting of the shuffle mode.

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

Parameters:

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

public void onCustomAction(java.lang.String action, Bundle extras)

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

Parameters:

action: The action that was originally sent in the PlaybackStateCompat.CustomAction.
extras: Optional extras specified by the MediaControllerCompat.

See also: MediaSessionCompat.ACTION_FLAG_AS_INAPPROPRIATE, MediaSessionCompat.ACTION_SKIP_AD, MediaSessionCompat.ACTION_FOLLOW, MediaSessionCompat.ACTION_UNFOLLOW

public void onAddQueueItem(MediaDescriptionCompat description)

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

Parameters:

description: The MediaDescriptionCompat for creating the MediaSessionCompat.QueueItem to be inserted.

public void onAddQueueItem(MediaDescriptionCompat description, int index)

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

Parameters:

description: The MediaDescriptionCompat for creating the MediaSessionCompat.QueueItem to be inserted.
index: The index at which the created MediaSessionCompat.QueueItem is to be inserted.

public void onRemoveQueueItem(MediaDescriptionCompat description)

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

Parameters:

description: The MediaDescriptionCompat for denoting the MediaSessionCompat.QueueItem to be removed.

public void onRemoveQueueItemAt(int index)

Deprecated: MediaSessionCompat.Callback.onRemoveQueueItem(MediaDescriptionCompat) will be called instead.

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

Parameters:

index: The index of the element to be removed.