MediaBrowserServiceCompat receiving empty bundle when item is clicked

731 Views Asked by At

I am trying to expose media items to other media apps that can browse my app's content through my MediaBrowserServiceCompat service. In my onLoadChildren method I am constructing MediaBrowserCompat.MediaItem with a MediaDescriptionCompat that includes a Bundle that has some extras that I need to play the item.

public class Service extends MediaBrowserServiceCompat {

...

    @Override
    public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {

        val bundle = Bundle().apply {
            putString("extra", "some value")
        }

        MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
                    .setMediaId(mediaId)
                    .setExtras(bundle)
                    .setTitle("title")
                    .setSubtitle("subtitle")
                    .setIconUri(uri)
                    .build();
        MediaBrowserCompat.MediaItem item = new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);

        val items = ArrayList<MediaBrowserCompat.MediaItem>()
        items.add(item)


        result.sendResult(items)
    }

So in the onPlayFromMediaId(String mediaId, Bundle extras) callback that I get when the user has clicked on the item, I am getting the right mediaId but the extras is an empty bundle.

    private class MediaSessionCallback extends MediaSessionCompat.Callback {
     ...
        @Override
        public void onPlayFromMediaId(String mediaId, Bundle extras) {
            super.onPlayFromMediaId(mediaId, extras);
            //here extras is empty
        }

I am sure that the MediaItem has the extras bundle when sent in the Result<List<MediaBrowserCompat.MediaItem>> result in onLoadChildren but I am not sure why it is being returned empty. What can cause such an issue?

Thank you!

2

There are 2 best solutions below

0
On

Have same issue and I can't found relation between MediaDescriptionCompat's extras and onPlayFromMediaId's extras. So "mediaId" - that is only information you got from MediaItem and you need put all your data for onPlayFromMediaId here.

1
On

I dont think you are actually getting the bundle. you set the parameter Bundle extra but I dont think there is actually anything in that bundle

usually how I have done it in the past after I create the bundle to retrieve it would do something like this

create a variable to store the received

val extra:String

then use that string variable to get the bundle you created

extra = bundle.getstring("extra")

which "extra" is matching your key for the bundle that you created up top which you pretty much have only your not actually getting the string from the bundle that the .getstring("extra") would get