Reputation: 7067
We have picture in picture working on an iPad Air 2 with our beta version of our app built with Xcode 7. The picture pops out, gets dragged around, pauses, etc correctly. Except we don't know how to make that first button work. I did not see this in the documentation. Inside our app, if you pop it out, you can pop it back in, but if you leave the app, and you hit that button, the video just stops. It does not take you back into our app like we see happen in Safari.
Is there documentation or any blogs that covers how that first button on the PIP should behave?
Upvotes: 1
Views: 5034
Reputation: 2224
If you using AVPlayerViewController
for playing video it's super easy, only you need to set delegate of AVPlayerViewController
to your viewController then implementing below function to restore dismissed playerViewController when app started pip mode.
func playerViewController(_ playerViewController: AVPlayerViewController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
// Present dismissed viewController again to continue playing in full screen mode
self.present(playerViewController, animated: true)
completionHandler(true)
}
Upvotes: 0
Reputation: 510
I've struggled with a similar issue, the difference was that I couldnt go back even in my app. What I've found out was that the PiP was killing the ViewController, which was starting the video player. The solution I came with was to keep a reference to that ViewController, so It can not be cleared from the memory. The second thing I needed to do was to make that ViewController a AVPlayerViewControllerDelegate and playerController.delegate = self
And after the delegation was set, I needed to listen for restoreUserInterfaceForPictureInPictureStopWithCompletionHandler
Where I said to the navigation controller to present the kept video player ViewController.
Upvotes: 4
Reputation: 4517
Did you set your project correctly?
Link on or after iOS 9.0.
In the Xcode Capabilities view for your project, select Audio and AirPlay in the Background Modes section.
Configure your audio session with an appropriate category, such as AVAudioSessionCategoryPlayback, If you have not already done so in your app.
Most important for you:
To respond to user interactions with the PiP window, implement the methods of the AVPlayerViewControllerDelegate protocol.
Having configured your project to support Picture in Picture, the player view controller then provides a PiP button for the user, along with the other playback controls.
Upvotes: 3