iOS 8 - 关闭全屏 AVPlayerViewController 时将方向更改回纵向

Posted

技术标签:

【中文标题】iOS 8 - 关闭全屏 AVPlayerViewController 时将方向更改回纵向【英文标题】:iOS 8 - change orientation back to portrait when dismissing full screen AVPlayerViewController 【发布时间】:2014-12-22 00:38:01 【问题描述】:

我的应用是仅纵向应用,但我有一个使用 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
            )
        )
    

问题:当我打开播放器的全屏视图,然后更改为横向,然后单击“完成”以关闭全屏视图时,我的应用程序仍处于横向状态。但我希望它再次旋转到纵向。我该怎么做?

【问题讨论】:

你是用你的AVFullScreenViewController 类继承AVPlayerViewController 吗? @LaurentRivard 不,AVFullScreenViewController 类是 AVKit 等价于 MPInlineVideoFullscreenViewController 运行时类。 【参考方案1】:

不要实现application(_:supportedInterfaceOrientationsForWindow:),而是尝试在每个视图控制器上实现supportedInterfaceOrientations()。比如:

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

这将确保视图控制器不能以横向显示,因此当关闭视频播放器时,它将直接回到纵向窗口。

更新 Objective-C:

- (NSUInteger)supportedInterfaceOrientations  
    return UIInterfaceOrientationMaskPortrait; 
 

【讨论】:

您能否为此答案添加目标 c?【参考方案2】:

在 Appdelegate 和所需的视图控制器中添加这些行

-(BOOL)shouldAutorotate

    return NO; 


-(NSUInteger)supportedInterfaceOrientations

    //LandScapeMode:- UIInterfaceOrientationMaskLandscape;
    //PortraitMode:- 
    return UIInterfaceOrientationMaskPortrait 


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

    //LandScapeMode:- UIInterfaceOrientationLandscapeRight;
   // ProtraitMode:-
   return UIInterfaceOrientationPortrait

【讨论】:

【参考方案3】:

我做了什么来解决这个问题,创建一个具有“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")
        
    

【讨论】:

以上是关于iOS 8 - 关闭全屏 AVPlayerViewController 时将方向更改回纵向的主要内容,如果未能解决你的问题,请参考以下文章

iOS 8中的popover全屏

当 MPMoviePlayerController 退出全屏时 UINavigationBar 错位(ios 8 问题)

在iOS 8中如何在UIWebView Player中接收全屏模式的通知?

iOS 8:全屏 Safari 主屏幕页面底部的空白间隙

如何检测iOS离开全屏视频?

模态全屏 iOS 8 后视频无法在 mpmovieplayercontroller 上播放