5

我正在开发一个 iphone 4 应用程序,该应用程序通过不同的标准过滤本地媒体库。我已经能够按歌曲名称、艺术家和流派进行过滤。现在我需要添加一个评级过滤机制。我在想类似“评级> = N星”(N:0..5)

我用来过滤的代码是:

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

但 MPMediaPropertyPredicate 不允许按 MPMediaItemPropertyRating 过滤(实际上它适用于艺术家和歌曲标题)。

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.'

我通过 MPMediaItemPropertyRating 进行谷歌搜索,但似乎我应该找到另一种按曲目评级过滤的方法。

有人可以给我小费吗?

谢谢

更新:这是我解决这个问题的代码:

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];
4

1 回答 1

4

MPMediaItemPropertyRating是用户定义的属性,根据Apple 文档

用户定义的属性不能用于构建媒体属性谓词。

解决这个问题的一种方法是首先遍历所有歌曲,将评分存储在数据库(或其他东西)中,然后从那里对数据进行排序。

于 2011-05-13T17:39:14.293 回答