如何让AVPlayer在暂停时重绘当前的AVItem videoComposition

问题描述 投票:1回答:1

我正在为macOS构建一个简单的视频编辑器:电影文件作为AVAsset加载,由AVVideoComposition中的一系列CIFilter转换,并由AVPlayer播放。我提供了CIFilters的一些参数的UI控件。

当视频播放时,一切都很好,我滑动滑块和效果改变!但是当视频暂停时,在UI中的控件发生更改后,AVPlayerView不会重绘。

我怎样才能鼓励AVPlayerView在暂停时重绘当前项目的videoComposition的内容?

class ViewController: NSViewController {

    @objc @IBAction func openDocument(_ file: Any) { ... }

    @IBOutlet weak var moviePlayerView: AVPlayerView!

    var ciContext:CIContext? = CIContext(mtlDevice: MTLCreateSystemDefaultDevice()!)

    var sliderValue: Double = 0.0

    @IBAction func sliderMoved(_ sender: NSSlider) {
        self.sliderValue = sender.doubleValue
        // need to update the view here when paused
        self.moviePlayerView.setNeedsDisplay(self.moviePlayerView.bounds)
        // setNeedsDisplay has no effect.
    }

    func loadMovie(file: URL) {
        let avMovie = AVMovie(url: file)
        let avPlayerItem = AVPlayerItem(asset: avMovie)

        avPlayerItem.videoComposition = AVVideoComposition(asset: avMovie) { request in
            let output = request.sourceImage.applyingGaussianBlur(sigma: self.sliderValue)
            request.finish(with: output, context: self.ciContext)
        }

        self.moviePlayerView.player = AVPlayer(playerItem: avPlayerItem)
    }
}
macos avfoundation avplayer core-image
1个回答
1
投票

事实证明重新设置AVPlayerItem的videoComposition属性将触发项目重绘。即使您将属性设置为它的当前值,这也适用:item.videoComposition = item.videoComposition。属性设置器似乎具有无证的副作用。

要修复上面的示例代码,请执行以下操作:

@IBAction func sliderMoved(_ sender: NSSlider) {
    self.sliderValue = sender.doubleValue
    // update the view when paused
    if self.moviePlayerView.player?.rate == 0.0 {
        self.moviePlayerView.player?.currentItem?.videoComposition = self.moviePlayerView.player?.currentItem?.videoComposition
    }
}

希望有人觉得这很有用!

© www.soinside.com 2019 - 2024. All rights reserved.