iosswiftswift3avfoundationmpmediaitem

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


I want to display lyrics from song that is currently playing by iOS system player.

Here is my custom player:

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
    }
}

To get lyrics from nowPlaying item I've tried 2 approaches and both of them always return nil.

This code always returns nil:

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

In following code MPMediaItemPropertyAssetURL always returns nil instead of actual 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).

I'm testing this on real device: iPhone 6s/iOS 10.3

Any suggestions how I can get lyrics or why MPMediaItemPropertyAssetURL returns nil?


Solution

  • I don't know why it was not working, but it looks like same code now works fine. Maybe it somehow connected to the singleton that I'm now using for player instance. Here is Swift 3 version that 100% working:

    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
        }
    }