Implement Podcast in a Service with ExoPlayer and Hilt #2

Inject 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:

In the 1st article, we learned how to create a simple player. Our player is ready, now, we are going to create a manager that will allow us to inject the player where we need it thanks to Hilt.

In order to retrieve it, we need a Context. Normally we can do this easily in an activity or a fragment.

We are going to create the AudioManager class that we can easily inject into our views, cells, fragments, or any other component using Hilt.

1. let’s create AudioManager. AudioManger is annotated with Singleton. Using this annotation will allow us to inject it later in AndroidEntryPoint components.

2. We need to write a getter for our AudioService. We are going to create a function that returns the service only if it’s bounded.

3. Start the service when the manager is created, we will do it in the init. Another possibility could be that AudioManager extends from LifecycleObserver and starts the service in the onCreate function.

AudioManager Injection

1. We can easily retrieve AudioManager from an Activity or a Fragment using the Hilt injection.

2. If we need to inject the AudioManager and use AudioService in a view that is not an AndroidEntryPoint (a root view for example). For that, we should use HiltEntryPoint.

3. We’re almost done! Now we can easily retrieve our AudioManager from anywhere in the app as long as we have a context! To do this, simply retrieve our EntryPoint.

private val audioManager: AudioManager by lazy {
EntryPointAccessors.fromApplication (context,
PodcastEntryPoint::class.java).audioManager()
}

Voilà!

Bonus View and LiveData

We can also add a listener to ExoPlayer to listen when a podcast is updated and then to notify the view.

To use LiveData in your app, add the new dependency to your build.gradle file:

implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"

Now we will add a LiveData in our AudioService class. Our components will be able to subscribe to currentTrack value to be notified when a new Audio is loaded.

private val _currentTrack = MutableLiveData <Audio> ()
val currentTrack: LiveData <Audio> = _currentTrack

Finally, we can postValue post when a new Audio is loaded, for example when the Player reads a playlist and a track reaches the end using an EventListener from ExoPlayer library.

Now you can observe the currentTrack from every LifecycleOwner:

Conclusion

To conclude, injecting a service is possible but it is expensive if Dagger or Hilt is not implemented yet. However, Hilt brings a lot of very interesting features.

Be careful, Hilt is still in Beta version. Even if there is no major error, it must be taken into account if you want to implement it in your project.

So now we have it! I hope you enjoyed the post. If you have any queries related to this post let me know in the comment section and feel free to click on the clap button!

Keep learning!

--

--