How to handle PiP & Fullscreen with AVPlayerViewController correctly?

I get this warning whenever I enter/exit Fullscreen or enter/exit PiP on the iPad.

AVPlayerViewController is trying to enter full screen, but is not in its view's window's view controller hierarchy. This results in undefined behavior.

I already watched https://developer.apple.com/videos/play/wwdc2019/503/ more than twice, but still don't get it. btw, why is there no tutorial/documentation on this important stuff? I mean, which developer playing videos does NOT want to go fullscreen or PiP if possible??

This is how I setup my player. I add the view of the playerViewController to an already existing UIView sitting in my normal UI.

Code Block
// configure video player
  self.playerViewController = [[AVPlayerViewController alloc] init];
  playerViewController.delegate = self;
  playerViewController.showsPlaybackControls = YES;
  [playerViewController setPlayer:[AVPlayer playerWithURL:bundleBasedVideoURL]];
  playerViewController.player.muted = YES;
  playerViewController.player.allowsExternalPlayback = YES;
  playerViewController.entersFullScreenWhenPlaybackBegins = NO;
  playerViewController.exitsFullScreenWhenPlaybackEnds = YES;
  playerViewController.allowsPictureInPicturePlayback = YES;
  playerViewController.canStartPictureInPictureAutomaticallyFromInline = YES;
  [self.placeholderView addSubview:playerViewController.view];
  [playerViewController didMoveToParentViewController:self];
  [playerViewController.view setFrame:self.placeholderView.bounds];
  LOG( @"STATUS: %i", (int)[self.playerViewController.player status] );


I guess I need to somehow take action on the called delegate methods, but what and where exactly?? I want to get rid of this message and be a good video-playing-dev-citizen.

Any help on this would be nice.


Ahhh... found out why all this happened.

I added one important line to my setup of the video player:

Code Block
[[self rootViewController] addChildViewController:playerViewController];


where rootViewController comes from:

Code Block
[[UIApplication sharedApplication].windows firstObject].rootViewController;

AFAIK I do not even need to take care of more stuff in the delegate's calls. I just have the playerViewController property retain it and when I dismiss my view containing the playerview, I just should not forget to remove the playerViewController from the rootViewController. For that I added a convenience method to my viewController which presents my view including the player's view.

Code Block
- (void) viewShouldTearDownVideo {
  [self timerStopVideoCheck];
  [self.playerViewController removeFromParentViewController];
  self.playerViewController.delegate = nil;
}


This does seem to do the trick.

Bonus: You do not even see the PiP-Symbol on the player if you do NOT provide the background-audio-key in the Info.plist.

And, yeah, I love Obj-C. :)



How to handle PiP & Fullscreen with AVPlayerViewController correctly?
 
 
Q