I am developing and IOS app to integrate MusicKit
to get User songs from their Music Library.
Since I am new to Swift and Native IOS development, it is quite difficult for me to understand the Apple Documentation.
I have able to get users Album, Songs, from library but It's properties are limited to
Song(id: "i.GEGNo8Xh0EMr8Ov", title: "Walk On Water (feat. Beyoncé)", artistName: "Eminem")
I need to get other properties of songs such as ISRC, genre, duration etc. What can I do to get the other properties? Below mentioned is my Swift Code for User Music.
I'm following this docs from Apple.
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 {
/* handle error */
}
}
The type of the song from the user's library has limited information, like the name, artist and the ID.
What you can do instead is try to get the equivalent song from the Apple Music catalog:
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)