gpt4 book ai didi

ios - 使用 MPMediaQuery 按评分查询 ios 库

转载 作者:可可西里 更新时间:2023-11-01 04:42:48 26 4
gpt4 key购买 nike

我正在开发一个 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];

最佳答案

MPMediaItemPropertyRating 是用户定义的属性,并且根据 Apple docs :

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

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

关于ios - 使用 MPMediaQuery 按评分查询 ios 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5983510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com