Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of onGetRoot() and onLoadChildren() in MediaBrowserServiceCompat?

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().

  1. My first question is in layman's term what is the purpose of these two methods.
  2. What is the root they are pertaining in onGetRoot()? What I have in mind is, it is the root of the directory, am I correct?
  3. What is the children pertaining in onLoadChildren()? what I have in mind is the list of music in the root, am I correct?
  4. Will this onLoadchildren() return the actual playlist?
like image 913
Player1 Avatar asked Mar 28 '18 23:03

Player1


1 Answers

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.

like image 177
goldy1992 Avatar answered Oct 02 '22 18:10

goldy1992