I create an player and working fine in most part of times.
In some situations (that I didn't realized why) screen video stays black with a play button that does nothing.
I verified url and is ok, that's not the problem.
In my viewController I can call this block of code multiples times with different urls, that's why I 'restart' AVPlayerViewController.
// Create an var in class...
// ....
self.videoPlayerViewController?.player?.pause()
self.videoPlayerViewController = AVPlayerViewController()
self.videoPlayerViewController?.player = viewModel.avPlayer
if let avController = self.videoPlayerViewController {
self.add(avController, in: self.playerView)
avController.player?.play()
} else {
// Error
}
That's function add:
extension UIViewController {
func add(_ viewController: UIViewController, in view: UIView) {
viewController.view.frame = view.bounds
addChildViewController(viewController)
view.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
view.clipsToBounds = true
}
}
Someone knows what is wrong? Thanks in advance!!
答案 0 :(得分:1)
经过这么多时间..我找到了解决方案。
问题是我没有在AVPlayerController中清理AVPlayer。我还在DispachQueue中添加了New实例。
该新代码:
self.videoPlayerViewController?.player?.pause()
self.videoPlayerViewController?.player = nil
self.videoPlayerViewController = nil
self.videoPlayerViewController = AVPlayerViewController()
self.videoPlayerViewController?.player = viewModel.avPlayer
在viewController中添加后:
if let avController = self.videoPlayerViewController {
DispatchQueue.main.async { [weak self] in
if let strongSelf = self {
strongSelf.add(avController, in: strongSelf.playerView)
avController.player?.play()
}
}
} else {
// Error
}
我希望它可以帮助别人!!