我正在尝试制作一个播放多个视频的表格视图,AVPlayer
并且AVPlayerItem
我需要为每个视频添加观察者,AVPlayerItem
以便我可以跟踪 playLikelyToKeepUp 属性
我尝试并失败的是在设置AVPlayerItem
和删除它之后添加观察者deinit
,UITableViewCell
但是由于单元格永远不会被释放但会被出队所以这不起作用,我会得到这个错误
An instance 0x14eedebc0 of class AVPlayerItem was
deallocated while key value observers were still registered with it.
搜索后我想出了这个
- 我不应该添加或删除观察者,
UITableViewCell
但我必须这样做,因为播放器项目是在单元子类中制作的 - 处理观察者的最佳方式是在“UITableViewDelegate”方法中
- 添加
willDisplayCell
和删除didEndDisplayingCell
但即使这在我的情况下也不起作用,因为AVPlayerItem
需要时间来初始化
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
cell.setUpPLayer()
return cell
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let cell = cell as! TableViewCell
if cell.Player == nil {
self.addObserversToCell(cell)
}
}
override func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let cell = cell as! TableViewCell
self.removeMyObserversFromCell(cell)
}
所以观察者不会被添加,willDisplayCell
但删除观察者将被调用,并会导致运行时错误
'Cannot remove an observer <AVFPlayer.TableViewCell 0x13cf1e9b0> for
the key path "playbackLikelyToKeepUp"
<AVPlayerItem0x13cf31860> because it is not registered as an observer.'
如果有人知道如何做到这一点,我会很高兴知道?谢谢