Question

I'm looping through all the songs from an iPhone's music library using the following code:

NSArray * songs = [[NSArray alloc] initWithArray:[[MPMediaQuery songsQuery] collections]];

for (MPMediaItemCollection * item in songs){

    NSString * persistentID = [[[item representativeItem] valueForProperty:MPMediaItemPropertyPersistentID] stringValue];
    // Do something with it.
}

[songs release];

Pretty basic stuff.

I'm getting the PersistentID as an NSString because I need to write it to an XML file (for transmission over a network to another device). Hence the reason I can't just leave it as an NSNumber.

The other device will then ask for the iPhone to play a track by transmitting the PersistentID back again.

At this point, the iPhone has an NSString of the PersistentID of the track it should play.

It would be combersome to loop through every song again and compare PersistentIDs until I find the track I want, so I'm trying to use the MPMediaPropertyPredicate to have the iPhone search for me.

I'm using the following code for the search:

MPMediaPropertyPredicate * predicate = [MPMediaPropertyPredicate predicateWithValue:persistentID forProperty:MPMediaItemPropertyPersistentID];

MPMediaQuery * songsQuery = [[MPMediaQuery alloc] init];
[songsQuery addFilterPredicate:predicate];

if ([[songsQuery items] count]){

    MPMediaItem * item = [[songsQuery items] objectAtIndex:0];
    // Play item.
}

[songsQuery release];

Where persistentID is the NSString from earlier.

Weirdly, this works for some songs, not for others. i.e, sometimes the items array is not empty, even though I'm passing an NSString, not an NSNumber.

I'm wondering if there's a way to convert my NSString back to the NSNumber it came from, and how I can do that.

UPDATE: I've tried the NSNumberFormatter, I've also tried something like:

[NSNumber numberWithFloat:[persID floatValue]];

I've tried all the standard ways of doing it without prevail.

Was it helpful?

Solution

This is working pretty well, no problems so far:

unsigned long long ullvalue = strtoull([persistentID UTF8String], NULL, 0);
NSNumber * numberID = [[NSNumber alloc] initWithUnsignedLongLong:ullvalue];

MPMediaPropertyPredicate * predicate = [MPMediaPropertyPredicate predicateWithValue:numberID forProperty:MPMediaItemPropertyPersistentID];
[numberID release];

// And so on.

Hope this helps anyone else who ran into this problem.

OTHER TIPS

If I understand you correctly, you're trying to convert an NSString to an NSNumber. To do this, you can use a NSNumberFormatter. Take a look at the numberFromString: method.

NSNumberFormatter Class Reference

I just had to do the same thing. The Query Predicate for MPMediaItemPropertyPersistentID has to be passed in as NSNumber. I found this answer on converting NSString to NSNumber from another StackOverflow post:

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * persistentIDasNumber = [f numberFromString:persistantID];
[f release];

MPMediaPropertyPredicate * predicate = [MPMediaPropertyPredicate   predicateWithValue:persistentIDasNumber  forProperty:MPMediaItemPropertyPersistentID];

This worked for me.

I ran into something very similar and worked out the following:

NSNumber *musicIdentifier = [[[NSNumberFormatter alloc] init] numberFromString: persistentID];
MPMediaQuery *query = [[MPMediaQuery alloc] initWithFilterPredicates:[NSSet setWithObject:[MPMediaPropertyPredicate predicateWithValue:musicIdentifier forProperty:MPMediaItemPropertyPersistentID]]];

MPMediaItem *item = query.items.firstObject;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top