Media Controller in Notification Panel like Spotify, Youtube: Customizing Playback Using MediaSession

ibrahimcanerdogan
3 min readApr 13, 2024

--

Photo by Brandon Salabarría on Unsplash

MediaSession serves as a universal means of communication between the media player and external sources, enabling seamless interaction with the playback functionalities. In Android’s Media 3 architecture, the default player is ExoPlayer, which implements the Player interface. Connecting the MediaSession to the player allows the application to advertise media playback externally and receive playback commands from various sources.

Initializing MediaSession

To initialize a MediaSession in Kotlin, follow these steps:

  1. Initialize MediaSessionCompact: Create an instance of MediaSessionCompat, which provides compatibility across different versions of Android.
  2. Initialize MediaSessionConnector: Instantiate a MediaSessionConnector and pass the MediaSession to it. Then, associate the player with the MediaSessionConnector using the setPlayer() method.

Customizing Metadata and Playback Actions

MediaSession allows developers to customize metadata and playback actions, enhancing the user experience. Here’s how:

  • Metadata Handling: MediaSession manages metadata such as title, album, position, state, artwork, and playback actions. Developers can set metadata for each media item and define actions like play, pause, skip, and more.
  • SetQueueNavigator: For applications with multiple media items, implementing SetQueueNavigator enables navigation between media items. Developers can provide media descriptions for each item, including title, description, subtitle, and artwork.
  • setEnabledPlaybackActions: Developers can customize which playback actions the MediaSession should support using setEnabledPlaybackActions(). By passing specific actions like play, pause, skip, etc., developers can tailor the MediaSession to suit the application’s needs.

Handling Subtitles and Commands

MediaSession provides methods for handling subtitles and custom commands:

  • setCaptionCallback: Implementing setCaptionCallback allows developers to handle captioning-related events like onset captioning, checking if captions are available, and executing custom commands.
  • setCaptioningEnabled: Developers can implement setCaptioningEnabled to enable or disable captioning based on application requirements.
  • onCommand: This method enables developers to define custom functionality for handling commands received by the MediaSession, providing flexibility in responding to external commands.

Create Playback Example

In the world of Android multimedia development, leveraging the MediaSession class in Kotlin allows developers to customize playback, manage metadata, and handle external playback commands seamlessly. Let’s dive into how to harness the power of MediaSession with Kotlin code examples.

Initializing MediaSession

// Initialize MediaSessionCompat
val mediaSession = MediaSessionCompat(context, "MediaSession")

// Initialize MediaSessionConnector
val mediaSessionConnector = MediaSessionConnector(mediaSession)
mediaSessionConnector.setPlayer(player) // Associate player with MediaSession

Customizing Metadata and Playback Actions

// Set metadata for the current media item
mediaSession.setMetadata(
MediaMetadataCompat.Builder()
.setTitle("Song Title")
.setArtist("Artist Name")
.setAlbum("Album Name")
.build()
)

// Define supported playback actions
mediaSession.setPlaybackState(
PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_PLAY
or PlaybackStateCompat.ACTION_PAUSE
or PlaybackStateCompat.ACTION_SKIP_TO_NEXT
// Add more actions as needed
)
.build()
)

Handling Subtitles and Commands

// Handle subtitle-related events
mediaSession.setCaptionCallback(object : MediaSessionCompat.CaptionCallback() {
override fun onSetCaptioningEnabled(enabled: Boolean) {
// Handle captioning enabled/disabled
}

override fun onSetCaptioningEnabledChanged(enabled: Boolean) {
// Handle captioning enabled/disabled changed
}

override fun onCommand(command: String, extras: Bundle?, cb: ResultReceiver?) {
// Handle custom commands
}
})

Conclusion

Utilizing MediaSession in Kotlin-based Android applications empowers developers to customize playback, manage metadata, and handle external playback commands effectively. By leveraging MediaSession’s capabilities, developers can create immersive multimedia experiences that seamlessly integrate with various external sources like notifications and voice assistants. Through careful implementation and customization, Kotlin developers can elevate the user experience and deliver feature-rich multimedia applications on the Android platform.

İbrahim Can Erdoğan

LINKEDIN

YOUTUBE

UDEMY

GITHUB

--

--

ibrahimcanerdogan
ibrahimcanerdogan

Written by ibrahimcanerdogan

Hi, My name is Ibrahim, I am developing ebebek android app within Ebebek. I publish various articles in the field of programming and self-improvement.

No responses yet