Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

MediaSession.Callback

public static abstract class MediaSession.Callback
extends Object

java.lang.Object
   ↳ android.media.session.MediaSession.Callback


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

Summary

Public constructors

MediaSession.Callback()

Public methods

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

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

void onCustomAction(String action, Bundle extras)

Called when a MediaController wants a PlaybackState.CustomAction to be performed.

void onFastForward()

Override to handle requests to fast forward.

boolean onMediaButtonEvent(Intent mediaButtonIntent)

Called when a media button is pressed and this session has the highest priority or a controller sends a media button event to the session.

void onPause()

Override to handle requests to pause playback.

void onPlay()

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's MediaBrowserService.

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 onPrepare()

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's MediaBrowserService.

void onPrepareFromSearch(String query, Bundle extras)

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 onRewind()

Override to handle requests to rewind.

void onSeekTo(long pos)

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

void onSetRating(Rating rating)

Override to handle the item being rated.

void onSkipToNext()

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

void onSkipToPrevious()

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

void onSkipToQueueItem(long id)

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

void onStop()

Override to handle requests to stop playback.

Inherited methods

Public constructors

MediaSession.Callback

added in API level 21
public MediaSession.Callback ()

Public methods

onCommand

added in API level 21
public void onCommand (String command, 
                Bundle args, 
                ResultReceiver cb)

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

Parameters
command String: The command name.

This value must never be null.

args Bundle: Optional parameters for the command, may be null.

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

onCustomAction

added in API level 21
public void onCustomAction (String action, 
                Bundle extras)

Called when a MediaController wants a PlaybackState.CustomAction to be performed.

Parameters
action String: The action that was originally sent in the PlaybackState.CustomAction.

This value must never be null.

extras Bundle: Optional extras specified by the MediaController.

This value may be null.

onFastForward

added in API level 21
public void onFastForward ()

Override to handle requests to fast forward.

onMediaButtonEvent

added in API level 21
public boolean onMediaButtonEvent (Intent mediaButtonIntent)

Called when a media button is pressed and this session has the highest priority or a controller sends a media button event to the session. The default behavior will call the relevant method if the action for it was set.

The intent will be of type Intent.ACTION_MEDIA_BUTTON with a KeyEvent in Intent.EXTRA_KEY_EVENT

Parameters
mediaButtonIntent Intent: an intent containing the KeyEvent as an extra

This value must never be null.

Returns
boolean True if the event was handled, false otherwise.

onPause

added in API level 21
public void onPause ()

Override to handle requests to pause playback.

onPlay

added in API level 21
public void onPlay ()

Override to handle requests to begin playback.

onPlayFromMediaId

added in API level 21
public void onPlayFromMediaId (String mediaId, 
                Bundle extras)

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

Parameters
mediaId String

extras Bundle

onPlayFromSearch

added in API level 21
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.

Parameters
query String

extras Bundle

onPlayFromUri

added in API level 23
public void onPlayFromUri (Uri uri, 
                Bundle extras)

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

Parameters
uri Uri

extras Bundle

onPrepare

added in API level 24
public void onPrepare ()

Override to handle requests to prepare playback. 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 PlaybackState.STATE_PAUSED after the preparation is done.

onPrepareFromMediaId

added in API level 24
public void onPrepareFromMediaId (String mediaId, 
                Bundle extras)

Override to handle requests to prepare for playing a specific mediaId that was provided by your app's MediaBrowserService. 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 PlaybackState.STATE_PAUSED after the preparation is done. The playback of the prepared content should start in the implementation of onPlay(). Override onPlayFromMediaId(String, Bundle) to handle requests for starting playback without preparation.

Parameters
mediaId String

extras Bundle

onPrepareFromSearch

added in API level 24
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. 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 PlaybackState.STATE_PAUSED after the preparation is done. The playback of the prepared content should start in the implementation of onPlay(). Override onPlayFromSearch(String, Bundle) to handle requests for starting playback without preparation.

Parameters
query String

extras Bundle

onPrepareFromUri

added in API level 24
public void onPrepareFromUri (Uri uri, 
                Bundle extras)

Override to handle requests to prepare a specific media item represented by a URI. 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 PlaybackState.STATE_PAUSED after the preparation is done. The playback of the prepared content should start in the implementation of onPlay(). Override onPlayFromUri(Uri, Bundle) to handle requests for starting playback without preparation.

Parameters
uri Uri

extras Bundle

onRewind

added in API level 21
public void onRewind ()

Override to handle requests to rewind.

onSeekTo

added in API level 21
public void onSeekTo (long pos)

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

Parameters
pos long: New position to move to, in milliseconds.

onSetRating

added in API level 21
public void onSetRating (Rating rating)

Override to handle the item being rated.

onSkipToNext

added in API level 21
public void onSkipToNext ()

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

onSkipToPrevious

added in API level 21
public void onSkipToPrevious ()

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

onSkipToQueueItem

added in API level 21
public void onSkipToQueueItem (long id)

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

Parameters
id long

onStop

added in API level 21
public void onStop ()

Override to handle requests to stop playback.