Reputation: 515
I'm making a music player app that can access and play music and playlists that come from Apple Music and the iOS music library, and I want to be able to save and reload the user's song queue across app launches. If a song is locally stored on a phone, it's easy enough to save its MPMediaItem.persistentID and then load the item back up using a predicate like this:
let predicate = MPMediaPropertyPredicate(value: savedLocalID, forProperty: MPMediaItemPropertyPersistentID);
let songQuery = MPMediaQuery.init(filterPredicates: [predicate]);
However, with songs that come from playlists saved from Apple music, their MPMediaItem.persistentID isn't preserved across app launches and doesn't work to load them as MPMediaItems. I was hoping that MPMediaItem.playbackStoreID could be used in a similar way with MPMediaQuery, but when I try to make a predicate using it with MPMediaItemPropertyPlaybackStoreID I get the error "MPMediaPropertyPredicate cannot filter using the playbackStoreID property." Any ideas how to get an MPMediaItem from a saved MPMediaItem.playbackStoreID, or another property I'm missing that works for non-local Apple Music files? Thanks!
Upvotes: 2
Views: 1558
Reputation: 4565
The best way I've found is to scan the library on app startup and keep a lookup table of playbackStoreIDs vs persistentIDs. I use Core Data for this. I found that going through all library items is fast. Updating the table can take long-ish the first time because it has to save all the pairings, but as my entity is indexed by persistentID, further updates are fast. Times will vary depending on device and library size, but this is a possible approach.
Upvotes: 0