3

My code sucessfully catalogs song names and ID's for my the entire music library. However, it will not actually play a song using this methodology and the console displays the following:

Message playbackState timed out.

Message nowPlayingItem timed out.

self.musicPlayer = [MPMusicPlayerController applicationMusicPlayer];

MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [everything items];
SongName = [[NSMutableArray alloc] init];
SongItem = [[NSMutableArray alloc] init];
NSString *songTitle;
NSString *songID;
//Collect names & ID for entire music library & put into arrays
for (MPMediaItem *song in itemsFromGenericQuery) {
songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
[SongName addObject:songTitle];
songID = [song valueForProperty: MPMediaItemPropertyPersistentID];
[SongItem addObject:songID];
}

NSLog (@"%@", [SongName objectAtIndex:1]);
NSLog (@"%@", [SongItem objectAtIndex:1]);
// Play the second song in the list
MPMediaItemCollection *collection = [MPMediaItemCollection collectionWithItems:[NSArray arrayWithObject:[SongItem objectAtIndex:1]]];
[self.musicPlayer setQueueWithItemCollection:collection];
[self.musicPlayer play];
Marsman
  • 825
  • 1
  • 10
  • 20

1 Answers1

6

Once again, I'll answer my own question. The issue was that collectionWithItems: expects an array of MPMediaItems, not an array of MPMediaItemPropertyPersistentIDs. Here is the working code for anyone who may have the same problem:

MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [everything items];
SongItem = [[NSMutableArray alloc] init];
for (MPMediaItem *song in itemsFromGenericQuery) {
   NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
   //NSLog (@”%@”, songTitle);
   songID = [song valueForProperty: MPMediaItemPropertyPersistentID];
   //NSLog (@”%@”, songID);
   [SongItem addObject:songID];
}

//Choose the first indexed song
NSString *selectedTitle = [SongItem objectAtIndex:0];

//Use the MPMediaItemPropertyPersistentID to play the song
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:selectedTitle forProperty:MPMediaItemPropertyPersistentID];
MPMediaQuery *mySongQuery = [[MPMediaQuery alloc] init];
[mySongQuery addFilterPredicate: predicate];
[musicPlayer setQueueWithQuery:mySongQuery];
[musicPlayer play];
Marsman
  • 825
  • 1
  • 10
  • 20
  • Only one minor issue with the code above (although won't affect this example in particular). The type associated with MPMediaItemPropertyPersistentID is NSNumber, not NSString. Thus, selectedTitle's type should be NSNumber*. Here's Apple's documentation:(http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMediaItem_ClassReference/Reference/Reference.html#//apple_ref/doc/c_ref/MPMediaItemPropertyPersistentID) – David Hunt Nov 12 '12 at 19:14
  • But how will we fetch the value of the MPMediaItemPropertyPersistentID in iPhone 5s. For some of the music from my device it is returning me a negative track_id because the value overflows. I am using NSNumber numberWithLong: – Itesh Jul 23 '14 at 08:00