2014-12-22 27 views
12

我的應用程序是一個只有肖像的應用程序,但我有一個視圖控制器使用AVPlayerViewController顯示實時流。iOS 8 - 在解除全屏後將方向改回到人像AVPlayerViewController

,以便對播放器的全屏視圖景觀我在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) 
} 

這是我如何調用初始化我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 
      }) 
     }) 
    } 
} 

問題是:當我打開播放器的全屏視圖時,然後切換到橫向,然後單擊「完成」以關閉全屏視圖,我的應用仍處於橫向模式。但我希望它再次旋轉到肖像。我怎樣才能做到這一點?

+0

你是用'AVFullScreenViewController'類繼承'AVPlayerViewController'嗎? –

+0

@LaurentRivard不,「AVFullScreenViewController」類是與「MPInlineVideoFullscreenViewController」運行時類相當的AVKit類。 –

回答

0

而不是實施application(_:supportedInterfaceOrientationsForWindow:)嘗試實施supportedInterfaceOrientations()每個視圖控制器。因此,例如:

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

這將確保解僱的視頻播放器,所以當視圖控制器不能顯示在景觀,它會直接回到畫像窗口。

更新的Objective-C:

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

你能否爲此答案添加目標c? – Ibdakine

+1

這不適合我。 – LightningStryk

0

添加在AppDelegate中和您所需的視圖這些線路控制器

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

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

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

我做了什麼來解決這個問題,創建一個新的類'viewDidLayoutSubviews'功能並覆蓋它!

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") 
     } 
    } 
} 
相關問題