>我有以下内容:
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];
}
效果很好,但只搜索了曲目的标题。我希望它返回标题、艺术家和专辑名称的结果。
这对我有用:
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];
}
我基本上在从媒体查询中获取所有项目后进行过滤。我不认为我的解决方案比首先过滤查询更好,但绝对比单独搜索三次更好。或者多次遍历数组。
尝试以下代码
MPMediaQuery* query = [MPMediaQuery albumsQuery];
[query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:self.strArtist forProperty:MPMediaItemPropertyArtist]]
[query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:self.strTitle forProperty:MPMediaItemPropertyAlbumTitle]];