Backing the ExoPlayer playlist with another datatype
See original GitHub issueMy app does not leverage the typical ExoPlayer media datatype, MediaItem
, as it tends to be too restrictive for my use. Instead, I have my own datatype (Song
) that can be transformed into a MediaItem
for use in ExoPlayer. By extension, I have my own data structure for the queue (or “playlist”) in my app that is also composed of Song
instances.
I’ve been trying to leverage the ExoPlayer playlist system for some time so I could implement Gapless Playback in my app, however, the major issue stems from the way I must convert between Song
and MediaItem
:
- I can’t just map the
Song
queue to a list of aMediaItem
instances and then callsetMediaItems
, as such could occur during playback and cause a variety of issues. - The more “correct” queue methods (ex.
addMediaItem
orShuffleOrder
) also do not suffice, as trying to keep two mirrors of the queue in different datatypes, making sure that edits in one also effect the other, is more or less asking for bugs to appear.
Thus, is there a way to extend ExoPlayer so that the playlist is backed with my own datatype, Song
? That way, I could keep one copy of the queue and simply use the correct methods to notify ExoPlayer when it changes. ExoPlayer should still try to attempt gapless playback, even with this custom playlist.
I think Timeline
is the API I should be using for this, but I have no clue if that’s the case or how I could use it to do what I want. Any help on this would be greatly appreciated. Thank you.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (6 by maintainers)
For the fun of it(!), I’m almost certain you don’t have to call
indexOf()
in the for-loop, which would cause the quadratic complexity. You could store 2 maps that map item-to-position (or position-to-item) for before and after the shuffle (needs two iterations), and then find for each item find its previous and new position with look-ups on the maps. This obviously comes with the cost of extra space for the maps. Overall, the playlist size should be so small than it doesn’t really make a difference to the user.Okay, turns out that the idea I proposed doesn’t really work @christosts.
I made this implementation that sort of does what I want:
But there is one crucial flaw: There can be multiple instances of the same song in a queue. For example,
MediaItem
3 in the player could correspond toSong
4 and 5, and the shuffle order has no way to find which index the player is truly at based on the raw player index. The only way I could fix this would be:ShuffleOrder
should be stateless, so I can’t do that.Care to elaborate on how I could leverage
moveMediaItems
?