iOS查询通过相册MPMediaEntityPersistentID有时会带回没有歌曲 [英] iOS Query via Album MPMediaEntityPersistentID sometimes brings back no songs

查看:233
本文介绍了iOS查询通过相册MPMediaEntityPersistentID有时会带回没有歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用Apple内置MediaPlayer库播放音乐的应用。我存储了一系列专辑和一组ID,然后当选择一个时,我使用id搜索一个专辑。出于某种原因,尽管有一个与之相关联的id,但有些专辑找不到任何歌曲。每次都是相同的专辑不起作用,似乎没有任何模式。

I'm writing an app that plays music using Apple's built in MediaPlayer library. I store an array of albums and an array of Ids, and then when one is selected I search for an album using the id. For some reason, some of the albums can't find any songs despite having an id associated with them. It is the same albums each time that won't work, and there seems to be no pattern to this.

我使用以下代码将名称和ID存储到两个数组

I use the following code to store the names and ids into two arrays

let query = MPMediaQuery.albumsQuery()
let mediaCollection = MPMediaItemCollection(items: query.items!)

for album in mediaCollection.items {
            albumTitleArray.append(album.albumTitle!)
            albumIdArray.append(album.albumPersistentID)
        }

当选择一个时,我会将id传递给queryMedia方法

When one is selected, I then pass the id to the queryMedia method

func queryMedia(identifier:MPMediaEntityPersistentID) -> MPMediaItemCollection {

    let predicateId = MPMediaPropertyPredicate(value: String(identifier), forProperty: MPMediaItemPropertyAlbumPersistentID, comparisonType:MPMediaPredicateComparison.EqualTo)

    let query = MPMediaQuery.init()
    query.addFilterPredicate(predicateId)

    let collection = MPMediaItemCollection(items: query.items!)

    return collection;
}

任何有关解决此问题的帮助都将不胜感激!

Any help in resolving this would be appreciated!

推荐答案

由于从MPMediaEntityPersistentID到String的转换导致查询中出现一些字符,或查询中存在比较错误。

There must have been some characters being malformed, or a comparison error in the query caused by the conversion from MPMediaEntityPersistentID to String.

我已将代码更改为转换为NSNumber,现在查询正确查找所有相册。

I've changed the code to instead convert to a NSNumber and the query now finds all albums correctly.

在代码示例中更改此部分以上

change this part in code example above

String(identifier)

NSNumber(unsignedLongLong: identifier)

所以看起来像这样

func queryMedia(identifier:MPMediaEntityPersistentID) -> MPMediaItemCollection {

let predicateId = MPMediaPropertyPredicate(value: NSNumber(unsignedLongLong: identifier), forProperty: MPMediaItemPropertyAlbumPersistentID, comparisonType:MPMediaPredicateComparison.EqualTo)

let query = MPMediaQuery.init()
query.addFilterPredicate(predicateId)

let collection = MPMediaItemCollection(items: query.items!)

return collection;
}

这篇关于iOS查询通过相册MPMediaEntityPersistentID有时会带回没有歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆