Chapters

Hide chapters

Android Apprentice

Fourth Edition · Android 11 · Kotlin 1.4 · Android Studio 4.1

Section II: Building a List App

Section 2: 7 chapters
Show chapters Hide chapters

Section III: Creating Map-Based Apps

Section 3: 7 chapters
Show chapters Hide chapters

26. Podcast Playback
Written by Fuad Kamal

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

At this point, you’ve built a decent podcast management app, but there’s no way to listen to content. Time to fix that!

In this chapter, you’ll learn how to build a media player that plays audio and video podcasts, and integrate it into the Android ecosystem. Building a good media player takes some work. The payoff, however, is an app that works well in the foreground and also while the user performs other tasks on their device.

Getting started

If you’re following along with your own project, the starter project for this chapter includes an additional icon that you’ll need to complete the section. Open your project then copy the following resources from the provided starter project into yours:

  • src/main/res/drawable/ic_pause_white.png
  • src/main/res/drawable/ic_play_arrow_white.png
  • src/main/res/drawable/ic_episode_icon.png

Also, copy all of the files from the drawable folders, including folders with the -hdpi, -mdpi, -xhdpi, -xxhdpi and -xxxhdpi extensions.

If you don’t have your own project, don’t worry. Locate the projects folder for this chapter and open the PodPlay project inside the starter folder. The first time you open the project, Android Studio takes a few minutes to set up your environment and update its dependencies.

Media player basics

Note: The Media classes mentioned here have backward compatible versions that you’ll use when building the app. The Compat part of the class names have been left out for brevity (i.e., MediaPlayer = MediaPlayerCompat).

MediaPlayer

The built-in core tool that Android provides for media playback is MediaPlayer. This class handles both audio and video and can play content stored locally or streamed from an external URL. MediaPlayer has standard calls for loading media, starting playback, pausing playback, and seeking to a playback position.

MediaSession

Android provides another class named MediaSession that is designed to work with any media player, either the built-in MediaPlayer or one of your choosing. The MediaSession provides callbacks for onPlay(), onPause() and onStop() that you’ll use to create and control the media player.

MediaController

The MediaController is used directly by the user interface, which in turn, communicates with a MediaSession, isolating your UI code from the MediaSession. MediaController provides callbacks for major MediaSession events, which you can use to update your UI.

MediaBrowserService

For a better listening experience, you’ll let the podcast play in the background and give the user playback controls from outside of PodPlay. There are many ways a user may want to control audio from outside an app, and MediaBrowserService makes it possible.

MediaBrowser

To control the MediaBrowserService service, you’ll use MediaBrowser. This class connects to the MediaBrowserService service and provides it with a MediaController. Your UI will then use a MediaController to control the playback operations. Other apps can also use their own MediaBrowser to connect to the PodPlay MediaBrowserService.

Building the MediaBrowserService

MediaBrowserService is where all of the hard work of managing the podcast playback happens. You’ll start with a basic implementation that’s just enough to get a podcast playing and then expand the service later.

Create a MediaSession

At the heart of MediaBrowserService is MediaSession. As PodPlay and other apps interact through MediaBrowserService, MediaSession responds. But before it can, you need to create the MediaSession when the service first starts.

Connecting the MediaBrowser

There’s no podcast episode player UI in the app yet — which is where you’d typically create the MediaBrowser and connect it to the PodplayMediaService — so for now, you’ll add the MediaBrowser code to the podcast details screen instead.

Create callbacks

Before adding the MediaBrowser object, you need to define the callback classes.

Initialize the MediaBrowser

With the two callback classes created, you’re ready to create the media browser object. This asynchronously kicks off the connection to the browser service.

Connect the MediaBrowser

The media browser should be connected when the Activity or Fragment is started. Add the following method:

Unregister the controller

The media controller callbacks should be unregistered when the Activity or Fragment is stopped.

Handle media browsing

To properly handle media browsing, there’s one part of PodplayMediaService you need to complete.

Sending playback commands

With the successful connection in place, it’s time to test out the ability to send play commands and recognize state changes.

Updating media session state

Finally, it’s time to update the media service to set the playback states based on the incoming play commands.

Using MediaPlayer

Now that you have the MediaBrowser talking to the MediaBrowserService, it’s time to hear some audio. However, it’s up to you to provide the media playback capabilities in response to the media session events. You can use any means you want to playback the media, including third-party media players.

Audio Focus

Android uses the concept of audio focus to make sure that apps cooperate with each other and the system, ensuring that audio is played at the appropriate times. Only one app has audio focus at a time, although more than one app can play audio at the same time.

Foreground service

To keep the audio playing, you need to set PodplayMediaService as a foreground service. Any foreground service requires that it display a visible notification to the user. This is done at the time the podcast begins playing.

Media notification

To display the notification, you’ll build it using the same APIs as you did the new episode notification in the last chapter, but this time the expanded notification will display playback controls. You’ll use a special style named MediaStyle on the notification that automatically displays and handles the playback controls.

Media metadata

There’s still one missing part. You haven’t told the media service about the details of the podcast episode yet. You need to pass in the additional episode details and add them to the media session metadata.

Final pieces

One more item is required to stop the playback if the user dismisses the app from the recent applications list. Add the following method to PodplayMediaService:

Lwoy giwd go gaccq, Ordjear Eneu (0), Ohprieb Sadkkreptuj (7), Imknaiz Hexzuqob (6)
Floy lusk ra riqdh, Utswaow Odai (3), Ajkxean Miwtvgirsud (1), Ockvial Hilhabob (0)

Unmhoah Tibbdyuxxeh Watpmjyoegr
Uvbnaok Qusnpjegyub Fupjmlniujs

Orchaaf Coam
Ovwtuet Juoq

Key Points

You don’t need to reinvent the wheel if you want to add media support to your app. Luckily the Android framework provides you the necessary pieces to build your functionality. In this chapter you learned:

Where to go from here?

That was a lot of work to get playback working, but it’s worth it to have podcasts that play correctly in the background. Take a break and find a relaxing podcast to listen to while you get ready for the next chapter.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now