Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing MPMediaQuery to query ios library by rating
    primarykey
    data
    text
    <p>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)</p> <p>The code I use to filter is:</p> <pre><code>allMedia = [MPMediaQuery songsQuery]; MPMediaPropertyPredicate *mpp1 = [MPMediaPropertyPredicate predicateWithValue:@"2" forProperty:MPMediaItemPropertyRating comparisonType:MPMediaPredicateComparisonEqualTo]; [allMedia addFilterPredicate:mpp1]; </code></pre> <p>But MPMediaPropertyPredicate does not allow to filter by MPMediaItemPropertyRating (and actually it works ok with artist and song title).</p> <pre><code>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.' </code></pre> <p>I google by MPMediaItemPropertyRating but it seems that I should find an alternative approach to filter by track rating. </p> <p>Can somebody give me a tip?</p> <p>thanks</p> <p>UPDATE: This is my code to solve this:</p> <pre><code>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++ &lt; 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] &gt;= sliderRating.value &amp;&amp; [playCount intValue] &lt;= playsQuery) { if(j++ &gt; 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++ &gt; 1000)break; } MPMediaItemCollection *itemCol = [[MPMediaItemCollection alloc] initWithItems:mArray]; [myPlayer setQueueWithItemCollection:itemCol]; [myPlayer setShuffleMode: MPMusicShuffleModeSongs]; [myPlayer setRepeatMode: MPMusicRepeatModeNone]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload