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

Update shuffle order in unprepared ConcatenatingMediaSource. #4915

Closed
HardikChauhanSA opened this issue Oct 5, 2018 · 28 comments
Closed

Update shuffle order in unprepared ConcatenatingMediaSource. #4915

HardikChauhanSA opened this issue Oct 5, 2018 · 28 comments
Assignees

Comments

@HardikChauhanSA
Copy link

Hello,

I have been implementing a shuffle feature for medial player app.

Recently i have found an issue like,
I have list A of a song and added it to media source.

Now i have shuffled list B and updated my media source according to the shuffled list.

The issue is after allocating new media source from shuffled list and when i play next song,
the next song is playing as per the sequence of list A.

After than click on next again it plays the song as per sequence of list B.

It seems that ExoPlayer is storing the next media source and playing that even after i have updated the media sources.

Please check and let me know how can i address this issue.

Thanks.

@tonihei
Copy link
Collaborator

tonihei commented Oct 5, 2018

Could you please be a little more specific about what you are doing exactly? For example, provide a minimal example of what you do to make the problem occurs.

@tonihei tonihei self-assigned this Oct 5, 2018
@HardikChauhanSA
Copy link
Author

Yes sure i will explain.

I have a list of 5 songs, lets say List A - song1, song2, song3, song4, song5.
I have added all the songs streaming from server so I have created a data source with HlsMediaSource for all the songs. and my player is playing the song normally.

Now I have shuffled the list and assgined it to shuffled list lets say List S - - song5, song3, song4, song1, song2.
Now what i have done is i have again created HlsMediaSource for the shuffled list and added those to the media source in loop for each songs in list like,
cMediaSource?.addMediaSource(i, hlsMediaSource)

Now after updating the media source list,
when player plays next song either by completing the current song or pressing on next button,
the next song that play is song2 from the sequence of List A.

It should be the song3 from the sequence of List S

@tonihei
Copy link
Collaborator

tonihei commented Oct 5, 2018

Did you call player.prepare(listS) to actually start playing the new media source. Or how do you switch from one to the other?

Generally, you should try to use player.setShuffleModeEnabled to turn shuffling on and off, especially if you are just randomly shuffling anyway.

@HardikChauhanSA
Copy link
Author

At the time of shuffling, one song is already playing so i want to change the media source position while the current keeps playing and position of all the songs needs to be change.

Yes I have also used player.setShuffleModeEnabled = true but still this issue is coming.

@tonihei
Copy link
Collaborator

tonihei commented Oct 5, 2018

If you set player.setShuffleModeEnabled = true, the order definitely should be shuffled. Can you just do that without changing the playlist itself?

@HardikChauhanSA
Copy link
Author

Okay, But i also want to show the shuffled list in the recyclerview which shows the list of songs.

Will get the shuffled list of urls go change the order of list in recyclerview also?

@HardikChauhanSA
Copy link
Author

And also i dont think it will be possible cause we are trying to achieve the shuffle functionality same as Google Play app. To shuffle the song before playing the list so user can actually see the the shuffled list also.

@tonihei
Copy link
Collaborator

tonihei commented Oct 5, 2018

You can query the order by getting the timeline with player.getCurrentTimeline and then call timeline.getNextWindowIndex(index, repeatMode, shuffleMode) which tells you the next index depending on the repeat mode and shuffle mode.

@tonihei
Copy link
Collaborator

tonihei commented Oct 5, 2018

And if you want to shuffle yourself in advance and specifically set the shuffle order (instead of using a random one), you can use concatenatingMediaSource.setShuffleOrder. Would that solve your issue?

@HardikChauhanSA
Copy link
Author

Okay. Let me try that, I will update here.

@HardikChauhanSA
Copy link
Author

HardikChauhanSA commented Oct 5, 2018

Hi,
I cant seem to find concatenatingMediaSource.setShuffleOrder this method.
I have imported class from here,
import com.google.android.exoplayer2.source.ConcatenatingMediaSource

There is not such method in class ConcatenatingMediaSource.java

@tonihei
Copy link
Collaborator

tonihei commented Oct 5, 2018

It was added in 2.9.0. So maybe you need to update.

Looking more closely, it may also be quite difficult to provide the initial order because the ShuffleOrder class wasn't written with that use in mind. To simplify this, we should probably do the following:

  1. Allow updating the shuffle order even if the media is not prepared yet (we currently force it to be empty).
  2. Provide an constructor for DefaultShuffleOrder with an initial user-provider order.

If we implement step 1 at least, you can do the following:
a. Create a DefaultShuffleOrder of the length of your playlist.
b. Use the shuffle order to display the shuffled list even though you don't have timeline yet.
c. When creating the ConcatenatingMediaSource, you provide this shuffle order as a parameter to the
constructor, or set it later with setShuffleOrder.

@HardikChauhanSA
Copy link
Author

Though I have also found one more issue.

When i add media source at same position using,
cMediaSource?.addMediaSource(i, hlsMediaSource)

And i already has one media source, this is keep adding the new media sources and the size of media sources gets increased.

I have 10 song in list and i shuffled and re-shuffled it 10 times so after that my media sources size reaches to 100+.

@tonihei
Copy link
Collaborator

tonihei commented Oct 5, 2018 via email

@HardikChauhanSA
Copy link
Author

okay. Is there any way to just replace the media source at specific index?

@tonihei
Copy link
Collaborator

tonihei commented Oct 5, 2018 via email

@HardikChauhanSA
Copy link
Author

Hello,

I have tried with the above solution, I am able to get some things fixed but found one more thing,
player?.currentWindowIndex!! exoplayer always returns 0.

How can i get the index of currently playing media source?

@tonihei
Copy link
Collaborator

tonihei commented Oct 6, 2018 via email

ojw28 pushed a commit that referenced this issue Nov 14, 2018
…orders

Issue: #4915

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=221075615
@ojw28
Copy link
Contributor

ojw28 commented Nov 23, 2018

What's the status of this issue?

@tonihei
Copy link
Collaborator

tonihei commented Nov 26, 2018

The commit above addressed point 2, but point 1 still needs to be done.

@kraizman
Copy link

Hello, I have a question related to this topic. I have implemented ExoPlayer with ConcatenatingMediaSource and DefaultShuffleOrder. I also player.shuffleModeEnabled = true.
What is the correct way to get window index until Player.STATE_READY. For some reason, I always get 0 when checking player.currentWindowIndex in buffering state. Issue related only to the first preparation. When state change to Player.STATE_READY index returned correctly.

@tonihei
Copy link
Collaborator

tonihei commented Nov 27, 2018

@kraizman The window index is still 0 while the timeline is still Timeline.EMPTY. It should be available with the first onTimelineChanged event. This is also similar to the comment I made here about the tag of the current window. I've opened #5155 to track this, but it's probably low priority for now.

@kraizman
Copy link

For player app this is crushual as you need to display which song is buffering. In case source is from internet it may take significant time to get it ready. So there will be no way to show what is going on to user.

@tonihei
Copy link
Collaborator

tonihei commented Nov 27, 2018

@kraizman As said above, you don't need to wait until the state is READY. The timeline (and the shuffled order) is available as soon as the first onTimelineChanged event arrives. This should happen within milliseconds after calling prepare.

@kraizman
Copy link

Oh, I didn't understand it this way. I will check this. Thanks for clarifications

@HardikChauhanSA

This comment has been minimized.

@tonihei

This comment has been minimized.

ojw28 pushed a commit that referenced this issue Nov 28, 2018
…orders

Issue: #4915

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=221075615
@tonihei tonihei changed the title Shuffling songs with new list issue Update shuffle order in unprepared ConcatenatingMediaSource. Feb 8, 2019
@tonihei tonihei assigned marcbaechinger and unassigned tonihei Jul 9, 2019
@tonihei
Copy link
Collaborator

tonihei commented Sep 19, 2019

setShuffleOrder is now part of player API and can be called at any time.

@tonihei tonihei closed this as completed Sep 19, 2019
@google google locked and limited conversation to collaborators Nov 19, 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

5 participants