一种可能的解决方法是使用谓词创建自定义查询。这可以通过使用MPMediaQuery
items
数组而不是collections
数组来实现。该collections
数组是MPMediaItemCollection
s 的数组。该items
数组是MPMediaItem
s 的数组,因此您可以通过 过滤查询MPMediaItemPropertyMediaType
。MPMediaItemPropertyMediaType
仅适用于 aMPMediaItem
而不是 a MPMediaItemCollection
。
根据播放列表标题和媒体类型,使用一组谓词初始化查询。这将选择您的播放列表并从查询中过滤掉所有视频:
NSString *playlistName = @"MyPlaylist";
MPMediaPropertyPredicate *playlistPredicate = [MPMediaPropertyPredicate predicateWithValue:playlistName
forProperty:MPMediaPlaylistPropertyName];
NSNumber *mediaTypeNumber = [NSNumber numberWithInteger:MPMediaTypeMusic]; // == 1
MPMediaPropertyPredicate *mediaTypePredicate = [MPMediaPropertyPredicate predicateWithValue:mediaTypeNumber
forProperty:MPMediaItemPropertyMediaType];
NSSet *predicateSet = [NSSet setWithObjects:playlistPredicate, mediaTypePredicate, nil];
MPMediaQuery *mediaTypeQuery = [[MPMediaQuery alloc] initWithFilterPredicates:predicateSet];
[mediaTypeQuery setGroupingType:MPMediaGroupingPlaylist];
MPMediaPropertyPredicate
如果您从数组中返回数组,这些s 将不会过滤MPMediaQuery
collections
。您应该在日志语句中看到 1 以外的媒体项目类型:
NSArray *playlistCollections = [mediaTypeQuery collections];
[playlistCollections enumerateObjectsUsingBlock:^(MPMediaPlaylist *playlist, NSUInteger idx, BOOL *stop) {
NSLog (@"Playlist title: %@", [playlist valueForProperty:MPMediaPlaylistPropertyName]);
NSArray *songs = [playlist items];
[songs enumerateObjectsUsingBlock:^(MPMediaItem *song, NSUInteger idx, BOOL *stop) {
NSString *songTitle = [song valueForProperty:MPMediaItemPropertyTitle];
NSInteger mediaValue = [[song valueForProperty:MPMediaItemPropertyMediaType] integerValue];
if (mediaValue != 1)
NSLog(@"title: %@ - media type value: %d", songTitle, mediaValue);
}]; // Should return other media types here.
}];
如果您从以下MPMediaPropertyPredicate
位置返回数组,则 s 将过滤MPMediaQuery
items
:
NSArray *playlistItems = [mediaTypeQuery items];
[playlistItems enumerateObjectsUsingBlock:^(MPMediaItem *song, NSUInteger idx, BOOL *stop) {
NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
NSInteger mediaValue = [[song valueForProperty:MPMediaItemPropertyMediaType] integerValue];
if (mediaValue != 1)
NSLog(@"title: %@ - media type value: %d", songTitle, mediaValue);
}]; // Should return nothing here.
您还可以通过仅使用以下上的 mediaTypePredicate 将媒体类型过滤器应用于整个库songsQuery
:
MPMediaQuery *query = [MPMediaQuery songsQuery];
[query setGroupingType:MPMediaGroupingPlaylist];
[mediaTypeQuery addFilterPredicate:mediaTypePredicate];
NSArray *playlists = [query items];