gpt4 book ai didi

ios - MediaItemCollection 中的下一首歌曲?

转载 作者:行者123 更新时间:2023-11-29 02:57:17 24 4
gpt4 key购买 nike

如何找到 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 中 - 所以它是下一首歌曲的预览。

任何帮助将不胜感激,谢谢! :)

最佳答案

保留您的列表(因为您无法在音乐播放器队列中访问)。

@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;
}

重要说明:
我没有检查当前项目是否是播放列表中的第一个/最后一个(根据您是否想要上一个/下一个)项目。因此要小心 NSArray 的边界,否则你会得到:

NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index Z beyond bounds [X .. Y]

那么previousItem“存在”吗? nextItem“存在”吗?

您可能还需要查看:

@property (nonatomic) MPMusicRepeatMode repeatMode

如果 nextItem 可能是第一项,或者前一项是最后一项。

关于ios - MediaItemCollection 中的下一首歌曲?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23737913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com