AVPlayer在iOS10.3 replaceCurrentItem 崩溃问题的解决办法
AVPlayer在实现连续播放使用replaceCurrentItem来切换音频的时候,在iOS10.3上会出现闪退。
self.player.replaceCurrentItem(with: playerItem)
错误内容如下
invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.
解决办法移除CurrentItem的监听,重新添加即可
private func currentItemAddObserver(){
addObserver = true
//监控状态属性,注意AVPlayer也有一个status属性,通过监控它的status也可以获得播放状态
player.currentItem?.addObserver(self, forKeyPath: "status", options: .new , context: nil)
//监控缓冲加载情况属性
player.currentItem?.addObserver(self, forKeyPath: "loadedTimeRanges", options: NSKeyValueObservingOptions.new, context: nil)
//监控播放完成通知
NotificationCenter.default.addObserver(self, selector: #selector(playbackFinished(noti:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
//监控时间进度
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()
网友评论