首先赞赏谷歌开发者,以便将嵌入式播放器添加到iOS项目中。我的问题是如何改变方向。
最初我只在常规设置中将项目设置为纵向。这适用于所有视图控制器,但它也适用于YouTube播放器。视频按预期播放,但是当我旋转手机时,视频会保持纵向。
如果我返回常规设置并将其设置为横向,则播放器正确旋转(但项目中的所有视图控制器也是如此)。
我在这里已经阅读了有关如何释放1个特定控制器的方向的其他答案,但我只想在全屏观看视频时旋转。如果用户决定只是在我创建的UIView中观看视频,那么我希望锁定方向。
我已按照https://developers.google.com/youtube/v3/guides/ios_youtube_helper的说明进行操作,因此我需要添加的唯一代码是:
[self.playerView loadWithVideoId:videoSourceID];
就像我说的那样,视频播放没有任何问题,制作全屏视频并没有错,这只是我关注的方向。
TLDR: 所以基本上,如果用户正在全屏观看YouTube视频,我只希望“旋转到横向”解锁,应用程序中的其他所有内容都应锁定为纵向(包括UIView显示窗口视频的视图控制器)。谢谢你的期待!
答案 0 :(得分: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"我作为一个例子写的
答案 1 :(得分:0)
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")
}
}