Possible to query for song by Apple Music trackId?

Given an Apple Music trackId is it possible to query the user’s media library to see if they’ve added it to their library? Something like:

Code Block
let predicate = MPMediaPropertyPredicate(value: "1440818675", forProperty: MPMediaItemPropertyPersistentID)
let query = MPMediaQuery(filterPredicates: Set([predicate]))
let songs = query.items
let isInLibrary = !songs.isEmpty

Did you ever figure this out? I'm looking for the same thing.

Hello @Jordan and @cheddarburrito,

MediaPlayer exposes another property you could filter by which works for IDs of Apple Music subscription items: MPMediaItemPropertyPlaybackStoreID.

Could you try something like this?

let predicate = MPMediaPropertyPredicate(value: "1440818675", forProperty: MPMediaItemPropertyPlaybackStoreID)
let query = MPMediaQuery(filterPredicates: Set([predicate]))
let songs = query.items
let isInLibrary = !songs.isEmpty

I hope this helps.

Best regards,

Hello @Jordan and @cheddarburrito,

I have a new suggestion which could help you achieve what you're trying to do. Using Apple Music API directly, you could request the library relationship on something like a song. See the list of song relationships for more information.

The idea is that if the resulting SongsLibraryRelationship is not empty, and contains one library song, then you know that the user has this song in their music library.

I hope this helps.

Best regards,

Possible to query for song by Apple Music trackId?
 
 
Q