iOS MPMusicPlayerController 歌曲更改通知没有歌曲对象

iOS MPMusicPlayerController song changed notification does not have a song object with it

我正在开发一个应用程序来监听 MPMusicPlayerController 的歌曲变化。

为此,我添加了以下观察者:

NotificationCenter.default
    .addObserver(self,
                 selector: #selector(systemSongDidChange(_:)),
                 name: .MPMusicPlayerControllerNowPlayingItemDidChange,
                 object: nil)

问题是,当触发通知时,可以在 (notification?.object as? MPMusicPlayerController)!.nowPlayingItem 处找到的 nowPlayingItem 始终是 nil

我是不是做错了什么,或者是否必须执行一些特殊的技巧才能检索到实际的 nowPlayingItem

这里有一个更完整的代码:

// ...
init() {
    let systemPlayer = MPMusicPlayerController.systemMusicPlayer

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(systemSongDidChange(_:)),
                                           name: .MPMusicPlayerControllerNowPlayingItemDidChange,
                                           object: systemPlayer)

    player.beginGeneratingPlaybackNotifications()
}

private func systemSongDidChange(notification: Notification) {
    let currentSong = (notification.object as? MPMusicPlayerController)?.nowPlayingItem
    // `currentSong` is always `nil` =/
}
// ...

我用的播放器是苹果的音乐播放器。我不是在播放云端歌曲

您需要在通知中设置对象,才能获得 nowPlayingItem

代码应如下所示:

private let playerController = MPMusicPlayerController.applicationMusicPlayer

NotificationCenter.default.addObserver(
    self,
    selector: #selector(systemSongDidChange(_:)),
    name: .MPMusicPlayerControllerNowPlayingItemDidChange,
    object: playerController
)

然后您应该能够在您的 systemSongDidChange 函数中像这样访问 nowPlayingItem

func systemSongDidChange(_ notification: Notification) {
    guard let playerController = notification?.object as? MPMusicPlayerController else {
        return
    }
    let item = playerController.nowPlayingItem
}

我刚刚发现为什么 nowPlayingItem 总是 nil

似乎用户 必须 允许该应用程序访问 "Media & Apple Music"。如果未授予此访问权限,应用将无权获知系统播放器当前正在播放的内容。

可以通过以下方式申请此授权:

// if not yet given or requested
MPMediaLibrary.requestAuthorization { authorizationStatus in }

// if already requested and denied (will take user to the App Settings Page)
UIApplication.shared.openURL(URL(string:UIApplicationOpenSettingsURLString)!)