如何在iOS10中获取Now Now歌曲的歌词(Swift 3) [英] How to get lyrics for Now Playing song in iOS10 (Swift 3)

查看:134
本文介绍了如何在iOS10中获取Now Now歌曲的歌词(Swift 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示iOS系统播放器当前正在播放的歌曲的歌词.

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

要从nowPlaying项目中获取歌词,我尝试了2种方法,并且两种方法总是返回nil.

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

此代码始终返回nil:

This code always returns nil:

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

在以下代码中,MPMediaItemPropertyAssetURL始终返回nil而不是实际URL:

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

所有歌曲均在设备上(由iTunes同步),包含歌词(在系统播放器中显示)和不受DRM保护的 (翻录的AAC/MP3).

All songs are on device (synced by iTunes), contain lyrics (displayed in system player) and non DRM-protected (ripped aac/mp3).

我正在真实设备(iPhone 6s/iOS 10.3)上对此进行测试

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

关于如何获取歌词或MPMediaItemPropertyAssetURL为什么返回nil的任何建议?

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

推荐答案

我不知道为什么它不起作用,但是看起来现在相同的代码可以正常工作.也许它以某种方式连接到我现在用于播放器实例的单例.这是100%可工作的Swift 3版本:

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

这篇关于如何在iOS10中获取Now Now歌曲的歌词(Swift 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆