ios - Swift MPMediaPropertyPredicate 不过滤选定的专辑歌曲

标签 ios swift

<分区>

我正在用媒体播放器进行第二次重写。第一个在 Objective C 中,效果很好。我在 swift 中构建的第二个主要是为了制作更高效的音乐播放器并加快使用新语言的速度。

在我的媒体选择器中,我试图在点击专辑后获取专辑歌曲。我可以按艺术家过滤,但是当我应用专辑过滤器时,我得到一个空表。这是代码:

// Get the representative media item
var itemCollection: MPMediaItemCollection =
    items!.objectAtIndex(itemRow) as MPMediaItemCollection

var mediaItem: MPMediaItem = itemCollection.representativeItem

//Build the query with predicate filters
var albumPredicate: MPMediaPropertyPredicate =
    MPMediaPropertyPredicate(value: mediaItem.albumTitle,
                       forProperty: MPMediaItemPropertyAlbumTitle)

var artistPredicate: MPMediaPropertyPredicate =
    MPMediaPropertyPredicate(value: mediaItem.artist,
                       forProperty: MPMediaItemPropertyAlbumArtist)

var noCloudPredicate: MPMediaPropertyPredicate =
    MPMediaPropertyPredicate(value: NSNumber.numberWithBool(false),
                       forProperty: MPMediaItemPropertyIsCloudItem)

var query: MPMediaQuery = MPMediaQuery.songsQuery()
query.addFilterPredicate(noCloudPredicate)
query.addFilterPredicate(artistPredicate)
query.addFilterPredicate(albumPredicate)

如果我注释掉 albumPredicate,我将得到所选艺术家下所有歌曲的正确列表,这意味着我的 Storyboard连接正确。此外,mediaItem.albumTitle 会正确返回所选的专辑标题。

最佳答案

汤米感谢您的回复。我在单个查询中使用了多个 filterPredicates,它在 Objective C 中工作。以下所有代码都在 objective c 音乐播放器上工作,但等效的 swift albumPredicate 不起作用:

MPMediaItemCollection *itemCollection = [[self items] objectAtIndex:itemIndex];
MPMediaItem *mediaItem = [itemCollection representativeItem];

MPMediaPropertyPredicate *albumPredicate =
    [MPMediaPropertyPredicate predicateWithValue:[mediaItem albumTitle]
                                     forProperty:MPMediaItemPropertyAlbumTitle];

MPMediaPropertyPredicate *artistPredicate =
    [MPMediaPropertyPredicate predicateWithValue:[mediaItem artist]
                                     forProperty:MPMediaItemPropertyAlbumArtist];
MPMediaPropertyPredicate *noCloudPredicate =
    [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithBool:NO]
                                     forProperty:MPMediaItemPropertyIsCloudItem];

MPMediaQuery *query = [MPMediaQuery songsQuery];
[query addFilterPredicate:noCloudPredicate];
[query addFilterPredicate:artistPredicate];
[query addFilterPredicate:albumPredicate];

我的理解是 [MPMediaQuery songsQuery] 将选择歌曲并按歌曲分组。然后应用谓词。我想知道为什么它不能快速工作。

关于ios - Swift MPMediaPropertyPredicate 不过滤选定的专辑歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26957644/

相关文章:

ios - UILabel 中的 NSDateFormatter 返回 NULL

iOS:检索在线的 Game Center 好友

ios - 在 iPad 上构建 flickr 应用程序

ios - 按用户位置距 firebase 的距离对数组进行排序

swift - 快速舍入数字

ios - 具有模糊效果的 UIImagePickerController

ios - 我可以访问表示层中 CAShapeLayer 的路径属性吗?

swift - UIImageView 的延迟时间

swift - 分页垂​​直滚动 WatchKit

ios - 核心数据一对一关系更新