ios 在Apple MusicKit集成方面需要帮助

s71maibg  于 2023-03-05  发布在  iOS
关注(0)|答案(1)|浏览(158)

我正在开发和iOS应用程序,以集成Musickit从他们的音乐库中获取用户歌曲。由于我是Swift和Native iOS开发的新手,所以我很难理解苹果文档。
我可以从媒体库中获取用户专辑、歌曲,但其属性仅限于**Song(id: "i.GEGNo8Xh0EMr8Ov", title: "Walk On Water (feat. Beyoncé)", artistName: "Eminem")**
我需要得到其他属性的歌曲,如ISRC,流派,持续时间等。我可以做些什么来获得其他属性?下面提到的是我的用户音乐斯威夫特代码。
我正在跟踪此文档:https://developer.apple.com/documentation/musickit/song

if #available(iOS 15.0, *) {
          let status = await MusicAuthorization.request()
          switch status {
          case .authorized:
            
            do{
                let playlistRequest = MusicLibraryRequest<Song>()
                var playlistResponse = try await playlistRequest.response().items[1]
                print(playlistResponse)
                print(playlistResponse.artistName)
                
            }catch {
              
            }
0sgqnhkj

0sgqnhkj1#

用户曲库中的歌曲类型具有有限的信息,如名称、艺术家和ID。
您可以尝试从Apple Music目录中获取相应的歌曲:

let songsLibraryRequest = MusicLibraryRequest<Song>()
let songsLibraryResponse = try await songsLibraryRequest.response()

guard let librarySong = songsLibraryResponse.items.first else { return }

print(librarySong)

let url = URL(string: "https://api.music.apple.com/v1/me/library/songs/\(librarySong.id.rawValue)/catalog")

guard let url else { return }

let request = MusicDataRequest(urlRequest: URLRequest(url: url))
let response = try await request.response()

let catalogSongs = try JSONDecoder().decode(MusicItemCollection<Song>.self, from: response.data)

guard let catalogSong = catalogSongs.first else { return }

print(catalogSong)
print(catalogSong.artistName)
print(catalogSong.duration)
print(catalogSong.isrc)

相关问题