Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect fullscreen mode of AVPlayerViewController

How can I detect when the user press the expand icon of the AVPlayerViewController? I want to know when the movie playing is entering the fullscreen mode.

like image 984
sverin Avatar asked Oct 12 '14 21:10

sverin


2 Answers

It is also possible to observe bounds of playerViewController.contentOverlayView and compare that to [UIScreen mainScreen].bounds, e.g.:

self.playerViewController = [AVPlayerViewController new];
// do this after adding player VC as a child VC or in completion block of -presentViewController:animated:completion:
[self.playerViewController.contentOverlayView addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];

...

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, id> *)change context:(void *)context {
    if (object == self.playerViewController.contentOverlayView) {
        if ([keyPath isEqualToString:@"bounds"]) {
            CGRect oldBounds = [change[NSKeyValueChangeOldKey] CGRectValue], newBounds = [change[NSKeyValueChangeNewKey] CGRectValue];
            BOOL wasFullscreen = CGRectEqualToRect(oldBounds, [UIScreen mainScreen].bounds), isFullscreen = CGRectEqualToRect(newBounds, [UIScreen mainScreen].bounds);
            if (isFullscreen && !wasFullscreen) {
                if (CGRectEqualToRect(oldBounds, CGRectMake(0, 0, newBounds.size.height, newBounds.size.width))) {
                    NSLog(@"rotated fullscreen");
                }
                else {
                    NSLog(@"entered fullscreen");
                }
            }
            else if (!isFullscreen && wasFullscreen) {
                NSLog(@"exited fullscreen");
            }
        }
    }
}
like image 189
kambala Avatar answered Oct 08 '22 06:10

kambala


You can use KVO to observe the videoBounds property of your AVPlayerViewController instance.

Edit The most basic example being

[_myPlayerViewController addObserver:self forKeyPath:@"videoBounds" options:0 context:nil];
like image 43
ChrisH Avatar answered Oct 08 '22 08:10

ChrisH