简体   繁体   中英

AVPlayerviewcontroller ios objective c

我有一个适用于两个ipad iphone的应用程序,ipad用户始终启用横向模式,而iphone用户则为纵向模式,现在我要实现的目标是在iphone应用程序中,我使用AVPlayerViewController播放视频,但由于该应用程序锁定在纵向模式下, appdelegate,当我按播放器上的全屏按钮时,它将保持纵向操作模式。我想使其横向显示,我尝试了我在stackoverflow中找到的所有答案,但是对如何使其正常运行没有任何想法?

Add below code in your AppDelegate.m file.

Objective C

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
#define supportedInterfaceOrientationsReturnType NSUInteger
#else
#define supportedInterfaceOrientationsReturnType UIInterfaceOrientationMask
#endif

- (supportedInterfaceOrientationsReturnType)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)windowx
{
    if ([[self.window.rootViewController presentedViewController] isKindOfClass:[AVPlayerViewController class]]
        )
    {
        if ([self.window.rootViewController presentedViewController].isBeingDismissed)
        {
            return UIInterfaceOrientationMaskPortrait;
        }else{
            return UIInterfaceOrientationMaskAll;
        }
    }
}

Swift

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
let supportedInterfaceOrientationsReturnType = NSUInteger
#else
let supportedInterfaceOrientationsReturnType = .mask
#endif

func application(_ application: UIApplication, supportedInterfaceOrientationsFor windowx: UIWindow) -> supportedInterfaceOrientationsReturnType {
    if (self.window!.rootViewController!.presented! is AVPlayerViewController) {
        if self.window!.rootViewController!.presented!.isBeingDismissed() {
            return .portrait
        }
        else {
            return .all
        }
    }
}

I solved this problem with an NSTimer I made a Method that will be called every second I know not good practice but this is what I did:

    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(yourMethod) userInfo:nil repeats:YES];

-(void)yourMethod{
if(!_fullscreen){
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}else{
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}   }

the _fullscreen is a flag that will change when the AVplayer goes to fullscreen view or normal view.

and to track the AVplayer states I used an Observer @"bounds" to check what size the AVPlayer screen is.

- (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");

                if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){

                }else{
                    _fullscreen = YES;
                    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
                }
            }
        }
        else if (!isFullscreen && wasFullscreen) {
            NSLog(@"exited fullscreen");
            if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){

            }else{
                _fullscreen = NO;
                [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

            }
        }
    }
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM