Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MPMediaQuery to query ios library by rating

I'm developing an iphone 4 app that filters the local media library by different criteria. I have been able to filter by song name, artist, and genre. Now I need to add a rating filter mechanism. I was thinking in something like "rating >= N stars" (N:0..5)

The code I use to filter is:

allMedia = [MPMediaQuery songsQuery];
MPMediaPropertyPredicate *mpp1 = [MPMediaPropertyPredicate predicateWithValue:@"2"     forProperty:MPMediaItemPropertyRating comparisonType:MPMediaPredicateComparisonEqualTo];
[allMedia addFilterPredicate:mpp1];

But MPMediaPropertyPredicate does not allow to filter by MPMediaItemPropertyRating (and actually it works ok with artist and song title).

2011-05-12 11:37:39.060 Radio3[1525:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'MPMediaPropertyPredicate cannot filter using the rating property.'

I google by MPMediaItemPropertyRating but it seems that I should find an alternative approach to filter by track rating.

Can somebody give me a tip?

thanks

UPDATE: This is my code to solve this:

allMedia = [MPMediaQuery songsQuery];
//MPMediaPropertyPredicate *mpp1 = [MPMediaPropertyPredicate predicateWithValue:@"2"   forProperty:MPMediaItemPropertyRating comparisonType:MPMediaPredicateComparisonEqualTo];
//MPMediaPropertyPredicate *mpp2 = [MPMediaPropertyPredicate predicateWithValue:@"Pop" forProperty:MPMediaItemPropertyGenre comparisonType:MPMediaPredicateComparisonContains];
//[allMedia addFilterPredicate:mpp1];
//[allMedia addFilterPredicate:mpp2];

//[myPlayer setQueueWithQuery:allMedia];


NSArray *itemsFromGenericQuery = [allMedia items];

NSMutableArray *mArray = [[NSMutableArray alloc] init];
int i = 0;
int j=0;

NSLog(@"itemCount: %d",[itemsFromGenericQuery count]);

float playsQuery = sliderPlays.value;
if(playsQuery == 20){playsQuery = 10000;}

NSLog(@"sliderRating.value %f  sliderPlays.value %.1f", [sliderRating value],  playsQuery);

while(i++ < 1000){
    int trackNumber = arc4random() % [itemsFromGenericQuery count];
    MPMediaItem *song = [itemsFromGenericQuery objectAtIndex:trackNumber];

    NSString *artistName = [song valueForProperty: MPMediaItemPropertyArtist];
    NSString *title = [song valueForProperty: MPMediaItemPropertyTitle];
    NSString *rating = [song valueForKey:MPMediaItemPropertyRating];
    double lengh = [[song valueForProperty:MPMediaItemPropertyPlaybackDuration] doubleValue];
    NSNumber *playCount = [song valueForKey:MPMediaItemPropertyPlayCount];



    if ([rating intValue] >= sliderRating.value && [playCount intValue] <= playsQuery) {
        if(j++ > 50){break;}
        NSLog (@"tracknumber: %d j: %d  artistName: %@ title: %@ lengh: %.1f rating: %@ playcount: %d",trackNumber, j, artistName, title, lengh, rating, [playCount intValue]);            
        [mArray addObject:song];            
    }

    if(i++ > 1000)break;
}


MPMediaItemCollection *itemCol = [[MPMediaItemCollection alloc] initWithItems:mArray];


[myPlayer setQueueWithItemCollection:itemCol];



[myPlayer setShuffleMode: MPMusicShuffleModeSongs];
[myPlayer setRepeatMode: MPMusicRepeatModeNone];
like image 662
Mariano Latorre Avatar asked Oct 12 '22 01:10

Mariano Latorre


1 Answers

MPMediaItemPropertyRating is a user-defined property, and according to the Apple docs:

User-defined properties cannot be used to build media property predicates.

One way to get around this would be to initially iterate over all the songs, store the ratings in a database (or something else) and sort the data from there.

like image 129
venables Avatar answered Dec 28 '22 08:12

venables