【发布时间】:2023-04-10 16:22:01
【问题描述】:
我也有类似的问题:
- How do I get the lyrics of the currently playing song in Swift 4? - 这里没有答案
- iPhone song lyrics access - 回答无效
- How to get lyrics for Now Playing song in iOS10 (Swift 3) - 回答无效
我需要获取当前播放歌曲的歌词。
如果我只是尝试从媒体项中获取歌词,则会得到空结果:
let musicPlayerController = MPMusicPlayerController.systemMusicPlayer
// ...
if let nowPlayingItem = musicPlayerController.nowPlayingItem {
// We have empty string here
if let lyrics = nowPlayingItem.lyrics {
print("Lyrics: \(lyrics)")
}
}
我已尝试按照上述答案中的建议通过资产 url 获取歌词,但它不起作用,资产 url 始终为零。
let musicPlayerController = MPMusicPlayerController.systemMusicPlayer
// ...
if let nowPlayingItem = musicPlayerController.nowPlayingItem {
if let songUrl = nowPlayingItem.value(forProperty: MPMediaItemPropertyAssetURL) as? URL {
print("song url: \(songUrl)")
let songAsset = AVURLAsset(url: songUrl)
if let lyricsFromAsset = songAsset.lyrics {
print("Lyrics from asset: \(lyricsFromAsset)")
}
}
}
我也尝试过但没有成功(也相应地得到 nil 和空字符串):
print("asset url: \(nowPlayingItem.assetURL)")
print("lyrics from value for property: \(nowPlayingItem.value(forProperty: MPMediaItemPropertyLyrics))")
我确切地知道这首歌有歌词,因为我在 iPhone 上的 Apple Music 应用程序中看到了这首歌的歌词。我试着为这首歌找歌词:Queen - Bohemian Rhapsody。
有人可以帮忙从苹果音乐中获取当前播放歌曲的歌词吗?
【问题讨论】:
-
我遇到了同样的问题你有解决办法吗?
标签: ios swift mpmediaitem