我的代码在iOS 12及更低版本上完美运行:
fileprivate func setupRemoteControl() {
UIApplication.shared.beginReceivingRemoteControlEvents()
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.player.play()
self.playPause.setImage(#imageLiteral(resourceName: "PausePlayer"), for: .normal)
self.miniPlayPauseButton.setImage(#imageLiteral(resourceName: "PausePlayerMini"), for: .normal)
self.setupElapsedTime()
MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1
return .success
}
commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.player.pause()
self.playPause.setImage(#imageLiteral(resourceName: "PlayPlayer"), for: .normal)
self.miniPlayPauseButton.setImage(#imageLiteral(resourceName: "PlayPlayerMini"), for: .normal)
self.setupElapsedTime()
MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0
return .success
}
commandCenter.togglePlayPauseCommand.isEnabled = true
commandCenter.togglePlayPauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
self.handlePlayPause()
return .success
}
commandCenter.skipForwardCommand.addTarget(self, action: #selector(handleSkipForward))
commandCenter.skipBackwardCommand.addTarget(self, action: #selector(handleSkipBackward))
}
@objc fileprivate func handleSkipForward() {
print("Seek 15 sec forward")
let fifteenSeconds = CMTimeMakeWithSeconds(15, Int32(NSEC_PER_SEC))
let seekTime = CMTimeAdd(player.currentTime(), fifteenSeconds)
let elapsedTime = CMTimeGetSeconds(seekTime)
MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = elapsedTime
player.seek(to: seekTime)
}
@objc fileprivate func handleSkipBackward() {
print("Seek 15 sec backward")
let fifteenSeconds = CMTimeMakeWithSeconds(-15, Int32(NSEC_PER_SEC))
let seekTime = CMTimeAdd(player.currentTime(), fifteenSeconds)
let elapsedTime = CMTimeGetSeconds(seekTime)
MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = elapsedTime
player.seek(to: seekTime)
}
但是,当我尝试在iOS 13模拟器上运行时,AppDelegate文件出错导致应用程序崩溃:
“线程1:异常:”不支持的操作方法签名。必须返回MPRemoteCommandHandlerStatus或采用完成处理程序作为第二个参数。“
怎么了?