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

Reset currentWindowIndex when changing MediaSource position #4791

Closed
gbirk opened this issue Sep 7, 2018 · 1 comment
Closed

Reset currentWindowIndex when changing MediaSource position #4791

gbirk opened this issue Sep 7, 2018 · 1 comment

Comments

@gbirk
Copy link

gbirk commented Sep 7, 2018

I'm working on my own shuffle implementation for a music player app. When the user press shuffle, I need to put the currently playing track at the top of the queue (first position). I could not find any solution for this using the ExoPlayer shuffle mode, so I decided to create my own solution by moving the MediaSource objects on the playlist.

Right now I'm saving a reference of ConcatenatingMediaSource as an attribute and using moveMediaSource to change the positions. I'm not calling prepare or seek before changing the positions.

Let's say that the currently playing index is 4. Then the user hits shuffle and I move the playing item to position 0 by using moveMediaSource. If the user hits next, I would expect the next index to be 1, because I moved the playing item to position 0. But what I'm seeing is that the next index is being reported as 5. Sometimes even another random index.

How I'm suppose to achieve this behavior? There is a way to reset the currently playing index (getCurrentWindowIndex())? Or should I implement this in a different way?

@tonihei tonihei self-assigned this Sep 10, 2018
@tonihei
Copy link
Collaborator

tonihei commented Sep 10, 2018

Implementing your own shuffle order should ideally happen by providing your own ShuffleOrder implementation to the ConcatenatingMediaSource. Have a look at our default to see how this class can be implemented. However, there is one problem for your case: the shuffle order is supposed to be immutable and you can't just change the order depending on when the shuffle mode is enabled (this is needed for thread safety). Ideally, we should add a method to ConcatanatingMediaSource which lets you update the ShuffleOrder. I'll mark this issue as an enhancement to track this.

After we added this method, you should be able to do something like

concatenatingMediaSource.setShuffleOrder(
    new CustomShuffleOrderStartingAtIndex(player.getCurrentWindowIndex()), 
    () -> player.setShuffleModeEnabled(true));

whenever a user turns on the shuffle mode in your UI.

Independent of that, you can of course move around the media sources yourself to implement shuffling. Note that this doesn't keep the original order to easily toggle between shuffle mode on and off. And it also doesn't support inserting / deleting element elements from both the original and shuffled order natively. And finally, you will get onTimelineChanged notifications for each single move operation, which may be annoying and is likely causing unnecessary UI update operations.

When calling concatenatingMediaSource.moveMediaSource(...), you send a command to the player to perform the update. Note that is an asynchronous process and you won't see the update reflected in the player state immediately. That's why player.getCurrentWindowIndex() is still 4 and player.getNextWindowIndex() is still 5 immediately after calling the move method. There are two ways to wait for the update to happen:

  1. Use the optional last parameter of the moveMediaSource method to do something after the operation completed. E.g. concatenatingMediaSource.moveMediaSource(4, 0, () -> Log.d(TAG, player.getCurrentWindowIndex()) will log "0".
  2. Manually wait for eventListener.onTimelineChanged event which will be issued after the player handled the move operation.

ojw28 pushed a commit that referenced this issue Sep 12, 2018
This allows to update the shuffle order after the ConcatenatingMediaSource
has been created. ShuffleOrder objects should be immutable to ensure thread
safety and thus there is no way to do this currently.

Issue:#4791

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=212443146
@ojw28 ojw28 closed this as completed Sep 12, 2018
@google google locked and limited conversation to collaborators Jan 31, 2019
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

3 participants