iOS picture in picture can't start if lazy load
I'm working on video playback app which needs to support PIP(picture in picture). The first problem is once app has instantiated a PIP controller with a player layer, it will auto enter PIP mode if user is playing video and press home button. Is that default behaviour? I can see some statement in Apple API document https://developer.apple.com/library/prerelease/ios/documentation/WindowsViews/Conceptual/AdoptingMultitaskingOniPad/QuickStartForPictureInPicture.html but I want to confirm.
If you support PiP this way but want to opt out of PiP for a particular video, do not associate that video’s AVPlayerLayer with an AVPictureInPictureController object. As soon as you instantiate a Picture in Picture controller with a player layer, you make the player layer’s video eligible for PiP; the way to opt out is to not perform that instantiation.
Another thing I find is if app initials PIP controller as soon as player view created(AVPlayerLayer as layer class), the video not start to play the PIP controller can function well, but if instantiate a PIP controller when video is playing and user click PIP button. The pictureInPicturePossible value will be no and can't start PIP. And the delegate method won't be invoked.
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error;
Does anyone have idea about this? Thanks
Yes, if your app instantiates AVPictureInPictureController with the AVPlayerLayer at the beginning, it will automatically go to picture-in-picture mode when the user presses the Home button. But this is only possible if you enable 'Allow Audio,Airplay and Picture in Picture' in 'Background Modes' under your App's Capabilities view.
Also for Picture in picture behavior to be consistent, we have to initialize at beginning and not in between. It's handy to implement the delegate methods to understand when the player is going in and out of PiP mode.
- pictureInPictureControllerWillStopPictureInPicture: (https://developer.apple.com/library/ios/documentation/AVKit/Reference/AVPictureInPictureControllerDelegate_Protocol)
To disallow a video from having PiP mode and if you use AVKit, just set its allowsPictureInPicturePlayback to NO.