Pergunta

I'm having some issues with my MPMusicPlayerController. It's an an iPodMusicPlayer, not an appmusicplayer. My problem is that I can not get the queue to play in the correct order. Let me explain further:

I use an iPod picker to select a media collection, which I then add to the music player's queue. Once i do this, I display the items in the collection on a table view. A user can then "play" this queue. However, for some reason when I try to navigate thru the playlist with the ipod controls, the queue seems like its in the incorrect order. Here is some code:

The media picker calls this method:

-(void)updatePlayerQueueWithMediaCollection:(MPMediaItemCollection *)collection {

if (collection) {

    // If there's no playback queue yet...

    if (self.userMediaItemCollection == nil) {
        [self setUserMediaItemCollection: collection];
        [self.musicPlayer setQueueWithItemCollection:self.userMediaItemCollection];
    } else {
        BOOL wasPlaying = NO;
        if (self.musicPlayer.playbackState == MPMusicPlaybackStatePlaying) {
            wasPlaying = YES;
        }

        // Save the now-playing item and its current playback time.

        MPMediaItem *nowPlayingItem        = self.musicPlayer.nowPlayingItem;
        NSTimeInterval currentPlaybackTime = self.musicPlayer.currentPlaybackTime;

        // Combine the previously-existing media item collection with the new one

        NSMutableArray *combinedMediaItems =
        [[self.userMediaItemCollection items] mutableCopy];
        NSArray *newMediaItems = [collection items];
        [combinedMediaItems addObjectsFromArray: newMediaItems];

        [self setUserMediaItemCollection:[MPMediaItemCollection collectionWithItems:(NSArray *) combinedMediaItems]];

        [self.musicPlayer setQueueWithItemCollection: self.userMediaItemCollection];

        // Restore the now-playing item and its current playback time.

        self.musicPlayer.nowPlayingItem      = nowPlayingItem;
        self.musicPlayer.currentPlaybackTime = currentPlaybackTime;

        if (wasPlaying) {
            [self.musicPlayer play];
        }
    }
}
int i = 0; 
for (MPMediaItem *item in [self.userMediaItemCollection items]){
    NSLog(@"playback queue item %i has title %@",i, [item valueForKey:MPMediaItemPropertyTitle] );
    i++;
}

}

simple enough, this is taken directly from apple. That NSLog at the bottom logs out the queue and it appears correct.

From here a user can click a play button to play the queue. I set that up like this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];

if ([segue.destinationViewController respondsToSelector:@selector(setNowPlayingItem:)]) {
    NSArray *items = [[MusicPlayerManager sharedMusicPlayer].userMediaItemCollection items];
    [segue.destinationViewController performSelector:@selector(setNowPlayingItem:) withObject:[items objectAtIndex:indexPath.row]];
}

if ([segue.destinationViewController respondsToSelector:@selector(setTrackList:)]) {
    [segue.destinationViewController performSelector:@selector(setTrackList:) withObject:[MusicPlayerManager sharedMusicPlayer].userMediaItemCollection];
    NSLog(@"tracklist from tltbvc looks like %@", [MusicPlayerManager sharedMusicPlayer].userMediaItemCollection);
}

}

In the destination view controller, the setNowPlayingItem method looks like this:

-(void) setNowPlayingItem:(MPMediaItem *)nowPlayingItem {
    _nowPlayingItem = nowPlayingItem;
    [MusicPlayerManager sharedMusicPlayer].musicPlayer.nowPlayingItem = nowPlayingItem;
    [[MusicPlayerManager sharedMusicPlayer].musicPlayer play];
    NSLog(@"index of now playing item %i", [[MusicPlayerManager sharedMusicPlayer].musicPlayer indexOfNowPlayingItem]);
}

This plays the correct song from the tableview indexpath, but for some reason the index no longer matches its location in the table. If the song in row index 0 is ABC, the player plays ABC but logs out an indexOfNowPlayingItem that is no longer 0. When i try to skip forward and back, the order from the queue is no longer maintained. I have no idea whats going on

Any help greatly appreciated, and sorry to include so much code

Foi útil?

Solução

wow for some reason it had shuffle set to on. Thats embarassing. this fixed everything:

    sharedMusicPlayer.musicPlayer.shuffleMode = MPMusicShuffleModeOff;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top