MPNowPlayingInfoCenter踩坑
主要为音频播放设置锁屏界面
代码如下:
var info = [String:Any]() ///标题 info[MPMediaItemPropertyTitle] = self.currentAudioM?.title ?? " " ///作者 // info[MPMediaItemPropertyArtist] = "wizet" //相簿标题 // info[MPMediaItemPropertyAlbumTitle] = "相册标题" ///封面 // let artWork = MPMediaItemArtwork(boundsSize: image!.size, requestHandler: { (size) -> UIImage in return image! }) // info[MPMediaItemPropertyArtwork] = artWork //当前播放进度 (会被自动计算出来,自动计算与MPNowPlayingInfoPropertyPlaybackRate设置的速率正相关) let currentPlayDuration = TimeInterval(self.player.currentPlayDuration) info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentPlayDuration //调整外部显示的播放速率正常为1、一般都是根据内部播放器的播放速率作同步,一般不必修改 // info[MPNowPlayingInfoPropertyPlaybackRate] = NSNumber(value: 1) //播放总时间 由当前播放的资源提供 // let v1 = TimeInterval(self.player.duration-self.player.currentPlayDuration) let duration = TimeInterval(self.player.duration) info[MPMediaItemPropertyPlaybackDuration] = duration MPNowPlayingInfoCenter.default().nowPlayingInfo = info
坑1:希望播放结束后,锁屏控制界面消失
A: UIApplication.shared.endReceivingRemoteControlEvents()实现,实际会经常出现不能消失的问题,猜想:beginReceivingRemoteControlEvents和endReceivingRemoteControlEvents可能有对应,所以,在每次beginReceivingRemoteControlEvents操作前,调用endReceivingRemoteControlEvents。
代码如下
UIApplication.shared.endReceivingRemoteControlEvents()
UIApplication.shared.beginReceivingRemoteControlEvents()
self.setPlayerNowPlayingInfo()