Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customisation of updateMediaSessionMetadata #3497

Closed
thibseisel opened this issue Nov 24, 2017 · 6 comments
Closed

Allow customisation of updateMediaSessionMetadata #3497

thibseisel opened this issue Nov 24, 2017 · 6 comments
Assignees

Comments

@thibseisel
Copy link

When using the MediaSession extension for ExoPlayer, it is possible to specify whether or not metadata should be updated with the doMaintainMetadata constructor parameter. If true, MediaSessionConnector will internally call the private method updateMediaSessionMetadata to recreate a MediaMetadataCompat from the currently playing MediaDescriptionCompat.

I already have MediaMetadataCompat objects stored in memory cache, and I'd like to retrieve them from it instead of creating them again. One solution would be to subclass MediaSessionConnector to redefine setPlayer and attach another Player.Listener to call my own updateMediaSessionMetadata, but it's not very handy.

Is there any easier way ? If not, I suggest the following:

  1. Make updateMediaSessionMetadata protected, or
  2. Provide a MetadataProvider interface to customize MediaSessionConnector, along with a DefaultMetadataProvider to retain the current behavior.

Thanks in advance !

@ojw28
Copy link
Contributor

ojw28 commented Nov 27, 2017

@marcbaechinger - What do you think?

@ojw28
Copy link
Contributor

ojw28 commented Dec 4, 2017

What's your memory cache keyed by?

@thibseisel
Copy link
Author

To comply with the MediaSession API, my metadata and items are keyed by a media id, which could be any String.
Also, you may note that I use RxJava Observable (and in this particular case, Maybe) to abstract network/disk access, so retrieving metadata from my cache is asynchronous.

@marcbaechinger
Copy link
Contributor

marcbaechinger commented Jan 9, 2018

The MetadataProvider approach seems like worth looking into on our side. I'll take a look into this.

Option 1 for now (with QueueNavigator):

If you need a quick solution for now, I wonder if you are using a QueueNavigator already? If you do so you can set the metadata yourself (while having doMaintainMetadata=false), that should work like when it is set by the connector. In case you are using the TimelineQueueNavigator you wrap it with your own implementatin of QueueNavigator to intercept the call to onCurrentWindowIndexChanged and then delegate to the actual TimelineQueueNavigator:

public void onCurrentWindowIndexChanged(Player player) {
   timelineQueueNavigator.onCurrentWindowIndexChanged(player);
   mediaSession.setMetadata(getMetadataForWindowIndex(player.getCurrentWindowIndex()));
}

The method getMetadataForWindowIndex can then just return the metadata object you are already caching.

Option 2 for now
If not using a QueueNavigator you can do what you mentioned and register your own EventListener and maintain metadata yourself.

Let me know whether the quick options help for now. I'll look into the MetadataProvider approach asap.

@tsuharesu
Copy link

I was having the same problem because I didn't know about QueueNavigator and don't want to use it. What I did is to set a listener in my player using player?.addListener(this). It's basically the same that MediaSessionConnector does internally.

class ExoPlayerAdapter(private val mediaSessionCompat: MediaSessionCompat) : PlayerEventListener {

[...] 

private fun setupPlayer() {
    if (player == null) {
        player = ExoPlayerFactory.newSimpleInstance(context, DefaultTrackSelector())
    }

    MediaSessionConnector(mediaSessionCompat).setPlayer(player, null)
    player?.addListener(this)
}

override fun onTimelineChanged(timeline: Timeline?, manifest: Any?, reason: Int) {
    mediaSessionCompat.setMetadata(currentMedia)
}
}

@ojw28
Copy link
Contributor

ojw28 commented Jun 20, 2018

Support for this was added in 62ee13b.

@ojw28 ojw28 closed this as completed Jun 20, 2018
@google google locked and limited conversation to collaborators Nov 23, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants