Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a media item? android

Tags:

android

i have read the official documentation but i can't understand what do do in this method:

@Override
public void onLoadChildren(final String parentMediaId,
final Result<List<MediaItem>> result) {

...
// Assume for example that the music catalog is already loaded/cached.

List<MediaItem> mediaItems = new ArrayList<>();

// Check if this is the root menu:
if (MY_MEDIA_ROOT_ID.equals(parentMediaId)) {

  // build the MediaItem objects for the top level,
  // and put them in the mediaItems list

  where is documented this part? how to build the media item?

} else {

  // examine the passed parentMediaId to see which submenu we're at,
  // and put the children of that menu in the mediaItems list
}
...
}

also if i want to play the music on the phone, how to "build the media item/s" based on the music of the phone?

like image 545
Brandon Avatar asked May 28 '17 01:05

Brandon


1 Answers

My response is a little late but I hope it'll help future doubts.

In your Service.java

case MEDIA_ID_ROOT:
    for (MediaMetadataCompat track : mMusicProvider.getAllMusics()) {

        String mediaId = track.getDescription().getMediaId();
        //Artist song
        String title = String.valueOf(track.getDescription().getTitle());
        //Artist name
        String subTitle = String.valueOf(track.getDescription().getSubtitle());
        //Artist album
        String descriptin = String.valueOf(track.getDescription().getDescription());
        //Song duration
        Long duration = track.getLong(MediaMetadataCompat.METADATA_KEY_DURATION);

        Bundle songDuration = new Bundle();
        songDuration.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration);

        MediaDescriptionCompat desc =
                new MediaDescriptionCompat.Builder()
                        .setMediaId(mediaId)
                        .setTitle(title)
                        .setSubtitle(subTitle)
                        .setDescription(descriptin)
                        .setExtras(songDuration)
                        .build();

        MediaBrowserCompat.MediaItem songList =
                new MediaBrowserCompat.MediaItem(desc,
                        MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);
        mediaItems.add(songList);
    }
    break;

mMusicProvider is the object of my model

in your provider.java

// Categorized caches for music track data:                                     
   private final LinkedHashMap<String, MediaMetadataCompat> mMusicListById;

public Iterable<MediaMetadataCompat> getAllMusics() {

    if (mCurrentState != State.INITIALIZED || mMusicListById.isEmpty()) {
        return Collections.emptyList();
    }
    return mMusicListById.values();
}

The MediaBrowserCompat.MediaItem basically can have MediaId, Title, Subtitle, Description, IconBitmap, IconUri, Bundle. In my case I needed the song's duration so thats why I added as an extra in a bundle. You can add more extras like composer, year, track number, etc.

If you don't need any extra information you can just call getdescription().

for (MediaMetadataCompat track : mMusicProvider.getAllMusics()) {
    MediaBrowserCompat.MediaItem bItem =
            new MediaBrowserCompat.MediaItem(track.getDescription(),
                    MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);

    mediaItems.add(bItem);
like image 110
Gerardo Mendez Dot Avatar answered Oct 28 '22 21:10

Gerardo Mendez Dot