MPMusicPlayerController队列问题

时间:2012-06-03 19:32:43

标签: ios mpmediaitemcollection mpmediaplayercontroller

我的MPMusicPlayerController存在一些问题。这是一个iPodMusicPlayer,而不是appmusicplayer。我的问题是我无法让队列以正确的顺序播放。让我进一步解释一下:

我使用iPod选择器选择媒体收藏,然后将其添加到音乐播放器的队列中。一旦我这样做,我在表格视图中显示集合中的项目。然后,用户可以“播放”此队列。但是,出于某种原因,当我尝试使用ipod控件导航到播放列表时,队列看起来像是错误的顺序。这是一些代码:

媒体选择器调用此方法:

-(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++;
}

}

很简单,这是直接来自苹果。底部的NSLog注销队列,看起来是正确的。

从这里,用户可以单击播放按钮来播放队列。我这样设置:

-(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);
}

}

在目标视图控制器中,setNowPlayingItem方法如下所示:

-(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]);
}

这会从tableview索引路径播放正确的歌曲,但由于某种原因,索引不再匹配其在表格中的位置。如果行索引0中的歌曲为ABC,则播放器播放ABC但会注销不再为0的indexOfNowPlayingItem。当我尝试向前和向后跳过时,不再保留队列中的顺序。我不知道最新情况

任何帮助都非常感谢,并且抱歉包含这么多代码

1 个答案:

答案 0 :(得分:5)

哇因某种原因哇哇哇哇哇哇哇哇哇哇哇哇哇。多数民众赞成在尴尬。这解决了一切:

    sharedMusicPlayer.musicPlayer.shuffleMode = MPMusicShuffleModeOff;