1

beginGeneratingPlaybackNotifications 似乎没有在我的应用程序中被调用。我可以获取 storeIds 并且正在播放音乐(我可以向前和向后跳过),但似乎从未调用过通知。有什么建议或有什么我想念的吗?

        let player = MPMusicPlayerController.applicationQueuePlayer

        // Trying all of these
        player.beginGeneratingPlaybackNotifications()
        MPMusicPlayerController.systemMusicPlayer.beginGeneratingPlaybackNotifications()
        MPMusicPlayerController.applicationMusicPlayer.beginGeneratingPlaybackNotifications()
        MPMusicPlayerController.applicationQueuePlayer.beginGeneratingPlaybackNotifications()

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(refreshView),
                                               name: .MPMusicPlayerControllerPlaybackStateDidChange,
                                               object: self)


        NotificationCenter.default.addObserver(self,
                                               selector: #selector(refreshView),
                                               name: .MPMusicPlayerControllerNowPlayingItemDidChange,
                                               object: self)

    let queue  = MPMusicPlayerStoreQueueDescriptor(storeIDs: storeIds)
    player.setQueue(with: queue)
    player.play()

    // Never called
    @objc func refreshView(){
      let nowPlaying = player.nowPlayingItem
4

1 回答 1

0

这些行是错误的:

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(refreshView),
                                           name: .MPMusicPlayerControllerPlaybackStateDidChange,
                                           object: self)


    NotificationCenter.default.addObserver(self,
                                           selector: #selector(refreshView),
                                           name: .MPMusicPlayerControllerNowPlayingItemDidChange,
                                           object: self)

改变

  object: self

  object: nil

同时。

于 2020-02-08T23:29:18.347 回答