# MediaSession.Callback

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

# Callback

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/android/media/session/MediaSession.Callback "View this page in Java")  

```
abstract class Callback
```

|---|--------------------------------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)                                                             ||
| ↳ | [android.media.session.MediaSession.Callback](https://developer.android.com/reference/kotlin/android/media/session/MediaSession.Callback#) |

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

## Summary

|                                                       Public constructors                                                       ||
|------------------------------------------------------------------------------------------------------------------------------|---|
| [Callback](https://developer.android.com/reference/kotlin/android/media/session/MediaSession.Callback#Callback())`()` <br /> |

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

## Public constructors

### Callback

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

```
Callback()
```

## Public methods

### onCommand

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

```
open fun onCommand(
    command: String, 
    args: Bundle?, 
    cb: ResultReceiver?
): Unit
```

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](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):The command name. This value cannot be`null`.                                                 |
| `args`    | [Bundle](https://developer.android.com/reference/kotlin/android/os/Bundle.html#)?:Optional parameters for the command, may be null.                                            |
| `cb`      | [ResultReceiver](https://developer.android.com/reference/kotlin/android/os/ResultReceiver.html#)?:A result receiver to which a result may be sent by the command, may be null. |

### onCustomAction

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

```
open fun onCustomAction(
    action: String, 
    extras: Bundle?
): Unit
```

Called when a[MediaController](https://developer.android.com/reference/kotlin/android/media/session/MediaController)wants a[PlaybackState.CustomAction](https://developer.android.com/reference/kotlin/android/media/session/PlaybackState.CustomAction)to be performed.

|                                                                                                                                           Parameters                                                                                                                                            ||
|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `action` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):The action that was originally sent in the[PlaybackState.CustomAction](https://developer.android.com/reference/kotlin/android/media/session/PlaybackState.CustomAction). This value cannot be`null`. |
| `extras` | [Bundle](https://developer.android.com/reference/kotlin/android/os/Bundle.html#)?:Optional extras specified by the[MediaController](https://developer.android.com/reference/kotlin/android/media/session/MediaController). This value may be`null`.                                   |

### onFastForward

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

```
open fun onFastForward(): Unit
```

Override to handle requests to fast forward.  

### onMediaButtonEvent

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

```
open fun onMediaButtonEvent(mediaButtonIntent: Intent): Boolean
```

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](https://developer.android.com/reference/kotlin/android/content/Intent.html#ACTION_MEDIA_BUTTON:kotlin.String)with a KeyEvent in[Intent.EXTRA_KEY_EVENT](https://developer.android.com/reference/kotlin/android/content/Intent.html#EXTRA_KEY_EVENT:kotlin.String)

|                                                                                      Parameters                                                                                      ||
|---------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `mediaButtonIntent` | [Intent](https://developer.android.com/reference/kotlin/android/content/Intent.html#):an intent containing the KeyEvent as an extra This value cannot be`null`. |

|                                                               Return                                                                ||
|------------------------------------------------------------------------------------|-------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | True if the event was handled, false otherwise. |

### onPause

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

```
open fun onPause(): Unit
```

Override to handle requests to pause playback.  

### onPlay

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

```
open fun onPlay(): Unit
```

Override to handle requests to begin playback.  

### onPlayFromMediaId

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

```
open fun onPlayFromMediaId(
    mediaId: String!, 
    extras: Bundle!
): Unit
```

Override to handle requests to play a specific mediaId that was provided by your app's[MediaBrowserService](https://developer.android.com/reference/kotlin/android/service/media/MediaBrowserService.html#).  

### onPlayFromSearch

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

```
open fun onPlayFromSearch(
    query: String!, 
    extras: Bundle!
): Unit
```

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[API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun onPlayFromUri(
    uri: Uri!, 
    extras: Bundle!
): Unit
```

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

### onPrepare

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

```
open fun onPrepare(): Unit
```

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

### onPrepareFromMediaId

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

```
open fun onPrepareFromMediaId(
    mediaId: String!, 
    extras: Bundle!
): Unit
```

Override to handle requests to prepare for playing a specific mediaId that was provided by your app's[MediaBrowserService](https://developer.android.com/reference/kotlin/android/service/media/MediaBrowserService.html#). 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](https://developer.android.com/reference/kotlin/android/media/session/PlaybackState#STATE_PAUSED:kotlin.Int)after the preparation is done. The playback of the prepared content should start in the implementation of[onPlay](https://developer.android.com/reference/kotlin/android/media/session/MediaSession.Callback#onPlay()). Override[onPlayFromMediaId](https://developer.android.com/reference/kotlin/android/media/session/MediaSession.Callback#onPlayFromMediaId(kotlin.String,%20android.os.Bundle))to handle requests for starting playback without preparation.  

### onPrepareFromSearch

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

```
open fun onPrepareFromSearch(
    query: String!, 
    extras: Bundle!
): Unit
```

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

### onPrepareFromUri

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

```
open fun onPrepareFromUri(
    uri: Uri!, 
    extras: Bundle!
): Unit
```

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

### onRewind

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

```
open fun onRewind(): Unit
```

Override to handle requests to rewind.  

### onSeekTo

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

```
open fun onSeekTo(pos: Long): Unit
```

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

|                                                          Parameters                                                           ||
|-------|------------------------------------------------------------------------------------------------------------------------|
| `pos` | [Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html):New position to move to, in milliseconds. |

### onSetPlaybackSpeed

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

```
open fun onSetPlaybackSpeed(speed: Float): Unit
```

Override to handle the playback speed change. To update the new playback speed, create a new[PlaybackState](https://developer.android.com/reference/kotlin/android/media/session/PlaybackState)by using[android.media.session.PlaybackState.Builder#setState(int,long,float)](https://developer.android.com/reference/kotlin/android/media/session/PlaybackState.Builder#setState(kotlin.Int,%20kotlin.Long,%20kotlin.Float)), and set it with[setPlaybackState(android.media.session.PlaybackState)](https://developer.android.com/reference/kotlin/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.

|                                                 Parameters                                                 ||
|---------|---------------------------------------------------------------------------------------------------|
| `speed` | [Float](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html):the playback speed |

**See Also**

- [#setPlaybackState(PlaybackState)](https://developer.android.com/reference/kotlin/android/media/session/MediaSession#setPlaybackState(android.media.session.PlaybackState))
- [android.media.session.PlaybackState.Builder#setState(int, long, float)](https://developer.android.com/reference/kotlin/android/media/session/PlaybackState.Builder#setState(kotlin.Int,%20kotlin.Long,%20kotlin.Float))  

### onSetRating

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

```
open fun onSetRating(rating: Rating): Unit
```

Override to handle the item being rated.

|                                                        Parameters                                                         ||
|----------|-----------------------------------------------------------------------------------------------------------------|
| `rating` | [Rating](https://developer.android.com/reference/kotlin/android/media/Rating.html#):This value cannot be`null`. |

### onSkipToNext

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

```
open fun onSkipToNext(): Unit
```

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

### onSkipToPrevious

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

```
open fun onSkipToPrevious(): Unit
```

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

### onSkipToQueueItem

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

```
open fun onSkipToQueueItem(id: Long): Unit
```

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

### onStop

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

```
open fun onStop(): Unit
```

Override to handle requests to stop playback.