2014-12-22 15 views
12

Moja aplikacja jest aplikacją tylko portretową, ale mam jeden kontroler widoku, który wyświetla strumień na żywo, za pomocą kontrolera AVPlayerViewController.iOS 8 - zmiana orientacji z powrotem na portret przy wyłączaniu trybu pełnoekranowego AVPlayerViewController

Aby umożliwić krajobrazu dla widoku pełnoekranowym tego odtwarzacza Napisałem tę metodę w AppDelegate.swift:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int { 
    var orientation = UIInterfaceOrientationMask.Portrait 

    if let presentedController = window?.rootViewController?.presentedViewController? { 

     if presentedController.isKindOfClass(NSClassFromString("AVFullScreenViewController").self) { 
      orientation = .AllButUpsideDown 
     } else if let navController = presentedController as? UINavigationController { 
      if navController.topViewController.isKindOfClass(NSClassFromString("AVFullScreenViewController").self) { 
       orientation = .AllButUpsideDown 
      } 
     } 
    } 

    return Int(orientation.rawValue) 
} 

ten sposób wzywam zainicjować mój AVPlayer:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "showLiveStream" { 

     SVProgressHUD.show() 
     var queue: dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 

     dispatch_async(queue, { 
      let streamURL = NSURL(string: liveStreamURL) 
      let playerItem = AVPlayerItem(URL: streamURL) 
      let player = AVPlayer(playerItem: playerItem) 

      dispatch_async(dispatch_get_main_queue(), { 
       SVProgressHUD.dismiss() 
       var playerViewController = segue.destinationViewController as AVPlayerViewController 
       playerViewController.player = player 
      }) 
     }) 
    } 
} 

Problem: gdy otwieram pełny ekran odtwarzacza, a następnie przełączam na krajobraz, a następnie klikam "Gotowe", aby zamknąć widok pełnoekranowy, moja aplikacja pozostanie w krajobrazie. Ale chcę, żeby znów się obracał do portretu. Jak mogę to zrobić?

+0

Czy podklasy 'AVPlayerViewController' z klasy' AVFullScreenViewController'? –

+0

@LaurentRivard Nie, klasa 'AVFullScreenViewController' jest odpowiednikiem AVKit klasy wykonawczej' MPInlineVideoFullscreenController'. –

Odpowiedz

0

Zamiast implementować application(_:supportedInterfaceOrientationsForWindow:) spróbuj wdrożyć supportedInterfaceOrientations() na dla każdego kontrolera widoku. Tak więc na przykład:

override func supportedInterfaceOrientations() -> Int { 
    return Int(UIInterfaceOrientationMask.Portrait.rawValue) 
} 

To zapewni, że kontroler widok nie mogą być wyświetlane w krajobrazie, więc kiedy odwoływanie odtwarzacz wideo, będzie pochodzić prosto z powrotem do okna pionowej.

Aktualizacja Objective-C:

- (NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskPortrait; 
} 
+0

Czy możesz dodać cel c dla tej odpowiedzi? – Ibdakine

+1

to nie działa dla mnie. – LightningStryk

0

Dodaj te linie w Appdelegate i swojej Wymagane View Controller

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    //LandScapeMode:- UIInterfaceOrientationMaskLandscape; 
    //PortraitMode:- 
    return UIInterfaceOrientationMaskPortrait 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    //LandScapeMode:- UIInterfaceOrientationLandscapeRight; 
    // ProtraitMode:- 
    return UIInterfaceOrientationPortrait 
} 
0

co zrobiłem, aby rozwiązać ten problem, należy utworzyć nową klasę z funkcja "viewDidLayoutSubviews" i zastąp ją!

final class NewMoviePlayerViewController: AVPlayerViewController { 
    override func viewDidLayoutSubviews() { 
     super.viewDidLayoutSubviews() 

     if view.bounds == contentOverlayView?.bounds { 
      let value = UIInterfaceOrientation.portrait.rawValue 
      UIDevice.current.setValue(value, forKey: "orientation") 
     } 
    } 
}