【发布时间】:2016-04-14 19:20:49
【问题描述】:
我正在尝试制作一个表格视图,它可以使用AVPlayer 和AVPlayerItem 播放多个视频,并且我需要将Observer 添加到每个AVPlayerItem 以便我可以跟踪playbackLikelyToKeepUp 属性
我尝试但失败的是在设置 AVPlayerItem 并在 UITableViewCell 的 deinit 中删除它之后添加观察者,但由于单元格永远不会被解除分配但会被出队,所以这不起作用,我会得到这个错误
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.'
如果有人知道如何实现这一点,我会很高兴知道?谢谢
【问题讨论】:
-
子类化
AVPlayerItem并在其中添加/删除观察者呢? -
看起来很有趣,我试试看
-
Awesome 效果很好,非常感谢:D
标签: ios swift avfoundation avplayerlayer avplayeritem