I want to display lyrics that are currently being played by an iOS 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?
To get the text from the nowPlaying element, I 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 the following code, MPMediaItemPropertyAssetURL always returns nil instead of the 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 on the device (synchronized with iTunes) contain lyrics (displayed in the system player) and are not DRM protected (ripped aac / mp3).
I am testing this on a real device: iPhone 6s / iOS 10.3
Any suggestions on how I can get the lyrics or why does MPMediaItemPropertyAssetURL return nil?
source share