我正在通过两个viewController显示MPRemoteCommandCenter
。
我正在处理播放,暂停,下一个,上一个动作以及在单个视图控制器中注册目标。
extension MediaViewController {
// Call in viewDidload
func setupNowPlayingInfoCenter() {
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try! AVAudioSession.sharedInstance().setActive(true)
deinitNowPlayInfoCenter()
MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(self.playPressed))
MPRemoteCommandCenter.shared().pauseCommand.addTarget(self, action: #selector(self.pausePressed))
MPRemoteCommandCenter.shared().nextTrackCommand.addTarget(self, action: #selector(self.nextPressed))
MPRemoteCommandCenter.shared().previousTrackCommand.addTarget(self, action: #selector(self.previousPressed))
MPRemoteCommandCenter.shared().skipForwardCommand.isEnabled = false
MPRemoteCommandCenter.shared().skipBackwardCommand.isEnabled = false
}
//Call this method when move to DownloadViewController.
func deinitNowPlayInfoCenter() {
MPRemoteCommandCenter.shared().playCommand.removeTarget(self)
MPRemoteCommandCenter.shared().pauseCommand.removeTarget(self)
MPRemoteCommandCenter.shared().nextTrackCommand.removeTarget(self)
MPRemoteCommandCenter.shared().previousTrackCommand.removeTarget(self)
}
private func updateNowPlayingInfoCenter(reset: Bool = false) {
if reset {
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
return
}
let mediaModal: MediaModel
if arrMedia.count > currentIndex, let player = self.playerViewController?.player {
mediaModal = arrMedia[currentIndex]
let nowPlayingInfo = [MPMediaItemPropertyTitle: mediaModal.media_title ?? "",
MPMediaItemPropertyAlbumTitle: mediaModal.event_type.name,
MPMediaItemPropertyArtist: mediaModal.mediaType ?? "",
MPNowPlayingInfoPropertyElapsedPlaybackTime: CMTimeGetSeconds(player.currentTime())
] as [String : Any]
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
}
}
与DownloadViewController相同
extension DownloadViewController {
//call in viewdidload.
func setupNowPlayingInfoCenter() {
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try! AVAudioSession.sharedInstance().setActive(true)
UIApplication.shared.endReceivingRemoteControlEvents()
UIApplication.shared.beginReceivingRemoteControlEvents()
self.deinitNowPlayInfoCenter()
MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(playPressed))
MPRemoteCommandCenter.shared().pauseCommand.addTarget(self, action: #selector(pausePressed))
MPRemoteCommandCenter.shared().nextTrackCommand.addTarget(self, action: #selector(nextPressed))
MPRemoteCommandCenter.shared().previousTrackCommand.addTarget(self, action: #selector(previousPressed))
MPRemoteCommandCenter.shared().skipForwardCommand.isEnabled = false
MPRemoteCommandCenter.shared().skipBackwardCommand.isEnabled = false
}
//call in viewWillDisappear
func deinitNowPlayInfoCenter() {
MPRemoteCommandCenter.shared().playCommand.removeTarget(self)
MPRemoteCommandCenter.shared().pauseCommand.removeTarget(self)
MPRemoteCommandCenter.shared().nextTrackCommand.removeTarget(self)
MPRemoteCommandCenter.shared().previousTrackCommand.removeTarget(self)
}
private func updateNowPlayingInfoCenter(reset: Bool = false) {
if reset {
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
return
}
let mediaModal: MediaModel
if arrMedia.count > currentIndex, let player = self.playerViewController?.player {
mediaModal = arrMedia[currentIndex]
let nowPlayingInfo = [MPMediaItemPropertyTitle: mediaModal.media_title ?? "",
MPMediaItemPropertyAlbumTitle: mediaModal.event_type.name,
MPMediaItemPropertyArtist: mediaModal.mediaType ?? "",
MPNowPlayingInfoPropertyElapsedPlaybackTime: CMTimeGetSeconds(player.currentTime())
] as [String : Any]
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
}
}
我正在处理受尊重的视图控制器(MPRemoteCommandCenter
通过其打开)中的操作。
除一个测试用例外,一切正常
第一次登录时,直接移至我的MediaViewController(在TabBarViewController中选择Media TabItem)并打开downloadViewController(从MediaViewController导航栏中的BarButtonItem)并播放歌曲(AvPlayerViewController),然后单击{{1}中的暂停按钮}。 MediaViewController的目标而不是DownloadViewController的目标被调用。我只在这个测试案例中发生。除了这种情况,它工作正常。如果用户注销并再次登录,它将再次生成。
如果用户登录并且比等待一段时间(30秒)或执行其他操作再多做一件事,那么它可以正常工作,但是如果用户直接进入mediaviewcontroller而没有花费一秒钟,那么它就会发生。
它仅在第一次发生。当用户登录后比一切正常。我认为
MPRemoteCommandCenter
开始加载应用程序时,不起作用。