Apple applicationMusicPlayer无法播放“ LibrarySong”类型,但是会播放“ Song”类型吗?

问题描述

正在使用Apple MediaPlayer frameworkAppleMusicAPI在移动应用中播放歌曲。

API调用(带有搜索字词参数)

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

成功返回一个Song类型的对象 MPMusicPlayerController.applicationMusicPlayer

可以播放

API调用

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

成功返回一个LibrarySong类型的对象 但不能与MPMusicPlayerController.applicationMusicPlayer一起播放

我这样玩:

        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()
                }
           }
        }

但仅播放所有LibrarySong条目时仍会收到此错误

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

此播放器是否同时播放两种类型的歌曲?

还有播放劲歌的步骤吗?

图书馆歌曲ID格式ID为:“ id”:“ i.ZomrKR9tv1xP989”

,歌曲格式ID为:“ id”:“ 1442890811”

解决方法

对于LibrarySongs,我最终在playParams中使用catalogId进行播放。

对于没有catalogId的Library Songs,只有购买的Id,我又打了个电话以获取catalogId进行播放

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