如何在 iOS10 (Swift 3) 中获取正在播放歌曲的歌词

How to get lyrics for Now Playing song in iOS10 (Swift 3)

我想显示 iOS 系统播放器当前正在播放的歌曲的歌词。

这是我的自定义播放器:

import UIKit
import MediaPlayer
import AVFoundation

class NowPlayingController: NSObject {
    var musicPlayer: MPMusicPlayerController {
        if musicPlayer_Lazy == nil {
            musicPlayer_Lazy = MPMusicPlayerController.systemMusicPlayer()

            let center = NotificationCenter.default
            center.addObserver(self,
                selector: #selector(self.playingItemDidChange),
                name: NSNotification.Name.MPMusicPlayerControllerNowPlayingItemDidChange,
                object: musicPlayer_Lazy)
            musicPlayer_Lazy!.beginGeneratingPlaybackNotifications()
        }

        return musicPlayer_Lazy!
    }
    private var musicPlayer_Lazy: MPMusicPlayerController?

    var nowPlaying: MPMediaItem?

    //If song changes
    func playingItemDidChange(notification: NSNotification) {
        nowPlaying = musicPlayer.nowPlayingItem
    }
}

为了从 nowPlaying 项中获取歌词,我尝试了两种方法并且它们都 always return nil.

此代码总是 returns nil:

let lyricsText = nowPlaying?.value(forProperty: MPMediaItemPropertyLyrics) as? NSString as String?

在下面的代码中 MPMediaItemPropertyAssetURL 总是 returns nil 而不是实际的 URL:

let songUrl = nowPlaying?.value(forProperty: MPMediaItemPropertyAssetURL) as? NSURL as URL?
if songUrl != nil {
    let songAsset = AVURLAsset(url: songUrl!, options: nil)
    lyricsText = songAsset.lyrics

All songs are on device (synced by iTunes), contain lyrics (displayed in system player) and non DRM-protected (ripped aac/mp3).

我正在真实设备上测试:iPhone 6s/iOS 10.3

关于如何获取歌词或为什么 MPMediaItemPropertyAssetURL return 没有的任何建议?

我不知道为什么它不起作用,但看起来相同的代码现在可以正常工作了。也许它以某种方式连接到我现在用于播放器实例的单例。这是 100% 工作的 Swift 3 版本:

import UIKit
import MediaPlayer
import AVFoundation

class NowPlayingController: NSObject {

    static let sharedController = NowPlayingController()

    //MARK: Init
    private override init () {
        super.init()

        var musicPlayer_Lazy: MPMusicPlayerController?

        // System player instance
        if musicPlayer_Lazy == nil {
            musicPlayer_Lazy = MPMusicPlayerController.systemMusicPlayer()
            NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.playingItemDidChange),
                                               name: NSNotification.Name.MPMusicPlayerControllerNowPlayingItemDidChange,
                                               object: musicPlayer_Lazy)
            musicPlayer_Lazy!.beginGeneratingPlaybackNotifications()
        }

        self.musicPlayer = musicPlayer_Lazy!
    }

    // MARK: Class properties
    var musicPlayer: MPMusicPlayerController!
    var nowPlaying: MPMediaItem?

    // MARK: Methods
    func playingItemDidChange(notification: NSNotification) {
        nowPlaying = musicPlayer.nowPlayingItem
        NotificationCenter.default.post(newSongNotification as Notification)
    }

    func getLyrics() {
        let songUrl = nowPlaying?.value(forProperty: MPMediaItemPropertyAssetURL) as? URL
        let songAsset = AVURLAsset(url: songUrl!, options: nil)
        let lyricsText = songAsset.lyrics
    }
}

我知道这有点老了,但我只花了大约 4 个小时研究如何从 MP3 文件中读取歌词(如果它有歌词)。我正在使用 AVAudioPlayer 而不是 MPMusicPlayerController。天啊,太他妈简单了...

Swift 5.3 for macOS (我不知道这是否适用于 iOS,但我想它会)

import AVFoundation

func getLyrics(url: URL) -> String? {
    let asset = AVAsset(url: url)
    return asset.lyrics
}

注意它返回一个可选值,因此最好检查是否为 nil。希望这会节省一些人的研究时间。

请参阅 Apple 文档 here