iOS 8 - 当退出全屏 AVPlayerViewController 时将方向更改回竖屏

16

我的应用程序是一个只支持竖屏的应用程序,但我有一个视图控制器,它显示一个使用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 - Laurent Rivard
@LaurentRivard 不,AVFullScreenViewController 类是 AVKit 中 MPInlineVideoFullscreenViewController 运行时类的等价物。 - flo_23
3个回答

1

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

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

这将确保视图控制器不能在横屏下显示,因此当关闭视频播放器时,其会直接回到纵向窗口。 更新 Objective-C:
- (NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskPortrait; 
} 

请问您能否为这个答案添加Objective-C代码? - Ibdakine

1
我为了解决这个问题,创建了一个新的类,并添加了一个名为 '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")
        }
    }
}

0

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

-(BOOL)shouldAutorotate
{
    return NO; 
}

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接