由于未捕获的异常而终止应用 'NSInternalInconsistencyException',原因:'实例0x1702076f0 当键值观察者被释放时,AVPlayerItem类被解除分配 仍在注册。目前的观察信息: (
(。<)NSKeyValueObservance 0x174846360:观察者:0x174846210,关键路径: loadedTimeRanges,选项:上下文: 0x0,属性:0x1748462d0> )'
这是我将 currentItem.loadedTimeRanges 观察者添加到我的AVPlayer后收到的错误
player = AVPlayer(url: videoUrl!)
playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = postVideoView.frame
playerLayer.frame.size.width = UIScreen.main.bounds.width
self.postVideoView.layer.addSublayer(playerLayer)
player?.play()
player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: nil, using: { (_) in
DispatchQueue.main.async {
self.player?.seek(to: kCMTimeZero)
self.player?.play()
}
})
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
//this is when the player is ready and rendering frames
if keyPath == "currentItem.loadedTimeRanges" {
if let duration = player?.currentItem?.duration {
let seconds = CMTimeGetSeconds(duration)
let secondsText = Int(seconds) % 60
let minutesText = String(format: "%02d", Int(seconds) / 60)
videoLengthLabel.text = "\(minutesText):\(secondsText)"
}
}
}
答案 0 :(得分:1)
你在哪里打removeObserver
?目前看起来你的顶级代码是否会创建一个新玩家并添加一个观察者。如果调用两次,则第一个实例上的观察者仍然存在。因此,我希望以下行位于顶部:
self.player?.removeObserver(self)
如果没有被调用两次,代码中的其他地方是否会重新分配或取消分配self.player
?如果是这样,您应首先致电removeObserver
。
您每次都会向NotificationCenter添加新的观察者。这应该只在被添加另一个之前被调用一次或removeObserver
。
答案 1 :(得分:0)
您需要确保不要多次添加和删除观察者。所以最好的方法就是声明一个bool变量让我们说var isObserverAdded = false.
添加观察者时,如果未添加,请选中此标志
if !isObserverAdded {
//Add your observer
isObserverAdded = true//Set this to true
}
删除观察者时,如果添加了
,请检查标记if isObserverAdded {
//Add your observer
isObserverAdded = false//Set this to false
}
如果使用上述检查,则永远不会收到错误。