複数のビデオを再生するテーブル ビューを作成しようとしていますがAVPlayer
、AVPlayerItem
それぞれに Observer を追加してAVPlayerItem
、playbackLikelyToKeepUp プロパティを追跡できるようにする必要がありました
私が試して失敗したのは、を設定した後にオブザーバーを追加し、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.'
誰かがこれを達成する方法を知っていれば、喜んで教えてくれますか? ありがとう