0

I have this code to play all songs in music library (added via iTunes and downloaded in library from Apple Music). I can not have just the songs. What am I doing wrong?

-(IBAction)playAllSongs:(id)sender {

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

  MPMediaItemCollection *items =  [MPMediaItemCollection collectionWithItems:songsArray];
  MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];
  [musicPlayer setQueueWithItemCollection:items]; 
}
Joannes
  • 2,569
  • 3
  • 17
  • 39
  • is there any problem you facing just clear your ques .. ? – vaibhav Sep 27 '16 at 13:13
  • my code return all media items (pdf, epub, video, songs, etc..) but not only songs. – Joannes Sep 27 '16 at 13:22
  • show your array, what data you are getting .. – vaibhav Sep 27 '16 at 13:28
  • I have the 4 songs in console (it's right) 'MyApp[338:25524] ( "", "", "", "" )' but not in app. Do you have a solution or tips or not? – Joannes Sep 27 '16 at 16:27
  • when after all this indicate we expect an answer... keyboard phenomena! – Joannes Sep 28 '16 at 05:57
  • as i said earlier you ques is not clear please edit it and show all the data with array so we can help .. – vaibhav Sep 28 '16 at 06:10
  • Who knows the framework understands that the problem is this line: MPMediaItemCollection * items = [MPMediaItemCollection collectionWithItems: songsArray]; without need of NSLog. Again keyboard phenomena! – Joannes Sep 28 '16 at 12:08

2 Answers2

1

The above answer works but it's a roundabout way to do it. A simpler way:

MPMediaQuery *songs = [MPMediaQuery songsQuery];
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];
[musicPlayer setQueueWithQuery:songs]
Clark
  • 36
  • 3
0

I solved my self

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];
Joannes
  • 2,569
  • 3
  • 17
  • 39