Implement Podcast in a Service with ExoPlayer and Hilt #1

Create the Player

Mickael Calatraba
Ideas by Idean
3 min readMar 22, 2021

--

Since 2018, Idean has been developing an application for Capgemini. This app, Oscar, features articles, offers, experts, and podcasts related to tech and Capgemini.

Oscar is an app UI/UX design based, you can download it on the store here:

First, we will talk about creating a simple podcast player using Google’s Exoplayer. And in a 2nd article, I invite you to talk about the injection of this podcast player using Hilt.

What is Hilt? Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project.

It is very easy to retrieve a Service from a Fragment or an Activity using ServiceConnection:

But now let’s imagine that we want to retrieve the service in a child's view of a cell for example.

One solution would be to create a Callback system that would control the Player and that could return the playback information (title and description of the podcast, playback time…) to our view.

Another solution would be to inject the player directly into the view using Hilt. And retrieve playback information, thanks to the LiveData. This is the solution that we will develop here. We will wrap the Player in a Manager that we will be able to inject wherever we want.

Add Dependencies

I advise you to read the documentation of LiveData, ExoPlayer, Hilt. You’ll also need Kotlin 1.4.0.

Open the build.gradle file for your project and add the following as shown below:

Open the build.gradle file for your app or module and add the following and the artifacts that you need as dependencies:

Note: Hilt uses Java 8 features. Enable Java 8 in your project

Creating AudioSevice

AudioService class will be our player, AudioSevice will contain the ExoPlayer player.

Note: ExoPlayer is a fairly complete player, I advise you to make your implementation of the player.

1. We create the Service’s functions, it’s a common Service implementation. Once the Service is created, we call the function to create the player. Then when the Service is destroyed, then, we release the player.

Now we can create the ExoPlayer and write the initialization function. The function createPlayer() is a simple example from the ExoPlayer documentation.

2. The function initialisePlayer() creates the player if the player is null. We clear the media items (in case the player already exists) and we load the new media item.

3. add the functions of controls, play, pause and stop. ControlDispatcher is responsible for dispatching commands to the player. Using a custom ControlDispatcher is useful if you want to implement notifications, by defining the same setControlDispatcher() method in your Service and your NotificationManager.

4. The ExoPlayer library offers a NotificationManager to easily manage notifications.

  1. We implement a MediaSessionCompat.

MediaSessionCompat allows a media app to expose its transport controls and playback information in a process to other processes including the Android framework and other apps. Common use cases are as follows.

2. We create a PlayerNotificationManager, we use createWithNotificationChannel() that allows us to create the manager with the compatibility for the different APIs.

3. We are going to set the MediaSessionCompat, the player, the dispatcher, and finally the icons.

Note: You can also personalize the notification such as adding color, different actions (back, next, fast forward, rewind …).

Conclusion

ExoPlayer is an open-source component, highly customizable and very complete. It can play audio as well as video.

In the next episode, thanks to Hilt, we will create a manager to inject the player.

--

--