在 AVPlayerLayer 和 AVPlayerViewController 中同步视频

Sync video in AVPlayerLayer and AVPlayerViewController

我正在与 AVPlayer 一起使用 URL 显示正在播放的视频。它有两个部分:

1. 首先,我使用 AVPlayerLayerAVPlayer 嵌入到视图的子层中,即

var player: AVPlayer?

func configure() {
    let urlString = "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
    if let url = URL(string: urlString) {
        self.player = AVPlayer(url: url)
        let playerLayer = AVPlayerLayer(player: self.player)
        playerLayer.frame = self.view.bounds
        self.view.layer.addSublayer(playerLayer)
        player.play()
    }
}

以上代码运行良好,视频正在播放。

2. 其次,在点击 UIButton 时,我将使用我创建的相同 AVPlayer 实例呈现 AVPlayerViewController较早,即

@IBAction func onTapVideoButton(_ sender: UIButton) {
    self.player?.pause()
    let controller = AVPlayerViewController()
    controller.player = self.player
    self.present(controller, animated: true) {
        self.player?.play()

    }
}

我遇到的问题是,AVPlayerViewController打开后,视频停止播放,但音频仍在播放。

我想要的是AVPlayerLayerAVPlayerViewController中同步视频

我认为将已创建的播放器共享到 AVPlayerViewController 时存在问题。我不确定为什么会停止,但如果您为该控制器创建一个新的 AVPlayer,它就不会发生。同步播放器和 AVPlayerViewController 的方法可能是这样的:

首先,您创建一个通知名称,您将在关闭 AVPlayerViewController 时使用它(Apple 不会让您知道用户何时关闭 AVPlayerViewController):

extension Notification.Name {
    static let avPlayerDidDismiss = Notification.Name("avPlayerDidDismiss")
}

然后,您将 AVPlayerViewController 扩展到 post 此通知何时会被关闭并发送用户离开视频的时间:

extension AVPlayerViewController {

    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if let seekTime = player?.currentTime() {
            let userInfo = ["seekTime": seekTime]
            NotificationCenter.default.post(name: .avPlayerDidDismiss, object: nil, userInfo: userInfo)
        }
    }

}

并且在您的 ViewController 中您观察到该通知,获取您想要的 seekTime 并使用它来设置您的 avPlayer:

class ViewController: UIViewController {
    var player: AVPlayer?

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        configure()
        NotificationCenter.default.addObserver(self, selector: #selector(avPlayerDidDismiss), name: .avPlayerDidDismiss, object: nil)
    }

    func configure() {
        self.player = getPlayer()
        let playerLayer = AVPlayerLayer(player: self.player)
        playerLayer.frame = self.view.bounds
        self.view.layer.addSublayer(playerLayer)
        self.player?.play()
    }

    @IBAction func onTapVideoButton(_ sender: UIButton) {
        self.player?.pause()
        let controllerPlayer = getPlayer()
        controllerPlayer.currentItem?.seek(to: self.player!.currentTime(), completionHandler: nil)
        let controller = AVPlayerViewController()
        controller.player = controllerPlayer
        self.present(controller, animated: true, completion: {
            controller.player?.play()
        })
    }

    func getPlayer() -> AVPlayer {
        let url = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!
        return AVPlayer(url: url)
    }

    @objc
    private func avPlayerDidDismiss(_ notification: Notification) {
        if let seekTime = notification.userInfo?["seekTime"] as? CMTime {
            player?.currentItem?.seek(to: seekTime, completionHandler: nil)
            player?.play()
        }
    }
}

缺点:它将为每个 AVPlayerViewController 发送通知。当您需要此信息时,您可以将您添加为观察员。希望它能有所帮助。