如何识别 AVPlayer 处于暂停状态并且用户正在清理

How to recognize AVPlayer is in paused state and user doing scrubbing

您好,我正在开发包含本地视频流的应用程序。当用户在暂停状态下擦洗时,我正在搜索回调或通知。我已经为 AVPlayer 添加了观察者,如下所示:

[self.avPlayerController.player addObserver:self forKeyPath:@"status" options:0 context:NULL];
[self.avPlayerController.player addObserver:self forKeyPath:@"rate" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionOld context:NULL];

但是当播放器暂停和用户擦洗时不会调用观察者。我只想检测用户何时完成擦洗或何时触摸进度条。任何帮助将不胜感激。谢谢

观察 ratestatus 应该有效 - 在我的代码中调用了观察回调,因此我假设它们也适用于您。我想问题在于它们仅在视频暂停时才会被调用,而不是在用户擦洗视频时被调用。所以我会回答这个问题。

您可能想要观察的是 currentTime。我自己尝试过,当我尝试观察 AVPlayer 或其 currentItem 时,都不会调用 KVO。我做了一些研究,看来这根本行不通(请参阅 this SO question). You have to find another way to do it. One way is to add a timer and periodically check the state of it. Combined with observing rate or timeControlStatus (see this answer),即使视频暂停,您也应该能够分辨出 currentTime 正在被用户更改。

希望您不介意阅读Swift代码,这是第一种方法:

class ViewController: UIViewController {

    let player = AVPlayerViewController()
    var timer: Timer? = nil
    var previousTime: Double = 0
    var stopped = false

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        present(player, animated: true, completion: {
            self.player.player = AVPlayer(url: URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!)
            self.player.player?.play()
            self.player.player?.addObserver(self, forKeyPath: "rate", options: .new, context: nil)
            self.timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { (timer) in
                let currentTime = self.player.player?.currentTime().seconds ?? 0
                if self.previousTime != currentTime && self.stopped {
                    print(">>> User scrubbing")
                }
                self.previousTime = currentTime
            })
        })
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "rate" && (change?[NSKeyValueChangeKey.newKey] as? Float) == 0 {
            print(">>> stopped")
            stopped = true
        } else if keyPath == "rate" && (change?[NSKeyValueChangeKey.newKey] as? Float) == 1 {
            print(">>> played again")
            stopped = false
        }
    }
}

第二种方法,我更喜欢,是子类化 AVPlayer 并重写它的 seek(to:toleranceBefore:toleranceAfter:completionHandler:) 方法。当用户擦洗视频时,AVPlayerViewController 调用此方法 - 因此此时您可以知道用户正在擦洗(尽管在播放视频时用户擦洗它时也会调用它,因此可能需要测试如果它正在播放也是如此。

class CustomAVPlayer: AVPlayer {
    override func seek(to time: CMTime, toleranceBefore: CMTime, toleranceAfter: CMTime, completionHandler: @escaping (Bool) -> Void) {
        super.seek(to: time, toleranceBefore: toleranceBefore, toleranceAfter: toleranceAfter, completionHandler: completionHandler)

        // if you don't call this method directly, it's called only by the
        // AVPlayerViewController's slider during scrubbing
        print(">> user scrubbing")
    }
}