I want to define a MediaLibraryService in such a way that Android Auto, Exoplayer, AIMP For Android and other apps can access and play the media my service manages.
I don't see a need for a UI/Activity. I just want to define a foreground service, and have the various front-ends find it.
I am trying to figure out if that is a reasonable approach. Can I just do that, or do I need to make a full app, with an activity with a MediaController/MediaBrowser in it too?
No, Media3 provides you with all the tools and APIs necessary to make the service its own standalone music player, so that controllers like Android Auto can access your music and play it, without the actual need for activities and other things. This means that you store your playlists on the service-side, you do everything on the service-side, and if you're willing to make a music player app, the activity can grab information from the service using APIs that are available by Media3 such as
getChildren...etcThe powerful thing about the library is that you can
sendCustomCommandwhich contain bundles of pretty much everything you need, this would prove helpful when you want to communicate between an activity and the service, but it's completely unnecessary when the app is being navigated via a controller like Android Auto and not the actual mobile app. A song is defined by its MediaId, so I believe that's all you need to make a song playable, also, services provide you with anapplicationContextso you can query MediaStore all you want, and use Datastore or SharedPreferences all you want as well.Here's an example MusicLibraryService which you can reference from (this is a bit of an advanced case where I am using two separate playlists (a tracklist and an actual playlist, both of which can be queued separately depending on which one the user picks from), also, it's a MedialibraryService so the hierarchy is very important. The very top-level parent root item is invisible, but it's extremely capital to the functionality of the service, because the whole hierarchy propagates from it, you can include mediaitems right below it if you're having one huge playlist, but the more levels of hierarchy you have the more complicated it gets. I suggest you take it step by step and get yourself familiarzed with MediaLibraryService using one root item and have children below directly first:
Please note that some functions are not defined in the reference above, that's because they're not directly relevant to our topic, for example the
scanMusiccall, it's just a function where I query MediaStore and return a lambda with a list of queried mediaitems... So do focus on the actual media3 APIs and how everything is put together