0

我有这段代码可以播放音乐库中的所有歌曲(通过 iTunes 添加并从 Apple Music 下载到库中)。我不能只有歌曲。我究竟做错了什么?

-(IBAction)playAllSongs:(id)sender {

  MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
  NSArray *songsArray = [songsQuery collections];

  MPMediaItemCollection *items =  [MPMediaItemCollection collectionWithItems:songsArray];
  MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];
  [musicPlayer setQueueWithItemCollection:items]; 
}
4

2 回答 2

1

上面的答案有效,但这是一种迂回的方式。更简单的方法:

MPMediaQuery *songs = [MPMediaQuery songsQuery];
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];
[musicPlayer setQueueWithQuery:songs]
于 2016-10-12T04:52:18.560 回答
0

我解决了自己

MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
NSArray *songsArray = [songsQuery items];

MPMediaItemCollection *items =  [MPMediaItemCollection collectionWithItems:songsArray];
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];

//[songsQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithBool:NO] forProperty:MPMediaItemPropertyIsCloudItem]]; // for iTunes Match or Apple music to exclude or not songs not in the device

[musicPlayer setQueueWithItemCollection:items];
于 2016-09-29T15:14:50.003 回答