androidx / media

Jetpack Media3 support libraries for media use cases, including ExoPlayer, an extensible media player for Android
Apache License 2.0
1.34k stars 315 forks source link

How to update the metadata for a currentMediaItem with CastPlayer #1508

Open FilipKastrupGP opened 3 days ago

FilipKastrupGP commented 3 days ago

I am working with an audio streaming app. I have a ReplaceableForwardingPlayer like in the UAMP project. The forwardingplayer switches between an ExoPlayer and a CastPlayer.

I need to control and change the metadata for a mediaItem. With the Exoplayer I can just replaceMediaItem(...) and it updates the metadata seamlessly: With the CastPlayer this is not the case. It reloads the entire mediaItem.

Is there a way to update the metadata on a CastPlayer?

tonihei commented 2 days ago

This is not seamlessly supported on CastPlayer at the moment. I can mark it as a feature request but we won't get around to work on it soon.

If you want to add support, I think you can amend CastPlayer.replaceMediaItems to check if just the metadata changed, and if so, call a new method on CastTimelineTracker that updates the internal item data only (without touching the remote cast client at all). The remote cast player metadata won't be updated though in this scenario (that is the title etc you'd see on the cast device). There is a method like queueUpdateItems, but it's documented that it's not possible to update just the metadata once the item has been loaded.

FilipKastrupGP commented 2 days ago

Thanks for the quick reply.

For anyone else how has this problem,- I made it work by making a copy of the CastPlayer class and changing the empty method

@Override
public void onMetadataUpdated() {}

to

@Override
public void onMetadataUpdated() {
        if (remoteMediaClient != null) {
            currentTimeline = timelineTracker.getCastTimeline(remoteMediaClient);
        }
    }

This depend on the cast-recevier doing the metadata changes to the MediaQueueItem.

tonihei commented 6 hours ago

making a copy of the CastPlayer class

If you already figured out how to improve this, do you want to upload a PR instead to merge it into our codebase? If not, we can make the same change ourselves too.