需要帮助进行Apple MusicKit集成。

huangapple go评论67阅读模式
英文:

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&#39;s properties are limited to 
**`Song(id: &quot;i.GEGNo8Xh0EMr8Ov&quot;, title: &quot;Walk On Water (feat. Beyonc&#233;)&quot;, artistName: &quot;Eminem&quot;)`**

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&#39;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&lt;Song&gt;()
        var playlistResponse = try await playlistRequest.response().items[1]
        print(playlistResponse)
        print(playlistResponse.artistName)
    } catch {
        /* handle error */
    }
}

答案1

得分: 2

用户的音乐库中的歌曲类型信息有限,如名称、艺术家和ID。

你可以尝试从Apple Music目录中获取相应的歌曲:

let songsLibraryRequest = MusicLibraryRequest&lt;Song&gt;()
let songsLibraryResponse = try await songsLibraryRequest.response()

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

print(librarySong)

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

guard let url else { return }

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

let catalogSongs = try JSONDecoder().decode(MusicItemCollection&lt;Song&gt;.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&lt;Song&gt;()
let songsLibraryResponse = try await songsLibraryRequest.response()

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

print(librarySong)

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

guard let url else { return }

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

let catalogSongs = try JSONDecoder().decode(MusicItemCollection&lt;Song&gt;.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 }

更多详情请参考 这里

英文:

Use the with(_:) instance method

let detailedSong = try await song.with([.genres])
guard let genres = detailedSong.genres else { return }

More details here

huangapple
  • 本文由 发表于 2023年2月8日 20:28:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定