Return type of onGetroot() method of MediaBrowserServiceCompat?

419 Views Asked by At

What is the first argument of BrowserRoot() which is a return type of onGetroot() in MediaBrowserServiceCompat.

Its first argument is rootId which is a string ,so do I pass the path of my album ? How I do get its value ? How do I return a content hierarchy that is not null ?

   @Override
   public BrowserRoot onGetRoot(String clientPackageName, int clientUid,
  Bundle rootHints) {


if (allowBrowsing(clientPackageName, clientUid)) {

    return new BrowserRoot(MY_MEDIA_ROOT_ID, null);  // what should I pass here ?
} else {

    return new BrowserRoot(MY_EMPTY_MEDIA_ROOT_ID, null);
}
}

Any help is appreciated.

1

There are 1 best solutions below

0
On

I use "root". Use any string, then in the load children, check if the parent id is "root" or an artist or album id, then pass the media items accordingly

edited to add some code from my onLoadChildren for clarity (note, the hashmaps can be lists, i use the hashmaps to easily set toolbar text using media ids mapped to artist/album names):

    ArrayList<MediaBrowserCompat.MediaItem> media_items = new ArrayList<>();
    HashMap<String, String> artist_ids = MusicLibrary.getListOfArtistIds(getApplicationContext());
    HashMap<String, String> album_ids = MusicLibrary.getListOfAlbumIds(getApplicationContext());

    // Check if this is the root menu:
    if ("root".equals(parentId)) {
        media_items = MusicLibrary.getArtists(context);

    } else if (artist_ids.containsKey(parentId)){
        // parent is artist
        media_items = MusicLibrary.getAlbums(context, parentId);

    } else if (album_ids.containsKey(parentId)){
        // parent is album
        media_items = MusicLibrary.getSongs(context, parentId);

    } 
    result.sendResult(media_items);

You can also change your root variable to playlist and check for that to get a list of playlists and use a list of playlist ids to get the members. Hope i helped

edited again just to put in my onGetRoot to show how simple mine is:

public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
    return new BrowserRoot(MusicLibrary.getRoot(), null);
}