首先要感谢谷歌开发者,让他们可以轻松地将嵌入式播放器添加到iOS项目中。 我的问题是如何改变方向。
最初,我仅在常规设置中将项目设置为"纵向"。 这适用于所有视图控制器,但它也适用于YouTube播放器。 视频按预期播放,但是当我旋转手机时,视频保持纵向。
如果我返回常规设置并将其设置为横向,则播放器将正确旋转(但项目中的所有视图控制器也是如此(。
我已经在这里阅读了有关如何释放 1 个特定控制器的方向的其他答案,但我只想在全屏观看视频时旋转工作。 如果用户决定只在我创建的 UIView 中观看视频,那么我希望锁定方向。
我已经按照 https://developers.google.com/youtube/v3/guides/ios_youtube_helper 的说明进行操作,因此除此之外我需要添加的唯一代码是:
[self.playerView loadWithVideoId:videoSourceID];
就像我说的,视频播放没有错,让视频全屏没有错,我关心的只是方向。
TLDR:所以基本上,如果用户全屏观看YouTube视频,我只想解锁"横向旋转",应用程序中的其他所有内容都应该锁定为纵向(包括UIView显示窗口视频的视图控制器(。 感谢您的观看!
将此代码粘贴到 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;
}
}
在以下方法中,您可以决定每个UIView控制器的方向。把它放在你的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
}
}
您必须检测视频全屏显示的UIView控制器的确切名称。您必须在堆栈跟踪中找到它,并输入它的名称而不是我作为示例编写的"FullScreenViewController">
Swift 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")
}
}