gpt4 book ai didi

ios - 在 AVPlayerLayer 和 AVPlayerViewController 中同步视频

转载 作者:搜寻专家 更新时间:2023-10-31 22:09:34 25 4
gpt4 key购买 nike

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

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

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 时会出现问题。我不确定为什么会停止,但如果您为该 Controller 创建一个新的 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 发送通知。当您需要此信息时,您可以将您添加为观察员。希望它能有所帮助。

关于ios - 在 AVPlayerLayer 和 AVPlayerViewController 中同步视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53352205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com