MPMediaQuery搜索艺术家

MPMediaQuery搜索艺术家

本文介绍了MPMediaQuery搜索艺术家,专辑和歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

如何以与iOS音乐应用程序相同的方式搜索iPod库?我想做一般查询,返回每个艺术家,专辑和歌曲的结果。例如,如果我搜索Kenny Chesney,我希望歌曲查询返回所有Kenny Chesney歌曲(以及包含Kenny Chesney的任何歌曲标题或专辑。)当我为每个属性(歌曲标题,专辑)进行此查询和谓词时标题,艺术家姓名),一个空数组返回。

How can I search the iPod Library in the same manner as the iOS Music application? I want to make general queries that return results for each Artists, Albums, and Songs. For instance, if I search Kenny Chesney I want the songs query to return all Kenny Chesney songs (and any songs titles or albums that contain Kenny Chesney in them.) When I make this query and a predicate for each property (song title, album title, artist name), an empty array returns.

这里有一些代码可以让你更好地了解我想要完成的事情:

MPMediaPropertyPredicate *songPredicate =
[MPMediaPropertyPredicate predicateWithValue:searchText
                                 forProperty:MPMediaItemPropertyTitle
                              comparisonType:MPMediaPredicateComparisonContains];

MPMediaPropertyPredicate *albumPredicate =
[MPMediaPropertyPredicate predicateWithValue:searchText
                                 forProperty:MPMediaItemPropertyAlbumTitle
                              comparisonType:MPMediaPredicateComparisonContains];

MPMediaPropertyPredicate *artistPredicate =
[MPMediaPropertyPredicate predicateWithValue:searchText
                                 forProperty:MPMediaItemPropertyArtist
                              comparisonType:MPMediaPredicateComparisonContains];

MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
[songsQuery addFilterPredicate:songNamePredicate];
[songsQuery addFilterPredicate:artistNamePredicate];
[songsQuery addFilterPredicate:albumNamePredicate];

NSLog(@"%@", [songsQuery items]);

我通过分别运行每个谓词的查询来实现这一点,但这看起来非常低效!

I have this working by running the query with each predicate separately but this seems very inefficient!

推荐答案

以这种方式组合谓词使其成为与关系。这意味着您要查询的歌曲的标题,专辑和名称都与搜索文本相匹配。

Combining your predicates this way makes it like "AND" relationship. It means that you are querying for a song that has title, album and name all are matching the search text.

要实现您的目标,您应该运行3查询并以适当的方式组合结果。

To achive what you are trying to do, you should run 3 queries and combining the results in a proper way.

如果您运行:

MPMediaPropertyPredicate *artistPredicate =
[MPMediaPropertyPredicate predicateWithValue:searchText
                                 forProperty:MPMediaItemPropertyArtist
                              comparisonType:MPMediaPredicateComparisonContains];

NSSet *predicates = [NSSet setWithObjects: artistPredicate, nil];

MPMediaQuery *songsQuery =  [[MPMediaQuery alloc] initWithFilterPredicates: predicates];

NSLog(@"%@", [songsQuery items]);

这将使您与符合搜索条件的艺术家联系。
你应该为歌曲和专辑做同样的事情。

This will return you with artists matching your search.The same you should do for songs and albums.

如果你这很慢,你可以一次检索所有媒体并手动过滤:

If you this this is slow, you may retrieve all the media at once and filter it manually:

MPMediaQuery *everything = [[MPMediaQuery alloc] init];

NSLog(@"Logging items from a generic query...");
NSArray *itemsFromGenericQuery = [everything items];
for (MPMediaItem *song in itemsFromGenericQuery) {
    NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
    /* Filter found songs here manually */
}

这篇关于MPMediaQuery搜索艺术家,专辑和歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:46