AVPlayerviewcontroller ios objective c

AVPlayerviewcontroller ios objective c

我有一个针对 ipad iphone 的应用程序,ipad 用户始终启用横向模式,iphone 用户始终启用纵向模式,现在我正在尝试实现是在 iphone 应用程序中我使用 AVPlayerViewController 播放视频但是由于应用程序在 appdelegate 中被锁定为纵向,当我按下播放器上的全屏按钮时它只会保持纵向模式我想制作它景观 我尝试了我在 Whosebug 中找到的所有答案,但没有任何关于如何使其工作的想法?

在您的 AppDelegate.m 文件中添加以下代码。

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
        }
    }
}

我用 NSTimer 解决了这个问题我做了一个每秒都会调用的方法我不知道这不是好的做法但我就是这样做的:

    [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];
}   }

_fullscreen 是一个标志,当 AVplayer 进入全屏视图或普通视图时会发生变化。

为了跟踪 AVplayer 状态,我使用 Observer @"bounds" 来检查 AVPlayer 屏幕的大小。

- (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];

            }
        }
    }
}}