With the arrival of media3 and for professional reasons, I have had to do a lot of research on the use of media3 for background audio playback using one of the different available services such as MediaSessionService, MediaLibraryService and MediaBrowserService. Also, I needed to display notifications on both the status bar and the lock screen.
During the time that I was documented, I realized that there were few clear examples of the use of these services. Including those available in Android's own repositories (https://github.com/android/uamp, https://github.com/androidx/media), they are all difficult to follow and understand if what you are looking for it is simplicity and only having the necessary features to play music in the background with notifications.
Goal
The purpose of creating this repository is to present a simpler approach, creating only the necessary classes to comply with the features that I will describe in the next point, but without neglecting a logical, meaningful and reactive structure.
I have not focused on UI, although I will update it to show the potential that a clean, simple service with features that could be assimilated to those offered by Spotify (with a bit of evolution) can have.
Features
These features are specific to the example, but I'm really going to put the emphasis on the service, the control of it, and the notifications, classes that I will detail below, which are found in the service module.
SimpleMediaService inherits from MediaSessionService and feeds on hilt-injected classes (see SimpleMediaModule for more details). The injected classes are as follows:
SimpleMediaNotificationManager has three main functions:
In this class is where we can create our custom notification with the controls, icons or images that we want.
SimpleMediaNotificationAdapter is used by SimpleMediaNotificationManager added to notification builder to get automaticatly metadata from the player and show album title, song title and image. All this data is added when MediaItem is created.
SimpleMediaServiceHandler is a bridge to be able to communicate the application with the service, because the only common dependency that is necessary to have is the one corresponding to the MediaSession.
Two sealed classes are used that encapsulate the actions and events necessary to coordinate the state of the playback:
Besides, it is in charge of arranging the necessary methods to add the MediaItem, be it one or a list.
A simple and clear playback service with notifications, with a reactive architecture. I will continue checking improvements, this is still an imperfect approach (nothing is perfect), improvable but that provides what is necessary without complications.