2016-09-23 4 views
0

У меня есть приложение для ipad iphones, пользователей ipad всегда используется ландшафтный режим и портрет для пользователей iphone, теперь то, что я пытаюсь достичь, находится в приложении iphone. Я воспроизвожу видео с помощью AVPlayerViewController, но поскольку приложение заблокировано в портрете внутри appdelegate, когда я нажимаю кнопку полного экрана на проигрывателе, он просто останется в режиме портретной ориентации. Я хочу сделать это ландшафт. Я попробовал все ответы, которые я нашел в 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.

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