IOS6风景在一个唯一的肖像iPhone应用程序中播放来自uiwebview的嵌入式YouTube视频

use*_*950 8 youtube landscape uiwebview ios6

我有一个带有故事板,少量xib和自定义单元格的iPhone应用程序.

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

实际上:我可以识别视频何时开始或停止播放.我可以识别设备方向.但我不能(我想)从纵向到横向切换(强制)方向,或者如果用户改变他的设备的方向则提出此能力.

提前致谢.

PS:如果需要,我可以显示应用程序标识内容的代码.

Ken*_*ker 24

我接受了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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在iOS 8上检查AVFullScreenViewController而不是MPInlineVideoFullscreenViewController (2认同)

Alm*_*bek 5

我有同样的问题.我的解决方案是: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;
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,每次系统都要求supportedInterfaceOrientationsForWindow,你检查当前的viewController是否是UIWebView嵌入youtube播放器的位置,并检查视频是否正在播放(即视频是全屏模式).
注:我用的UINavigationController,如果你不这样做,你必须做出一些改变来获得当前的viewController.

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

+(BOOL)isVideoPlaying;
Run Code Online (Sandbox Code Playgroud)


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

static BOOL _isVideoPlaying = NO;
Run Code Online (Sandbox Code Playgroud)


5.在viewDidLoad中添加addObserver以进行通知,以便了解视频何时开始播放和willExitPlaying:

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


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;
            }];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


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

+(BOOL)isVideoPlaying {
    return _isVideoPlaying;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if(!isVideoPlaying) {
        return toInterfaceOrientation != UIInterfaceOrientationLandscapeLeft && toInterfaceOrientation != UIInterfaceOrientationLandscapeRight;
    }
    return YES;
}
Run Code Online (Sandbox Code Playgroud)


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