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

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? //If song changes func playingItemDidChange(notification: NSNotification) { nowPlaying = musicPlayer.nowPlayingItem } } 

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?

+5
source share
1 answer

I donโ€™t know why it didnโ€™t work, but it seems that the same code is now working fine. Maybe it is somehow related to the single that I now use for the player instance. Here is a version of Swift 3 that works 100%:

 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
source

Source: https://habr.com/ru/post/1266346/


All Articles