Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stanman64

macrumors 6502a
Original poster
Jul 9, 2010
793
873
I'm getting a weird bug with an AVPlayerViewController presented to full screen from inline/embedded state. The bug is triggered when you begin to swipe to dismiss out of full screen, but then cancel the dismissal (i.e. try to keep the video full screen). The video tries to stay full screen but then just goes black, and the whole app becomes unresponsive while the audio still plays.


I downloaded Apple's Using AVKit in iOS sample project after watching the Delivering Intuitive Media Playback with AVKit session from WWDC19. In their sample project, they have an AVPlayerViewController embedded inline in a cell, and when the user taps the expand arrows, it goes full screen. While in full screen, the user can pan the video around in any direction without issue. Completing the pan/swipe gesture in a downward motion completes the dismissal and the view dismisses from full screen.

In my project (Xcode 11.4) I'm embedding an AVPlayerViewController exactly how Apple does in their sample project, yet when I pan the video in full screen, the video goes black and the app has to be restarted. Interestingly, I rebuilt my own code inside Apple's demo Xcode project and everything works fine!

The sample project says it's supposed to be exactly for the 2019 WWDC session and is compatible with Xcode 11+, however under "Project Format" in general settings the sample project says it's Xcode 9.3 compatible, and it doesn't have a SceneDelegate which was introduced with iOS 13.

So in conclusion, could this be a bug with AVPlayerViewController and SceneDelegate? Or is there something I am missing?


Here's the exact ViewController I'm using that works in Apple's sample project, but does NOT work in Xcode 11.4:


Code:
class ViewController: UIViewController {
  
    let videoContainer = UIView()
    let padding: CGFloat = 20

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemGroupedBackground
        title = "Video"
        navigationController?.navigationBar.prefersLargeTitles = true
      
        configure()
        embedVideoInline(in: self, container: videoContainer)
    }
  
  
    private func configure() {
        videoContainer.backgroundColor = .secondarySystemGroupedBackground
        videoContainer.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(videoContainer)

        NSLayoutConstraint.activate([
            videoContainer.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: padding),
            videoContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: padding),
            videoContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -padding),
            videoContainer.heightAnchor.constraint(equalToConstant: ((view.frame.width - padding*2)/16) * 9)
        ])
    }
  
  
    func embedVideoInline(in parent: UIViewController, container: UIView) {
        let url: URL! = URL(string: "https://www.radiantmediaplayer.com/media/bbb-360p.mp4")
        let playerItem = AVPlayerItem(url: url)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = AVPlayer(playerItem: playerItem)
      
        parent.addChild(playerViewController)
        container.addSubview(playerViewController.view)
        playerViewController.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            playerViewController.view.centerXAnchor.constraint(equalTo: container.centerXAnchor),
            playerViewController.view.centerYAnchor.constraint(equalTo: container.centerYAnchor),
            playerViewController.view.widthAnchor.constraint(equalTo: container.widthAnchor),
            playerViewController.view.heightAnchor.constraint(equalTo: container.heightAnchor)
        ])
        playerViewController.didMove(toParent: parent)
    }
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.