AVPlayer在iOS10.3 replaceCurrentItem 崩潰問(wèn)題的解決辦法
AVPlayer在實(shí)現(xiàn)連續(xù)播放使用replaceCurrentItem來(lái)切換音頻的時(shí)候碌宴,在iOS10.3上會(huì)出現(xiàn)閃退掠归。
self.player.replaceCurrentItem(with: playerItem)
錯(cuò)誤內(nèi)容如下
invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.
解決辦法移除CurrentItem的監(jiān)聽(tīng),重新添加即可
private func currentItemAddObserver(){
addObserver = true
//監(jiān)控狀態(tài)屬性美旧,注意AVPlayer也有一個(gè)status屬性鼎文,通過(guò)監(jiān)控它的status也可以獲得播放狀態(tài)
player.currentItem?.addObserver(self, forKeyPath: "status", options: .new , context: nil)
//監(jiān)控緩沖加載情況屬性
player.currentItem?.addObserver(self, forKeyPath: "loadedTimeRanges", options: NSKeyValueObservingOptions.new, context: nil)
//監(jiān)控播放完成通知
NotificationCenter.default.addObserver(self, selector: #selector(playbackFinished(noti:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
//監(jiān)控時(shí)間進(jìn)度
timeObserver = player.addPeriodicTimeObserver(forInterval: CMTime.init(value: 1, timescale: 1), queue: DispatchQueue.main, using: { (time : CMTime) in
})
}
private func currentItemRemoveObserver(){
if addObserver {
addObserver = false
guard let currentItem = player.currentItem else { return }
currentItem.removeObserver(self, forKeyPath: "status")
currentItem.removeObserver(self, forKeyPath: "loadedTimeRanges")
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
if let observer = self.timeObserver {
player.removeTimeObserver(self.timeObserver)
}
}
}
currentItemRemoveObserver()
self.player.replaceCurrentItem(with: playerItem)
if #available(iOS 10.0, *) {
self.player.automaticallyWaitsToMinimizeStalling = false
}
currentItemAddObserver()