我正在尝试播放iPod播放列表中的歌曲。就播放列表也可能有视频而言,我只想在其中播放歌曲。
MPMusicPlayerController *mp = [MPMusicPlayerController applicationMusicPlayer];
MPMediaQuery *query = [MPMediaQuery songsQuery];
query.groupingType = MPMediaGroupingPlaylist;
NSArray *playlists = [query collections];
BOOL found = NO;
for(MPMediaPlaylist *playlist in playlists)
{
NSString *name = [playlist valueForProperty:MPMediaPlaylistPropertyName];
if([name caseInsensitiveCompare:@"MyPlaylist"] == NSOrderedSame && [playlist count] > 0)
{
MPMediaItemCollection *collection = [MPMediaItemCollection collectionWithItems:playlist.items];
[mp setQueueWithItemCollection:collection];
found = YES;
NSLog(@"items count %d", [playlist count]);
for (MPMediaItem *item in [collection items]){
NSLog(@"%@", [item valueForKey:MPMediaItemPropertyMediaType]);
}
break;
}
}
加载包含 1 首歌曲和 2 个视频的播放列表后,输出为:
项目计数 3
256
256
1
我不明白为什么我也会收到视频,如果查询旨在检索歌曲,并且媒体项的媒体类型为 MPMediaTypeMovie(256 或 1 <<8)。
有什么建议吗?
一种可能的解决方法是使用谓词创建自定义查询。这可以通过使用MPMediaQuery
items
数组而不是collections
数组来实现。collections
数组是 MPMediaItemCollection
的数组。items
数组是 MPMediaItem
的数组,因此您可以按 MPMediaItemPropertyMediaType
筛选查询。 MPMediaItemPropertyMediaType
仅适用于MPMediaItem
,不适用于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];
如果从MPMediaQuery
collections
数组返回数组,这些MPMediaPropertyPredicate
将不会过滤。您应该在日志语句中看到 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.
}];
如果您从MPMediaQuery
items
返回数组,则MPMediaPropertyPredicate
s 将过滤:
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.
您还可以通过在songsQuery
上仅使用 mediaTypePredicate 将媒体类型筛选器应用于整个库:
MPMediaQuery *query = [MPMediaQuery songsQuery];
[query setGroupingType:MPMediaGroupingPlaylist];
[mediaTypeQuery addFilterPredicate:mediaTypePredicate];
NSArray *playlists = [query items];