Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/media/session/MediaSession.Callback#pubctors) \| [Methods](https://developer.android.com/reference/android/media/session/MediaSession.Callback#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/media/session/MediaSession.Callback#inhmethods)

# MediaSession.Callback

*** ** * ** ***

` public static abstract class MediaSession.Callback `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.media.session.MediaSession.Callback |

<br />

*** ** * ** ***

Receives media buttons, transport controls, and commands from controllers and the system. A callback may be set using `https://developer.android.com/reference/android/media/session/MediaSession#setCallback(android.media.session.MediaSession.Callback)`.

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#Callback()() ` |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onCommand(java.lang.String,%20android.os.Bundle,%20android.os.ResultReceiver)(https://developer.android.com/reference/java/lang/String command, https://developer.android.com/reference/android/os/Bundle args, https://developer.android.com/reference/android/os/ResultReceiver cb) ` Called when a controller has sent a command to this session. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onCustomAction(java.lang.String,%20android.os.Bundle)(https://developer.android.com/reference/java/lang/String action, https://developer.android.com/reference/android/os/Bundle extras) ` Called when a `https://developer.android.com/reference/android/media/session/MediaController` wants a `https://developer.android.com/reference/android/media/session/PlaybackState.CustomAction` to be performed. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onFastForward()() ` Override to handle requests to fast forward. |
| ` boolean` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onMediaButtonEvent(android.content.Intent)(https://developer.android.com/reference/android/content/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` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPause()() ` Override to handle requests to pause playback. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPlay()() ` Override to handle requests to begin playback. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPlayFromMediaId(java.lang.String,%20android.os.Bundle)(https://developer.android.com/reference/java/lang/String mediaId, https://developer.android.com/reference/android/os/Bundle extras) ` Override to handle requests to play a specific mediaId that was provided by your app's `https://developer.android.com/reference/android/service/media/MediaBrowserService`. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPlayFromSearch(java.lang.String,%20android.os.Bundle)(https://developer.android.com/reference/java/lang/String query, https://developer.android.com/reference/android/os/Bundle extras) ` Override to handle requests to begin playback from a search query. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPlayFromUri(android.net.Uri,%20android.os.Bundle)(https://developer.android.com/reference/android/net/Uri uri, https://developer.android.com/reference/android/os/Bundle extras) ` Override to handle requests to play a specific media item represented by a URI. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPrepare()() ` Override to handle requests to prepare playback. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPrepareFromMediaId(java.lang.String,%20android.os.Bundle)(https://developer.android.com/reference/java/lang/String mediaId, https://developer.android.com/reference/android/os/Bundle extras) ` Override to handle requests to prepare for playing a specific mediaId that was provided by your app's `https://developer.android.com/reference/android/service/media/MediaBrowserService`. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPrepareFromSearch(java.lang.String,%20android.os.Bundle)(https://developer.android.com/reference/java/lang/String query, https://developer.android.com/reference/android/os/Bundle extras) ` Override to handle requests to prepare playback from a search query. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPrepareFromUri(android.net.Uri,%20android.os.Bundle)(https://developer.android.com/reference/android/net/Uri uri, https://developer.android.com/reference/android/os/Bundle extras) ` Override to handle requests to prepare a specific media item represented by a URI. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onRewind()() ` Override to handle requests to rewind. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onSeekTo(long)(long pos) ` Override to handle requests to seek to a specific position in ms. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onSetPlaybackSpeed(float)(float speed) ` Override to handle the playback speed change. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onSetRating(android.media.Rating)(https://developer.android.com/reference/android/media/Rating rating) ` Override to handle the item being rated. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onSkipToNext()() ` Override to handle requests to skip to the next media item. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onSkipToPrevious()() ` Override to handle requests to skip to the previous media item. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onSkipToQueueItem(long)(long id) ` Override to handle requests to play an item with a given id from the play queue. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSession.Callback#onStop()() ` Override to handle requests to stop playback. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public constructors

### Callback

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Callback ()
```

<br />

<br />

## Public methods

### onCommand

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `command` | `String`: The command name. This value cannot be `null`. <br /> |
| `args` | `Bundle`: Optional parameters for the command, may be null. <br /> |
| `cb` | `ResultReceiver`: A result receiver to which a result may be sent by the command, may be null. <br /> |

### onCustomAction

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onCustomAction (String action, 
                Bundle extras)
```

Called when a `https://developer.android.com/reference/android/media/session/MediaController` wants a `https://developer.android.com/reference/android/media/session/PlaybackState.CustomAction` to be performed.

<br />

| Parameters ||
|---|---|
| `action` | `String`: The action that was originally sent in the `https://developer.android.com/reference/android/media/session/PlaybackState.CustomAction`. This value cannot be `null`. <br /> |
| `extras` | `Bundle`: Optional extras specified by the `https://developer.android.com/reference/android/media/session/MediaController`. This value may be `null`. <br /> |

### onFastForward

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onFastForward ()
```

Override to handle requests to fast forward.

<br />

### onMediaButtonEvent

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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 `https://developer.android.com/reference/android/content/Intent#ACTION_MEDIA_BUTTON` with a KeyEvent in `https://developer.android.com/reference/android/content/Intent#EXTRA_KEY_EVENT`

<br />

| Parameters ||
|---|---|
| `mediaButtonIntent` | `Intent`: an intent containing the KeyEvent as an extra. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `boolean` | True if the event was handled, false otherwise. <br /> |

### onPause

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onPause ()
```

Override to handle requests to pause playback.

<br />

### onPlay

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onPlay ()
```

Override to handle requests to begin playback.

<br />

### onPlayFromMediaId

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onPlayFromMediaId (String mediaId, 
                Bundle extras)
```

Override to handle requests to play a specific mediaId that was provided by your app's `https://developer.android.com/reference/android/service/media/MediaBrowserService`.

<br />

| Parameters ||
|---|---|
| `mediaId` | `String` <br /> |
| `extras` | `Bundle` <br /> |

### onPlayFromSearch

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `query` | `String` <br /> |
| `extras` | `Bundle` <br /> |

### onPlayFromUri

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onPlayFromUri (Uri uri, 
                Bundle extras)
```

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

<br />

| Parameters ||
|---|---|
| `uri` | `Uri` <br /> |
| `extras` | `Bundle` <br /> |

### onPrepare

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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 `https://developer.android.com/reference/android/media/session/PlaybackState#STATE_PAUSED` after the preparation is done.

<br />

### onPrepareFromMediaId

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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 `https://developer.android.com/reference/android/service/media/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 `https://developer.android.com/reference/android/media/session/PlaybackState#STATE_PAUSED` after the preparation is done. The playback of the prepared content should start in the implementation of `https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPlay()`. Override `https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPlayFromMediaId(java.lang.String,%20android.os.Bundle)` to handle requests for starting playback without preparation.

<br />

| Parameters ||
|---|---|
| `mediaId` | `String` <br /> |
| `extras` | `Bundle` <br /> |

### onPrepareFromSearch

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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 `https://developer.android.com/reference/android/media/session/PlaybackState#STATE_PAUSED` after the preparation is done. The playback of the prepared content should start in the implementation of `https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPlay()`. Override `https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPlayFromSearch(java.lang.String,%20android.os.Bundle)` to handle requests for starting playback without preparation.

<br />

| Parameters ||
|---|---|
| `query` | `String` <br /> |
| `extras` | `Bundle` <br /> |

### onPrepareFromUri

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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 `https://developer.android.com/reference/android/media/session/PlaybackState#STATE_PAUSED` after the preparation is done. The playback of the prepared content should start in the implementation of `https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPlay()`. Override `https://developer.android.com/reference/android/media/session/MediaSession.Callback#onPlayFromUri(android.net.Uri,%20android.os.Bundle)` to handle requests for starting playback without preparation.

<br />

| Parameters ||
|---|---|
| `uri` | `Uri` <br /> |
| `extras` | `Bundle` <br /> |

### onRewind

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onRewind ()
```

Override to handle requests to rewind.

<br />

### onSeekTo

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onSeekTo (long pos)
```

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

<br />

| Parameters ||
|---|---|
| `pos` | `long`: New position to move to, in milliseconds. <br /> |

### onSetPlaybackSpeed

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onSetPlaybackSpeed (float speed)
```

Override to handle the playback speed change. To update the new playback speed, create a new `https://developer.android.com/reference/android/media/session/PlaybackState` by using `https://developer.android.com/reference/android/media/session/PlaybackState.Builder#setState(int,%20long,%20float)`, and set it with `https://developer.android.com/reference/android/media/session/MediaSession#setPlaybackState(android.media.session.PlaybackState)`.

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.

<br />

| Parameters ||
|---|---|
| `speed` | `float`: the playback speed <br /> |

**See also:**

- `https://developer.android.com/reference/android/media/session/MediaSession#setPlaybackState(android.media.session.PlaybackState)`
- `https://developer.android.com/reference/android/media/session/PlaybackState.Builder#setState(int,%20long,%20float)`

### onSetRating

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onSetRating (Rating rating)
```

Override to handle the item being rated.

<br />

| Parameters ||
|---|---|
| `rating` | `Rating`: This value cannot be `null`. <br /> |

### onSkipToNext

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onSkipToNext ()
```

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

<br />

### onSkipToPrevious

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onSkipToPrevious ()
```

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

<br />

### onSkipToQueueItem

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onSkipToQueueItem (long id)
```

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

<br />

| Parameters ||
|---|---|
| `id` | `long` <br /> |

### onStop

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onStop ()
```

Override to handle requests to stop playback.

<br />