How to make a AVPlayerViewController go to fullscreen programmatically?

前端 未结 9 1151
既然无缘
既然无缘 2020-12-29 20:12

I\'m trying to make a AVPlayerViewController go to full screen mode programmatically, coming from \"embedded\" mode, however this does not appear to be possible with the pub

9条回答
  •  醉话见心
    2020-12-29 20:44

    Updated for iOS 11

    There is no supported way to programmatically go fullscreen with AVPlayerViewController (a bit of an oversight in my opinion).

    However, AVPlayerViewController does contain a private method that does exactly that. You'll have to decide for yourself whether you'd want to use it or not given you're not supposed to call private methods.

    AVPlayerViewController+Fullscreen.h

    #import 
    
    @interface AVPlayerViewController (Fullscreen)
    
    -(void)goFullscreen;
    
    @end
    

    AVPlayerViewController+Fullscreen.m

    #import "AVPlayerViewController+Fullscreen.h"
    
    @implementation AVPlayerViewController (Fullscreen)
    
    -(void)goFullscreen {
        NSString *selectorForFullscreen = @"transitionToFullScreenViewControllerAnimated:completionHandler:";
        if (@available(iOS 11.3, *)) {
            selectorForFullscreen = @"transitionToFullScreenAnimated:interactive:completionHandler:";
        } else if (@available(iOS 11.0, *)) {
            selectorForFullscreen = @"transitionToFullScreenAnimated:completionHandler:";
        }
        SEL fsSelector = NSSelectorFromString([@"_" stringByAppendingString:selectorForFullscreen]);
        if ([self respondsToSelector:fsSelector]) {
            NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:fsSelector]];
            [inv setSelector:fsSelector];
            [inv setTarget:self];
    
            NSInteger index = 2; //arguments 0 and 1 are self and _cmd respectively, automatically set
            BOOL animated = YES;
            [inv setArgument:&(animated) atIndex:index];
            index++;
    
            if (@available(iOS 11.3, *)) {
                BOOL interactive = YES;
                [inv setArgument:&(interactive) atIndex:index]; //arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
                index++;
            }
    
            id completionBlock = nil;
            [inv setArgument:&(completionBlock) atIndex:index];
            [inv invoke];
        }
    }
    
    @end
    

提交回复
热议问题