var player: AVPlayer?
func setupPlayerView(_ urlString: String) {
if let url = URL(string: urlString) {
player = AVPlayer(url: url)
let playerLayer = AVPlayerLayer(player: player)
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.frame
player?.play()
}
}
player.addObserver(self, forKeyPath: "status", options: .new, context: nil)
// catch changes to status
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "status" {
// do something
}
}