我的 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。当我尝试向前和向后跳过时,队列中的顺序不再保持。我不知道是怎么回事
非常感谢任何帮助,很抱歉包含这么多代码