使用MPMediaPropertyPredicate搜索多种类型

时间:2012-04-24 08:14:31

标签: iphone ios ipod mpmediaitem mpmediaquery

我有以下内容:

MPMediaPropertyPredicate *titlePredicate = [MPMediaPropertyPredicate predicateWithValue:textString 
                                                                            forProperty:MPMediaItemPropertyTitle
                                                                         comparisonType:MPMediaPredicateComparisonContains];

NSSet *predicateSet = [NSSet setWithObject:titlePredicate];
MPMediaQuery *searchQuery = [[MPMediaQuery alloc] initWithFilterPredicates:predicateSet];
NSArray *itemsFromTextQuery = [searchQuery items];

 for (MPMediaItem *song in itemsFromTextQuery) 
 {
     [arrayOfSongItems addObject:song];
 }

哪个效果很好,但只搜索了曲目的标题。我希望它返回标题,艺术家和专辑名称的结果。

2 个答案:

答案 0 :(得分:4)

尝试以下代码

 MPMediaQuery* query = [MPMediaQuery albumsQuery];

[query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:self.strArtist forProperty:MPMediaItemPropertyArtist]]

[query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:self.strTitle forProperty:MPMediaItemPropertyAlbumTitle]];

答案 1 :(得分:4)

这对我有用:

MPMediaQuery *searchQuery = [[MPMediaQuery alloc] init];
NSPredicate *test = [NSPredicate predicateWithFormat:@"title contains[cd] %@ OR albumTitle contains[cd] %@ OR artist contains[cd] %@", searchString, searchString, searchString];
NSArray *filteredArray = [[searchQuery items] filteredArrayUsingPredicate:test];

//NSLog(@"%@", [filteredArray valueForKeyPath:@"title"]);

for (MPMediaItem *song in filteredArray) 
{
    [arrayOfSongItems addObject:song];
}

我基本上在获取媒体查询中的所有项目之后过滤。我不认为我的解决方案比首先过滤查询更好,但肯定比分别搜索三次更好。或者多次遍历数组。