Search code examples
iosmpmusicplayercontroller

When populating a MPMusicPlayerController with an MPMediaPlaylist, how to advance to next item, and begin playback there?


Here's what I'm doing:

self.iPodController = [MPMusicPlayerController applicationMusicPlayer];
MPMediaPlaylist* playlist = [self lookupSavedPlaylist];
[self.iPodController setQueueWithItemCollection:playlist];

[self.iPodController skipToNextItem];
[self.iPodController play];

This results in the first song playing, not the second. This kind of makes sense, but it's annoying and I'm hoping there's a work-around.


Solution

  • Well, it turns out that if you explicitly set the nowPlayingItem, you won't have this issue. Here's the modified code:

    self.iPodController = [MPMusicPlayerController applicationMusicPlayer];
    MPMediaPlaylist* playlist = [self lookupSavedPlaylist];
    [self.iPodController setQueueWithItemCollection:playlist];
    self.iPodController.nowPlayingItem = [playlist.items objectAtIndex:0]; // explicitly set to track 1 to start
    
    [self.iPodController skipToNextItem]; // will now skip to track 2!
    [self.iPodController play]; // will now play track 2