2

我有以下内容:

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

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

4

2 回答 2

5

这对我有用:

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

我基本上在从媒体查询中获取所有项目后进行过滤。我认为我的解决方案并不比首先过滤查询更好,但绝对比单独搜索三次更好。或者多次遍历数组。

于 2013-02-06T15:25:12.837 回答
4

试试下面的代码

 MPMediaQuery* query = [MPMediaQuery albumsQuery];

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

[query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:self.strTitle forProperty:MPMediaItemPropertyAlbumTitle]];
于 2012-06-04T08:47:18.800 回答