NSNumber for MPMediaItemPropertyPersistentID to NSString and back again
Asked Answered
L

4

5

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.

Latoyia answered 18/1, 2011 at 15:8 Comment(0)
L
15

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.

Latoyia answered 18/1, 2011 at 21:56 Comment(1)
+1 for the solution. Without converting to NSNumber i tried to input NSString to predicate and returning 0 items. more than half a day gone trying to debug. Your solution helped. Thanks.Stenger
T
0

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

Tereasaterebene answered 18/1, 2011 at 18:9 Comment(1)
I've editing my question to include what I've tried. The number formatter was one of them.Latoyia
S
0

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.

Sandblind answered 21/9, 2011 at 18:8 Comment(0)
M
0

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;
Magel answered 8/1, 2015 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.