AVPlayer, notification for play/pause state?

后端 未结 5 809
离开以前
离开以前 2021-01-31 15:45

I\'m searching for a way to get notified the exact moment when AVPlayer starts playing. There\'s the \"rate\" property, but currently I am checking it periodically

5条回答
  •  面向向阳花
    2021-01-31 16:18

    1)Add below (observer)code to your avPlayer Object

    self.player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
    

    2)And below delegate for that

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
            if (keyPath == "rate") {
                if ((player?.rate) == 1) {
                    print("Plyaing")
    
                } else {
                    print("Pause")
                }
            }
        }
    

提交回复
热议问题