0

I'm trying to play back a song referenced from a stored persistentIdentifier, but the MPMediaQuery returns 0 results if I apply a filter to it using the stored value.

My code for the not-working filtered version:

NSNumber *persistentId = [NSNumber numberWithLongLong:15991677378153886747];
MPMediaPredicate *filter = [MPMediaPropertyPredicate predicateWithValue:persistentId forProperty:MPMediaItemPropertyPersistentID];
MPMediaQuery *songQuery = [[MPMediaQuery alloc] initWithFilterPredicates:[NSSet setWithObject:filter]];
NSArray *songs = [songQuery items]; // [songs count] is zero here

Despite songs containing a MPMediaItem which should pass through this filter, I instead get zero items returned. Check this out (done the bad way):

MPMediaQuery *songQuery = [[MPMediaQuery alloc] init];
NSArray *songs = [songQuery items];

 // gives <MPConcreteMediaItem: 0x1dd4d3c0> 15991677378153886747
NSLog(@"%@", [songs objectAtIndex:5]); 

 // gives 15991677378153886747
NSLog(@"%@", persistentId);

What am I doing wrong? I have read the docs / sample projects and it looks like this is how one does it.

buildsucceeded
  • 4,203
  • 4
  • 34
  • 72

2 Answers2

0

It might be an issue with initializing the unsigned long long constant. Try adding a ULL suffix to the number:

15991677378153886747ULL

Then wrap it into an NSNumber literal:

NSNumber *persistentId = @15991677378153886747ULL;
// Identical to: [NSNumber numberWithUnsignedLongLong:15991677378153886747ULL]
Bryan Luby
  • 2,527
  • 22
  • 31
0

I don't think it's zero items, it's saying it has one concrete media item:

// gives <MPConcreteMediaItem: 0x1dd4d3c0> 15991677378153886747

Try adding this, see if any titles pop up:

NSLog(@"%@", [songs valueForKeyPath:@"title"]);
Fluffhead
  • 845
  • 1
  • 9
  • 21
  • I think you are looking at the "this way works" part of my code... that is the "bad" way of (1) getting a list of all songs in the music library, and (2) reading a known index in a test environment for debugging :) – buildsucceeded Sep 14 '13 at 08:26