在AVPlayerLayer和AVPlayerViewController中同步视频 [英] Sync video in AVPlayerLayer and AVPlayerViewController

查看:113
本文介绍了在AVPlayerLayer和AVPlayerViewController中同步视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working with AVPlayerto show the playing video using a URL in it. There are 2 parts to it:

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

1. Firstly, I've embedded the AVPlayer to a view's sublayer using AVPlayerLayer, i.e.

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()
    }
}

上面的代码可以正常工作,并且正在播放视频.

The above code is working fine and the video is playing.

2.其次,在UIButton拍子上,我使用与我先前创建的相同的AVPlayer实例(即

2. Secondly, on a UIButton tap, I'm presenting a AVPlayerViewController using the same AVPlayer instance that I created earlier, i.e.

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

    }
}

我在这里面临的问题是AVPlayerViewController打开后,视频停止播放,但音频仍在播放.

The problem I'm facing here is, after the AVPlayerViewController opens, the video stops playing but the audio still plays on.

我想要的是在AVPlayerLayerAVPlayerViewController同步视频.

推荐答案

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

I think there is a problem when sharing a player already created to the AVPlayerViewController. I'm not sure why is stoping but it wont happen if you create a new AVPlayer for that controller. A way to sync your player and your AVPlayerViewController could be like this:

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

First, you create a Notification Name that you'll use when the AVPlayerViewController is dismiss (apple does not give you a way to know when the user dismiss the AVPlayerViewController):

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

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

Then, you extend AVPlayerViewController to post this notification when is going to be dismiss and to send the time when the user left the video:

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:

And in your ViewController you observe that notification, get the seekTime you want to go and use it to setup your 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作为观察者.希望能对您有所帮助.

Cons: It will send the notification for every AVPlayerViewController. You use add you as an observer when you need this info. Hope it can help.

这篇关于在AVPlayerLayer和AVPlayerViewController中同步视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆