在AVPlayerLayer和AVPlayerViewController中同步视频

时间:2018-11-17 14:35:26

标签: ios swift avplayer avplayerviewcontroller avplayerlayer

我正在与AVPlayer一起使用其中的URL播放正在播放的视频。它分为两部分:

1。。首先,我使用AVPlayerAVPlayerLayer嵌入到视图的子层,即

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轻拍上,我使用我之前创建的同一AVPlayerViewController实例(即

AVPlayer

我在这里遇到的问题是,@IBAction func onTapVideoButton(_ sender: UIButton) { self.player?.pause() let controller = AVPlayerViewController() controller.player = self.player self.present(controller, animated: true) { self.player?.play() } } 打开后,视频停止播放,但音频仍在播放。

我想要的是在AVPlayerViewControllerAVPlayerLayer同步视频

1 个答案:

答案 0 :(得分:5)

我认为与AVPlayerViewController共享已经创建的播放器时会出现问题。我不确定为什么要停止,但是如果为该控制器创建一个新的AVPlayer则不会发生。同步播放器和AVPlayerViewController的方法可能是这样的:

首先,您创建一个将在关闭AVPlayerViewController时使用的通知名称(Apple无法让您知道用户何时关闭AVPlayerViewController的方式):

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

然后,您扩展AVPlayerViewController以便在该通知将被撤消时发布此通知,并发送用户离开视频的时间:

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发送通知。当您需要此信息时,可以使用add you作为观察者。希望能有所帮助。