Picture in Picture(PiP) is one of killer feature in iOS 9.
Great article about multitasking
Related session : Introducing AVKit in iOS 9 WWDC14 , Mastering modern media playback
Deprecated APIs in iOS 9
MPMoviePlayerController
MPMoviePlayerViewController
Replaced by AVPlayerViewController
Support PiP frameworks
AVPlayerViewController
AVAudioSessionCategoryPlayback
allowsPictureinPicture
in AVPlayerViewController if you want to stop PiPAVFoundation
AVPictureInPictureController
for custom player controls// Check whether Picture in Picture is supported on device.
if AVPictureInPictureController.isPictureInPictureSupported() {
// Create Picture in Picture controller.
pipController = AVPictureInPictureController(playerLayer: playerLayer)!
// Set delegate.
pipController.delegate = self
}
//
// Find out whether Picture in Picture is possible.
let pipPossible = pipController.pictureInPicturePossible
// Enable/disable Picture in Picture button.
pipButton.enabled = pipPossible
//
func pipButtonTapped(sender: AnyObject?) {
// Make sure Picture in Picture is not already active.
if !pipController.pictureInPictureActive {
// Start Picture in Picture on button tap.
pipController.startPictureInPicture()
}
}
//Delegate
func pictureInPictureControllerDidStartPictureInPicture(pipController:
AVPictureInPictureController) {
// Dismiss modal video playback view controller.
dismissViewControllerAnimated(true, completion: nil)
}
func pictureInPictureController(pipController: AVPictureInPictureController,
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler
completionHandler: (Bool) -> Void) {
// Present video playback view controller again.
navigationController?.presentViewController(self, animated: true) {
// Don’t forget to call completion handler.
completionHandler(true)
}
}
func pictureInPictureControllerWillStartPictureInPicture(pipController: AVPictureInPictureController)
will be called when pip is about to startyfunc pictureInPictureControllerDidStopPictureInPicture(pipController:
AVPictureInPictureController)
Demo Project : AVFounationPiPPlayer
WKWebConfiguarion
to config allowsPictureinPicture
default is yessecondaryAudioShouldBeSilencedHint
to check if secondary audio shuld be silenced UIRequiresFullscreen = YES
if requires whole screen as the camera view finderstartViewCapture()
return value to see if video capturing is availableAVCaptureSessionWasInterruptedNotification
for interruptionAVCaptureSessionInterruptionReasonKey
would give VideoDeviceNotAvailableWithMultipleForegroundApps
if other app takes camera controlAVCaptureSession
will be resumes automaticallyAVCaptureSessionInterruptionEndedNotification
for listening the interruption is ended