MediaBrowserServiceCompat receiving empty bundle when item is clicked
Asked Answered
P

2

11

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!

Paramagnetism answered 22/7, 2019 at 13:20 Comment(6)
Please add the code, where you create and get the bundle.Sleety
@Sleety Thank you for your comment. Please check the updated question where I added more code.Paramagnetism
Can you add how you exactly receive the bundle and extract the result.Sleety
@Sleety if you simple check the bundle in onPlayFromMediaId callback, it is empty.Paramagnetism
What device and Android version are you testing on?Beam
I am seeing this problem on Android Automotive OS, but it does work on Android Auto...Forlini
T
0

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

Ticktack answered 31/7, 2019 at 19:46 Comment(1)
Thank you for your answer. Sorry but that isn't the case. The bundle does contain my extra before sending it as part of the result.Paramagnetism
G
0

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.

Glyceride answered 25/5, 2020 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.