简体   繁体   English

AVPlayerViewController 从全屏返回后停止

[英]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情况下设置AVPlayerViewControllerplayer属性,并且只在之后设置它(依赖于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初始化AVPlayerViewControllerAVPlayer ,然后才将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.

相关问题 AVPlayerViewController确实退出了全屏 - AVPlayerViewController did exit full screen AVPlayerViewController全屏按钮动作? - AVPlayerViewController full screen button Action? 在 AVPlayerViewController, Swift 中将 AVPlayer 设置为全屏视图 - Make AVPlayer to full screen view in AVPlayerViewController, Swift 当 AVPlayerViewController 全屏时处理 - Handle when AVPlayerViewController goes full screen iOS 8 - 在关闭全屏AVPlayerViewController时将方向改回肖像 - iOS 8 - change orientation back to portrait when dismissing full screen AVPlayerViewController AVPlayerViewController 仅纵向应用程序中的全屏旋转行为 - AVPlayerViewController full screen rotation behavior in portrait only app 有没有一种方法可以防止AVPlayerViewController通过MPNowPlayingInfoCenter更新锁定屏幕? - Is there a way to prevent AVPlayerViewController from updating the lock screen via MPNowPlayingInfoCenter? 从后台返回后,Alamofire进度回调停止工作 - Alamofire progress callback stops working after returning from background 从背景返回后,MPMoviePlayerViewController黑屏 - MPMoviePlayerViewController black screen after returning from background 从全屏视频返回后,导航工具栏垂直延伸 - Navigation toolbar extends vertically after returned from full screen video
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM