[英]Next Song in MediaItemCollection?
如何找到MPMediaItem
集合中要播放的下一个项目的名称? 我希望将其存储为MPMediaItem
。
我有一个songsViewController
,其中包含所有歌曲的tableView
。 这是我的didSelectRowAtIndexPath
代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MPMediaItem *selectedItem;
selectedItem = [[songs objectAtIndex:indexPath.row] representativeItem];
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
[musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:[songsQuery items]]];
[musicPlayer setNowPlayingItem:selectedItem];
nowPlayingViewController *nowPlaying = [[rightSideMenuViewController alloc]
initWithNibName:@"nowPlayingViewController" bundle:nil];
[self presentViewController:nowPlaying animated:YES completion:nil];
[musicPlayer play];
}
我的nowPlayingViewController
上有一个UILabel
,当用户选择一首歌曲时弹出。 我想在MediaItemCollection
/ queue中将下一个项目的标题存储在该UILabel
-因此它是下一首歌曲的预览。
任何帮助将不胜感激,谢谢! :)
保留您的列表(因为您无法在musicplayer队列中访问)。
@property (nonatomic, strong) NSArray *playlist;
当您这样做时:
[musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:[songsQuery items]]];
加:
playlist = [songsQuery items];
要获取您的上一个/下一个:
-(MPMediaItem *)nextItem
{
int currentIndex = [musicPlayer indexOfNowPlayingItem];
MPMediaItem *nextItem = [playlist objectAtIndex:currentIndex+1];
return nextItem
}
-(MPMediaItem *)previousItem
{
int currentIndex = [musicPlayer indexOfNowPlayingItem];
MPMediaItem *previousItem = [playlist objectAtIndex:currentIndex-1];
return previousItem;
}
重要的提示:
我没有检查当前项目是否是playlist
的第一个/最后一个(根据您是否想要上一个/下一个)。 因此,请注意NSArray
边界,否则您将获得:
NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index Z beyond bounds [X .. Y]
那么previousItem
“存在”? nextItem
是否“存在”?
您可能还需要查看:
@property (nonatomic) MPMusicRepeatMode repeatMode
如果nextItem
可能是第一个项目,或者前一个项目是最后一个项目。
MPMusicPlayerController提供以下实例方法来快速跳至下一首或上一首歌曲:
[musicPlayer skipToNextItem];
[musicPlayer skipToPreviousItem];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.