Apple applicationMusicPlayer not playing type "LibrarySong" but does play "Song" type?

326 views Asked by At

Am using Apple MediaPlayer framework and AppleMusicAPI to play songs in mobile app.

API call (with search term parameter):

https://api.music.apple.com/v1/catalog/{id}/search

Successfully returns a Song type object that is playable with the MPMusicPlayerController.applicationMusicPlayer

API call:

https://api.music.apple.com/v1/me/library/songs

Successfully returns a LibrarySong type object but will not play with MPMusicPlayerController.applicationMusicPlayer.

I am playing this way:

        self.cloudPlayer = MPMusicPlayerController.applicationMusicPlayer
        self.cloudPlayer?.setQueue(with: [trackId])
        
       Async.main {
            self.cloudPlayer?.prepareToPlay {error in
                if let error = error {
                    logger.debug("Not Loaded, Prepare to Play Error: \(error)")
                } else {
                    logger.debug("Loaded, start playing")
                    self.cloudPlayer?.play()
                }
           }
        }

but keep receiving this error when playing all LibrarySong entries only:

Not Loaded, Prepare to Play Error: 
Error Domain=MPMusicPlayerControllerErrorDomain Code=6 "Failed to prepare to play" UserInfo={NSDebugDescription=Failed to prepare to play}

does this player play both types of songs?

is there an additional step to play a LibrarySong?

the Library song id format id is : "id" : "i.ZOMrKR9tv1xP989"

and the Song format id is: "id" : "1442890811"

1

There are 1 answers

0
dreloz On

For LibrarySongs, I ended up using the catalogId in playParams for playback.

For LibrarySongs that don't have a catalogId only a purchasedId, I made another call to get the catalogId for playback

https://api.music.apple.com/v1/catalog/us/songs?ids=\(purchasedId)