AVPlayer, notification for play/pause state?

后端 未结 5 795
离开以前
离开以前 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:37

        player = AVPlayer(url: URL(fileURLWithPath: path))
    player.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.new, context: nil)
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "rate" {
            if player.rate > 0 {
                print("video started")
            }
        }
    }
    

    in swift

提交回复
热议问题