英文:
Need help in Apple MusicKit integration
问题
我正在开发一个iOS应用,以集成MusicKit
来从用户的音乐库中获取用户的歌曲。
由于我对Swift和原生iOS开发都很陌生,所以我很难理解苹果的文档。
我已经能够从库中获取用户的专辑和歌曲,但其属性有限,例如
Song(id: "i.GEGNo8Xh0EMr8Ov", title: "Walk On Water (feat. Beyoncé)", artistName: "Eminem")
我需要获取歌曲的其他属性,例如ISRC、流派、时长等。我应该如何获取其他属性?以下是我用于用户音乐的Swift代码。
我正在遵循苹果的这个文档。
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 {
/* 处理错误 */
}
}
<details>
<summary>英文:</summary>
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][1].
```swift
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 */
}
}
答案1
得分: 2
用户的音乐库中的歌曲类型信息有限,如名称、艺术家和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)
英文:
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)
答案2
得分: 0
使用 with(_:)
实例方法
let detailedSong = try await song.with([.genres])
guard let genres = detailedSong.genres else { return }
更多详情请参考 这里
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论