I’m implementing a music player into my app and when a user selects a playlist, they’ll have control over shuffle and repeat modes.
Initially, individual MPMediaItems of the playlist are stored in an NSArray (which is then handled by a function to play the music). When the user toggles the shuffle mode to ON, the array is copied to an NSMutableArray and the objects are shuffled (with the currently playing song at index 0). I’m good up to this point; the issue arises when the user turns the shuffle mode OFF.
Just like in the iPod music app, when the user turns the shuffle mode OFF, the original playlist is restored, with the location of the currently playing song in the original array.
Basically I want this to happen:
- User selects track 5 of 12 in the playlist: Now playing Track 5 of
12 - User turns shuffle mode ON: Now playing Track 1 of 12
- User plays presses Next song: Now playing Track 2 of 12
- User turns shuffle mode OFF: Now playing Track 7 of 12 (where track
7 is the location of that song in the original playlist, which was
track 2 in the shuffled playlist).
I hope I’ve made this clear enough. I’d very much appreciate some guidance on how I should go about doing this in the most efficient way.
Thanks!
Add an orderValue for object which you put in NSArray which will be equal to object index. So you can shuffle an array and restore original order according to this value.