My app loads a music playlist automatically when it starts up. In order to do this, I store the song IDs MPMediaItemPropertyPersistentID
to a database, and load the songs when the app is starting up next time. The main code is the following:
MPMediaQuery *MPMediaSongQuery = [MPMediaQuery songsQuery];
MPMediaPropertyP开发者_运维知识库redicate *iPodMusicSongPredicateiPodMusicSongPredicate = [MPMediaPropertyPredicate
predicateWithValue:[NSNumber numberWithUnsignedLongLong: songID]
forProperty:MPMediaItemPropertyPersistentID
comparisonType:MPMediaPredicateComparisonEqualTo];
[MPMediaSongQuery addFilterPredicate:iPodMusicSongPredicate];
NSArray *collections = MPMediaSongQuery.collections;
The code loads song one by one. My question is: is there any way to query two or more songs by MPMediaItemPropertyPersistentID
at one time when using the function addFilterPredicate:
? Thanks.
If you use more than one addFilterPredicate they are combined with a logical AND. So you canm only reduce the results of the first query, but not extend it. As a consequence you are not allowed to use several addFilterPredicates for the same property. In fact the result is undefined and most likely will end up in an empty collection. What you are looking for is a combination of the same property with a logical OR. You can achieve this like depicted in the following pseudo-code:
MPMediaQuery *MPMediaSongQuery = [MPMediaQuery songsQuery];
NSMutableArray *collections = [[NSMutableArray alloc] initWithCapacity:1];
for (int i=0; i < songIDCount; i++) {
MPMediaPropertyPredicate *iPodMusicSongPredicateiPodMusicSongPredicate = [MPMediaPropertyPredicate
predicateWithValue:[NSNumber numberWithUnsignedLongLong: songID[i]]
forProperty:MPMediaItemPropertyPersistentID
comparisonType:MPMediaPredicateComparisonEqualTo];
[MPMediaSongQuery addFilterPredicate:iPodMusicSongPredicate];
[collections addObjectsFromArray:MPMediaSongQuery.collections];
[MPMediaSongQuery removeFilterPredicate:iPodMusicSongPredicate];
}
...
精彩评论