2016-09-23 49 views
0

我有一個應用程序的兩個ipad的iphones,ipad的用戶總是啓用橫向模式和iphone用戶的肖像,現在我想實現的是在iphone應用程序中使用AVPlayerViewController播放視頻,但自應用程序被鎖定在appdelegate裏面的肖像中,當我按下播放器上的全屏按鈕時,它將保持opn肖像模式我想使它成爲風景我嘗試了所有我在stackoverflow中找到的答案,但沒有運氣如何使任何想法這行得通 ?AVPlayerviewcontroller iOS目標C

+0

我的答案是否適合您? –

+0

不,它沒有。不管怎麼說,多謝拉。 –

回答

0

我解決了這個問題,一個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]; 

      } 
     } 
    } 
}} 
0

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

目標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; 
     } 
    } 
} 

斯威夫特

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