Since iOS 10 you can observe timeControlStatus
property of AVPlayer. It can be .playing
.
Check the code:
private func setupAVPlayer() {
avPlayer.addObserver(self, forKeyPath: "status", options: [.old, .new], context: nil)
if #available(iOS 10.0, *) {
avPlayer.addObserver(self, forKeyPath: "timeControlStatus", options: [.old, .new], context: nil)
} else {
avPlayer.addObserver(self, forKeyPath: "rate", options: [.old, .new], context: nil)
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if object as AnyObject? === avPlayer {
if keyPath == "status" {
if avPlayer.status == .readyToPlay {
avPlayer.play()
}
} else if keyPath == "timeControlStatus" {
if #available(iOS 10.0, *) {
if avPlayer.timeControlStatus == .playing {
videoCell?.muteButton.isHidden = false
} else {
videoCell?.muteButton.isHidden = true
}
}
} else if keyPath == "rate" {
if avPlayer.rate > 0 {
videoCell?.muteButton.isHidden = false
} else {
videoCell?.muteButton.isHidden = true
}
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…