I am a beginner in Android Development trying to create a media player with the implementation of MediaBrowser
and MediaSession
.
I am confused with the function of onGetRoot()
and onLoadChildren()
.
layman's term
what is the purpose of these two methods. onGetRoot()
? What I have in mind is, it is the root of the directory, am I correct?onLoadChildren()
? what I have in mind is the list of music in the root, am I correct?onLoadchildren()
return the actual playlist?Reference: https://developer.android.com/guide/topics/media-apps/audio-app/building-a-mediabrowserservice
In order to understand this, it is important to have a clear understanding of what a MediaItem
represents.
In the reference the following statement is provided "Your service is responsible for associating the ID with the appropriate menu node or content item."
You need to use the MediaItem class to define a hierarchy of items (PLAYABLE or BROWSEABLE). An example would be
root (Not a media item, but can be subscribed to return the highest level fo Media items) -> songs(browseable) albums (browseable) artists (browseable)
the albums MediaItem, if subscribed to, would return something like -> album1 (browseable) album2 (browseable)
and in your album1 you would have your playable MP3s e.g. -> song1.mp3 (playable) song2.mp3 (playable)
For all browseable MediaItems we can choose to "subscribe" to them to get all of the child nodes; which helps when we one to dynamically build our UI.
Relating this concept to the software architecture, you need to think about what is going to access onGetRoot
and onLoadChildren
. They are part of a MediaBrowserService, therefore the functions will be interacted by a MediaBrowser.
1) onGetRoot()
is called to gain the authority to access the Media that the MediaBrowserService
provides. It will return a "root ID" which can be subscribed to and return the MediaItems at the highest level of your hierarchy. I.e. in the above example subscribing to the root node would return Songs, Albums and Artists.
onLoadChildren()
is therefore called when we choose to subscribe to MediaItems to get their children nodes.
2) This does not relate to the root directory but rather a "root ID" used to subscribe to the top (root) level of your self defined hierarchy
3) OnLoadChildren
is called by the subscribe method of the MediaBrowser and will return all child MediaItems (browseable or playable).
4) OnLoadChildren
can return a playlist if you wish. To do so define a mediaItem "playlist name" which can be browseable and it will return the MediaItems in that playlist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With