I browse all the songs from the iPhone 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];
Pretty simple stuff.
I get PersistentID as NSString
because I need to write it to an XML file (for transmission over the network to another device). Therefore, I canβt just leave it as NSNumber
.
Then another device will ask the iPhone to play the track, again transmitting PersistentID.
At this point, the iPhone has the NSString
PersistentID of the track to play.
It would be funny to repeat each song and compare PersistentID until I find the right track, so I'm trying to use MPMediaPropertyPredicate
to find the iPhone for me.
I use the following code to 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];
Where persistentID
is NSString
from earlier.
Strange, this works for some songs, not for others. those. sometimes the items
array is not empty, although I pass in an NSString
, not an NSNumber
.
I am wondering if there is a way to convert my NSString
back to NSNumber
, where it came from, and how I can do it.
UPDATE : I tried NSNumberFormatter, I also tried something like:
[NSNumber numberWithFloat:[persID floatValue]]
I tried all the standard ways to do this without prevailing.