[英]AVPlayerViewController stops after returning from full screen
I have an AVPlayerViewController
which I initialize with an AVPlayer
and some AVPlayerItem
(iOS 10, Xcode 8, Objective C).我有一个
AVPlayerViewController
,我用一个AVPlayer
和一些AVPlayerItem
(iOS 10、Xcode 8,Objective C)初始化它。 The AVPlayerViewController
is presented "inline" inside some subview, and everything works perfectly with the native playback controls. AVPlayerViewController
在一些子视图中“内联”呈现,并且一切都与本机播放控件完美配合。
When I press the native fullscreen button, it also works ok and switches to full screen mode (with Done button on top left).当我按下本机全屏按钮时,它也可以正常工作并切换到全屏模式(左上角有完成按钮)。
My problem is when I press the Done button to return from full screen, the player for some reason stops playing, resets itself, and if I check .currentItem
, I see it's nil
.我的问题是当我按下 Done 按钮从全屏返回时,播放器出于某种原因停止播放,自行重置,如果我检查
.currentItem
,我会看到它是nil
。
What's happening here?这里发生了什么事? Why can't
AVPlayerViewController
maintain its AVPlayerItem
in between switching from/to full screen?为什么
AVPlayerViewController
不能在从全屏切换到全屏之间保持其AVPlayerItem
?
Since it looks like the current behavior of AVPlayerViewController
is to pause when exiting full screen, we can call play()
when exiting by implementing the delegate:由于看起来
AVPlayerViewController
的当前行为是退出全屏时暂停,因此我们可以通过实现委托在退出时调用play()
:
class VideoView {
private var playerViewController: AVPlayerViewController?
func something() {
playerViewController = AVPlayerViewController()
// Other setups
playerViewController?.delegate = self
}
}
extension VideoView: AVPlayerViewControllerDelegate {
func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
// The system pauses when returning from full screen, we need to 'resume' manually.
coordinator.animate(alongsideTransition: nil) { transitionContext in
self.playerViewController?.player.play()
}
}
}
Following on from the above answer https://stackoverflow.com/a/58818395/196555 and the comment AVPlayerViewController stops after returning from full screen继上述回答https://stackoverflow.com/a/58818395/196555 之后,评论AVPlayerViewController 在全屏返回后停止
I found using this extension works if you want to know if the AVPlayerViewController
is playing or not如果你想知道
AVPlayerViewController
是否正在播放,我发现使用这个扩展是有效的
extension AVPlayer {
var isPlaying: Bool {
rate != 0 && error == nil
}
}
@objc func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
let isPlaying = self.playerViewController.player?.isPlaying ?? false
// The system pauses when returning from full screen, we need to 'resume' manually.
coordinator.animate(alongsideTransition: nil) { _ in
if isPlaying {
self.playerViewController.player?.play()
}
}
}
Using @matt's reference, I saw that what I'd done different was to set the AVPlayerViewController
's player
property without an AVPlayerItem
, and only setting it afterwards (relying on replaceCurrentItemWithPlayerItem()
.使用@matt 的参考,我看到我所做的不同是在没有
AVPlayerItem
情况下设置AVPlayerViewController
的player
属性,并且只在之后设置它(依赖于replaceCurrentItemWithPlayerItem()
。
In other words - from my experience, you should initialize both the AVPlayerViewController
AND the AVPlayer
with URL
or any AVPlayerItem
and only then adding the AVPlayerViewController
as a child view controller.换句话说 - 根据我的经验,您应该使用
URL
或任何AVPlayerItem
初始化AVPlayerViewController
和AVPlayer
,然后才将AVPlayerViewController
添加为子视图控制器。
Code for using without AutoLayout:不使用 AutoLayout 使用的代码:
if let playerView = self.playerView {
let playerItem = AVPlayerItem(url: self.url)
let player = AVPlayer(playerItem: playerItem)
let playerVc = AVPlayerViewController()
playerVc.player = player
self.addChildViewController(playerVc)
playerVc.view.frame = playerView.bounds
playerView.addSubview(playerVc.view)
playerVc.didMove(toParentViewController: self)
player.play()
}
The above solution will always play the video on exit full screen, even if it is paused.上述解决方案将始终在退出全屏时播放视频,即使暂停也是如此。
Add this to fix it:添加这个来修复它:
func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
let 获取播放状态 = self.parent.player.isPlaying
// The system pauses when returning from full screen, we need to 'resume' manually.
coordinator.animate(alongsideTransition: nil) { transitionContext in
if 获取播放状态 {
self.parent.player.play()
}
}
}
And和
extension AVPlayer {
var isPlaying: Bool {
return rate != 0 && error == nil
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.