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