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

时间:2017-04-05 06:01:13

标签: ios swift swift3 avfoundation mpmediaitem

我想显示当前由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项目中获取歌词,我尝试了2种方法,并且两者都始终返回nil

此代码始终返回nil:

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

在以下代码MPMediaItemPropertyAssetURL中,始终返回nil而非实际网址:

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

所有歌曲都在设备上(由iTunes同步),包含歌词(显示在系统播放器中)和非DRM保护   (翻录aac / mp3)。

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

有关我如何获取歌词或MPMediaItemPropertyAssetURL为何返回nil的任何建议?

2 个答案:

答案 0 :(得分:1)

我不知道为什么它不起作用,但看起来现在相同的代码工作正常。也许它以某种方式连接到我现在用于播放器实例的单例。这是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
    }
}

答案 1 :(得分:0)

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

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

import AVFoundation

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

请注意,它返回一个可选值,因此最好检查nil。希望这可以节省某人的研究时间。

请参阅Apple文档here