我正在尝试制作一个播放多个视频的表格视图AVPlayer
and AVPlayerItem
我需要为每个添加观察者AVPlayerItem
这样我就可以跟踪playbackLikelyToKeepUp 属性
我尝试但失败的是在设置后添加观察者AVPlayerItem
并将其删除deinit
of the 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.'
如果有人知道如何实现这一目标,我会很高兴知道?谢谢