1

首先要感谢 Google Developers 让将嵌入式播放器添加到 iOS 项目变得如此容易。我的问题是如何改变方向。

最初我只在常规设置中将我的项目设置为肖像。这适用于所有视图控制器,但它也适用于 YouTube 播放器。视频按预期播放,但是当我旋转手机时,视频保持纵向。

如果我返回常规设置并将其设置为横向,则播放器会正确旋转(但项目中的所有视图控制器也会正确旋转)。

我在这里阅读了有关如何为 1 个特定控制器释放方向的其他答案,但我只想在全屏观看视频时旋转工作。如果用户决定只观看我创建的 UIView 中的视频,那么我希望锁定方向。

我已按照https://developers.google.com/youtube/v3/guides/ios_youtube_helper上的说明进行操作,因此我需要添加的唯一代码是:

[self.playerView loadWithVideoId:videoSourceID];

就像我说的那样,视频播放没有问题,将视频设为全屏也没有问题,只是我关心的方向。

TLDR:所以基本上,如果用户正在全屏观看 YouTube 视频,我只想解锁“旋转到横向”,应用程序中的所有其他内容都应该锁定为纵向(包括 UIView 显示窗口视频的视图控制器)。感谢您的关注!

4

2 回答 2

1

将此代码粘贴到您的 AppDelegate.m 上:

- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
// Handling UITabBarController
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
    UITabBarController* tabBarController = (UITabBarController*)rootViewController;
    return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
}
// Handling UINavigationController
else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
    UINavigationController* navigationController = (UINavigationController*)rootViewController;
    return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
}
// Handling Modal views
else if (rootViewController.presentedViewController) {
    UIViewController* presentedViewController = rootViewController.presentedViewController;
    return [self topViewControllerWithRootViewController:presentedViewController];
}
// Handling UIViewController's added as subviews to some other views.
else 
{
    for (UIView *view in [rootViewController.view subviews])
    {
        id subViewController = [view nextResponder];    // Key property which most of us are unaware of / rarely use.
        if ( subViewController && [subViewController isKindOfClass:[UIViewController class]])
        {
            return [self topViewControllerWithRootViewController:subViewController];
        }
    }
    return rootViewController;
    }
}

在以下方法中,您可以决定每个 UIViewController 的方向。把它放在你 AppDelegate.m

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

id presentedViewController = [self topViewController];
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;

if (window && [className isEqualToString:@"FullScreenViewController"]) { //FullScreenViewController should support all orientations. Put the exact name of the viewcontroller displayed when the video is fullscreen

    return UIInterfaceOrientationMaskAll;
} else {
    return UIInterfaceOrientationMaskPortrait; //Every other viewcontroller will support portrait only
}
}

您必须检测视频全屏时显示的 UIViewController 的确切名称。您必须在堆栈跟踪中找到它,并将其名称而不是我作为示例编写的“FullScreenViewController”

于 2016-01-29T17:13:30.087 回答
0

斯威夫特 3.0

class AppDelegate
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        guard UIApplication.currentVC.isMovie else { return application.supportedInterfaceOrientations(for: window) }
        return UIInterfaceOrientationMask.allButUpsideDown
    }
}


extension UIApplication {
    struct currentVC {
        static var isMovie: Bool {
            guard let presentedViewController = UIApplication.shared.keyWindow?.rootViewController?.presentedViewController else { return false }

            let className = String(describing: type(of: presentedViewController))

            return ["MPInlineVideoFullscreenViewController",
                    "MPMoviePlayerViewController",
                    "AVFullScreenViewController"].contains(className)
        }
    }
}


import AVKit
extension AVPlayerViewController {
    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        UIDevice.current.setValue(UIInterfaceOrientationMask.portrait.rawValue, forKey: "orientation")
    }
}
于 2017-09-08T11:25:52.083 回答