3
votes

I have the similar problem like:

I need to get lyrics for current playing song.

I get empty result if I simply try to get lyrics from a media item:

let musicPlayerController = MPMusicPlayerController.systemMusicPlayer

// ...

if let nowPlayingItem = musicPlayerController.nowPlayingItem {
    // We have empty string here
    if let lyrics = nowPlayingItem.lyrics {
        print("Lyrics: \(lyrics)")
    }
}

I've tried to get lyrics through asset url as suggested in answers above, but it does not work, asset url is always nil.

let musicPlayerController = MPMusicPlayerController.systemMusicPlayer

// ...

if let nowPlayingItem = musicPlayerController.nowPlayingItem {
    if let songUrl = nowPlayingItem.value(forProperty: MPMediaItemPropertyAssetURL) as? URL {
        print("song url: \(songUrl)")
        let songAsset = AVURLAsset(url: songUrl)

        if let lyricsFromAsset = songAsset.lyrics {
            print("Lyrics from asset: \(lyricsFromAsset)")
        }
    }
}

I've also tried without any success (also get nil and empty string accordingly):

print("asset url: \(nowPlayingItem.assetURL)")
print("lyrics from value for property: \(nowPlayingItem.value(forProperty: MPMediaItemPropertyLyrics))")

I know exactly that the song has lyrics because I see it in Apple Music app on iPhone for the song. I've tried to get lyrics for the song: Queen - Bohemian Rhapsody.

Can somebody help with getting lyrics for current playing song from apple music?

1
I faced the same issue you got any solution?Yogesh Patel

1 Answers

0
votes

Are we talking about local files here? I am not sure how it works with Apple Music files, but for local files this method has 100% success rate in retrieving lyrics.

extension MPMediaItem {
    func lyrics() -> String? {
        guard let url = assetURL else { return nil }
        let ass = AVAsset(url: url)
        return ass.lyrics
    }
}