I am trying to add an observer to my reusable cell, and the problem is that it adds multiple observers. So I am wondering if there is any way around it because I really need this observer.
var player: AVPlayer?
var post: Post? {
didSet {
updateView()
}
}
Pretty much post is an array of videos and this gets called every time a post is set as a row on the table view.
This is why I was not able to add the observer in this method because it would be setting multiple observers.
func updateView() {
if let videoUrlString = post?.videoURL {
let videoUrl = URL(string: videoUrlString)
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()
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: nil, using: { (_) in
DispatchQueue.main.async {
self.player?.seek(to: kCMTimeZero)
self.player?.play()
}
})
}
self.updateLike(post: self.post!)
}
So then i tried setting it in the awakeFromNib() method.
override func awakeFromNib() {
super.awakeFromNib()
player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)
}
But it wasn't working because the player hadn't been completely initialized yet.
This is my observer function:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
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)"
}
}
}
How i remove the observer:
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "videoPostCell", for: indexPath) as! HomeTableViewCell
cell.playerLayer.removeFromSuperlayer()
cell.player?.pause()
cell.player?.isMuted = true
cell.delegate = self
cell.player?.removeObserver(self, forKeyPath: "currentItem.loadedTimeRanges")
}