我有一个ipad iphone的应用程序,ipad用户总是启用横向模式和iphone用户的肖像,现在我想要实现的是在iphone应用程序中我使用AVPlayerViewController播放视频但是因为应用程序被锁定在肖像里面appdelegate,当我按下播放器上的全屏按钮时它将保持opn肖像模式我想让它成为风景我尝试了所有我在stackoverflow中找到的答案,但没有运气任何想法如何让它工作?
在 AppDelegate.m 文件中添加以下代码 .
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进入全屏视图或普通视图时会改变 .
为了跟踪AV播放器状态,我使用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]; } } } }}
2 回答
在
AppDelegate.m
文件中添加以下代码 .Objective C
Swift
我用NSTimer解决了这个问题我做了一个方法,每隔一段时间就会调用一次我知道不是很好的做法,但这就是我所做的:
_fullscreen是一个标志,当AVplayer进入全屏视图或普通视图时会改变 .
为了跟踪AV播放器状态,我使用Observer @“bounds”来检查AVPlayer屏幕的大小 .