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.
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)
}
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!