Any way to check if song is in Library with MusicKit? ?relate=library not working for me

For my app that heavily uses both ShazamKit and MusicKit, I need to be able to check if the matched song is in the user's library or not.

I couldn't find an easy way to do this with MusicKit so I first then turned to the Apple Music API as they introduced the new parameter ?relate=library during this year's WWDC.

When I first tried it, it worked as expected, and was very happy to have gotten it to work. However, it stopped working lately and now I turned to use MPMediaLibrary as that still works but is a lot slower in performance.

Anyone has any idea why the ?relate=library stopped working for me or know a better way to check if the Song exists in the user's library?

func checkInLibrary(from appleMusicID: String) async {
do {
    let countryCode = try await MusicDataRequest.currentCountryCode

    let libURL = URL(string: "https://api.music.apple.com/v1/catalog/\(countryCode)/songs/\(appleMusicID)?relate=library")!

    let request = MusicDataRequest(urlRequest: URLRequest(url: libURL))

    let dataResponse = try await request.response()
    print(dataResponse.debugDescription)  
            } 
catch {  // I'm handling errors here }}
Answered by in 704101022

We believe this is due to expired authentication tokens. MusicKit should now be handling refreshing those automatically for you. Can you please try again?

Hello @hiddevdploeg,

Thank you for your question on how to check whether a song is in the user's library.

Assuming appleMusicID in that code snippet is indeed the identifier of a Song, this code should work.

I tried this out on my end of things, and it worked just fine with one of the songs in my own library.

So this must be some kind of edge case. However, the information you sent us here is not sufficient for us to understand the root cause of this issue. I'm trying to figure out what additional information we can ask from you specifically to be able to investigate this further.

That said, I would love to hear more about how you were trying to use MPMediaLibrary to find this information. While MPMediaLibrary might not have sufficient public API support to do this well in a third-party app, it is backed by a local library on device, so it has the potential for being considerably faster than having to load additional content from a server.

For now, could you share a snippet of code showing how you were trying to achieve this with MPMediaLibrary?

Thank you very much in advance.

Best regards,

Hi Joe,

Thanks for responding! I tripple checked and appleMusic is indeed the identifier of a Song object so not sure what's happening there.

As in regards to your second request, here is how I'm currently checking in the MPMediaLibrary

func checkInLocalLib(with id: String) {
        let query = MPMediaQuery.songs()
        let allSongs = query.items! as [MPMediaItem]

        if allSongs.isEmpty == false {
            let matchedSong = allSongs.filter { $0.playbackStoreID == id }
            if matchedSong.isEmpty {
                currentSongInLibrary = false
            } else {
                currentSongInLibrary = true
            }
        }
    }

Hello @hiddevdploeg,

Thank you very much for following up on this.

Indeed, iterating through every song in the media library to figure out if a single one is there is bound to be extremely inefficient.

The only way to do this efficiently would be to use an MPMediaQuery with a filter predicate on the ID. However, you cannot do that with MPMediaItemPropertyPlaybackStoreID because it is not a filterable property.

As I was saying earlier, I think we're going to need more information to get to the bottom of this.

Could you please reproduce the issue with the Apple Music API endpoint and the ?relate=library query, grab a sysdiagnose, and then file a new ticket on Feedback Assistant including that sysdiagnose and an approximate timestamp when the issue happened? Please also include the ID of the song you were looking up.

Thank you very much in advance for your help.

Best regards,

Submitted feedback: FB9823513

Hello @hiddevdploeg,

Thank you very much for submitting this new ticket.

We have a good idea as to why ?relate=library probably stopped for you.

We'll keep you posted in the near future when we have a specific update about this issue.

Best regards,

Accepted Answer

We believe this is due to expired authentication tokens. MusicKit should now be handling refreshing those automatically for you. Can you please try again?

Any way to check if song is in Library with MusicKit? ?relate=library not working for me
 
 
Q