【问题标题】:IOS6 landscape playing embedded youtube video from a uiwebview within an only portrait iPhone appIOS6 横向在仅纵向 iPhone 应用程序中从 uiwebview 播放嵌入的 youtube 视频
【发布时间】:2013-07-14 21:20:30
【问题描述】:

我有一个 iPhone 应用程序,带有情节提要、少量 xib 和自定义单元格。

应用程序被设置为“纵向”作为“支持的界面方向”(我的意思是一切都是这样显示的)。 在我的自定义单元格中,有一个链接到 youtube 嵌入式视频的 Uiwebview,当我单击它时,视频开始播放,但我的问题是它们总是以“纵向”模式播放。 我已经阅读了很多解决此问题的内容,但仅在 ios5 中。

其实: 我可以识别视频何时开始或停止播放。 我可以识别设备方向。 但是我不能(我想要)将(强制)方向从纵向切换到横向,或者如果用户改变他的设备的方向,也不能提出这种能力。

提前致谢。

PS:如果需要,我可以显示用于识别应用程序的代码。

【问题讨论】:

    标签: ios6 uiwebview youtube landscape


    【解决方案1】:

    我接受了 Almas Adilbek 的回答(做得非常好!)并将其归结为它的基本组成部分。仅此代码(添加到我的应用程序委托中)似乎正在为我获得所需的结果。如果我遇到任何问题会更新。

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    
        id presentedViewController = [window.rootViewController presentedViewController];
        NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
    
        if (window && [className isEqualToString:@"MPInlineVideoFullscreenViewController"]) {
            return UIInterfaceOrientationMaskAll;
        } else {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    

    【讨论】:

    • 太棒了!谢谢你:)
    • 工作正常,除非视频嵌入在模式中并且在横向关闭视频时存在小错误。如果这样做,则界面为横向。要更正它,请添加以下测试:[presentedViewController isBeingDismissed] == NO
    • 谢谢答案。但它不适用于我在 iOS7 上。 className 始终是 RootViewController。任何想法 ?我将使用“isPlayingYoubeVideo”标志,但这不是很漂亮......
    • 在 iOS 8 上检查 AVFullScreenViewController 而不是 MPInlineVideoFullscreenViewController
    • 在 iOS 8 上我得到了 nil presentViewController。知道为什么吗?
    【解决方案2】:

    我有同样的问题。我的解决方案是:

    1.首先在你的xcode项目中开启所有方向:

    2.在AppDelegate.m中添加:

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        NSArray *stackViewControllers = self.navigationController.viewControllers;
        UIViewController *rvc = [stackViewControllers objectAtIndex:stackViewControllers.count - 1];
    
        if([rvc isKindOfClass:[VideoViewController class]])
        {
            id presentedViewController = [rvc presentedViewController];
    
            NSString *viewControllerName = NSStringFromClass([presentedViewController class]);
            if([viewControllerName isEqual:@"MPInlineVideoFullscreenViewController"] && [VideoViewController isVideoPlaying]) {
                return UIInterfaceOrientationMaskAll;
            }
        }
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
    }
    

    在上面的代码中,每次系统询问 supportedInterfaceOrientationsForWindow 时,您都会检查当前的 viewController 是否是您放置 UIWebView 并嵌​​入了 youtube 播放器的位置,并检查现在是否正在播放视频(即视频是全屏模式)。
    注意:我使用UINavigationController,如果不使用,则必须进行一些更改才能获得当前 viewController。

    3.在 viewController 中,我为 youtube 嵌入式播放器放置了 UIWebView(在我的情况下是 VideoViewController),在它的头文件中添加方法:

    +(BOOL)isVideoPlaying;
    


    4.在VideoViewController.m添加静态变量:

    static BOOL _isVideoPlaying = NO;
    


    5.在viewDidLoad中添加通知的addObserver,以便知道视频何时开始播放和将退出播放

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerWillExitFullscreen:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
    


    6.另外,添加通知选择器方法:

    -(void)playerStarted:(NSNotification *)notification{
        _isVideoPlaying = YES;
    }
    -(void)playerWillExitFullscreen:(NSNotification *)notification {
        _isVideoPlaying = NO;
    
        if([AppUtils iOSVersion] < 6) //For iOS < 6.0, you must manually rotate viewController's view when fullscreen video playing is dismissed.
        {
            if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)
            {
                self.navigationController.view.userInteractionEnabled = NO;
                [UIView animateWithDuration:0.5 animations:^{
                    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft  animated:NO];
                    // rotate main view, in this sample the view of navigation controller is the root view in main window
                    [self.navigationController.view setTransform: CGAffineTransformMakeRotation(180 * M_PI * 0.5)];
                    // set size of view
                    [self.navigationController.view setFrame:CGRectMake(0, 0, 320, 960)];
                } completion:^(BOOL finished) {
                    self.navigationController.view.userInteractionEnabled = YES;
                }];
            }
        }
    }
    


    7.并且,在VideoViewController.m中添加方法:

    +(BOOL)isVideoPlaying {
        return _isVideoPlaying;
    }
    
    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        if(!isVideoPlaying) {
            return toInterfaceOrientation != UIInterfaceOrientationLandscapeLeft && toInterfaceOrientation != UIInterfaceOrientationLandscapeRight;
        }
        return YES;
    }
    


    所有这些技巧对我都很有效,支持 iOS 5 及更高版本。
    希望它对你有用!

    【讨论】:

    • 非常感谢,它有效。如果需要,我可以为任何人提供资源。
    • @user2587950 你能把这个演示项目发给我吗?我出错了?我不知道如何解决它。
    猜你喜欢
    • 2013-03-23
    • 2011-09-08
    • 2011-05-27
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多