Search code examples
mpmusicplayercontrollermpmediaitemmpmediaitemcollection

MPMusicPlayerController setQueue and noPlayingItem defaults to index 0


When I try to change the queue of a MPMusicPlayerController and set the nowPlayingItem the player always goes to the first item in the queue. The documentation says if I want to set the current position in a queue I should set the nowPlayingItem to an MPMediaItem in the queue. Am I doing something wrong?

MPMusicPlayerController *player = [MPMusicPlayerController systemMusicPlayer];
    [player setRepeatMode:MPMusicRepeatModeNone];
    [player setShuffleMode:MPMusicShuffleModeOff];
    [player pause];
    MPMediaItemCollection *collection = [MPMediaItemCollection collectionWithItems:[[HMFAudioManager sharedManager] playlist]];
    [player setQueueWithItemCollection:collection];
    MPMediaItem *selectedItem = collection.items[indexPath.row];
    NSLog(@"selected item                          - %@",selectedItem.title);
    NSLog(@"manager's current now playing item     - %@",[[HMFAudioManager sharedManager] nowPlayingItem].title);
    [[HMFAudioManager sharedManager] setNowPlayingItem:collection.items[indexPath.row]];
    NSLog(@"manager's new now playing item         - %@",[[HMFAudioManager sharedManager] nowPlayingItem].title);
    player.nowPlayingItem = collection.items[indexPath.row];
    player.currentPlaybackTime = [collection.items[indexPath.row] bookmarkTime];
    [player play];
    NSLog(@"player's now playing                   - %@",player.nowPlayingItem.title);
    NSLog(@"manager's new now playing item         - %@",selectedItem.title);
    [self dismissViewControllerAnimated:YES completion:nil];

Log -

2015-08-07 16:51:42.003 Undulib[1986:487940] selected item                          - Star Wars: The Empire Strikes Back
2015-08-07 16:51:42.004 Undulib[1986:487940] manager's current now playing item     - Star Wars: A New Hope
2015-08-07 16:51:42.005 Undulib[1986:487940] manager's new now playing item         - Star Wars: The Empire Strikes Back
2015-08-07 16:51:42.238 Undulib[1986:487940] player's now playing                   - Star Wars: A New Hope
2015-08-07 16:51:42.238 Undulib[1986:487940] manager's new now playing item         - Star Wars: The Empire Strikes Back

Solution

  • Stopping the player instead of pausing solved the issue.