IOS6风景在一个唯一的肖像iPhone应用程序中播放来自uiwebview的嵌入式YouTube视频 [英] IOS6 landscape playing embedded youtube video from a uiwebview within an only portrait iPhone app

查看:122
本文介绍了IOS6风景在一个唯一的肖像iPhone应用程序中播放来自uiwebview的嵌入式YouTube视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got an iPhone application with a storyboard,few xib and custom cells.

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

The application is set as a "portrait" as "supported interface orientation" (i mean everything is display like that). In my custom cells, there is Uiwebview that is linked to a youtube embedded video, when i clicked it the video start to playing, but my problem is that they are always playing in "portrait" mode. I've read lots of things that solve this problem but only in ios5.

实际上:
我可以识别视频何时启动或停止播放。
我可以识别设备方向。
但是我不能(我想)从纵向到横向切换(强制)方向,或者如果用户改变他的设备的方向,则建议这种能力。

Actually : I can identify when the video start or stop playing. I can identify the device orientation. But i can't (I want) switch (Force) orientation from portrait to landscape, or propose this ability if the user change the orientation of his device.

提前致谢。

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

PS : I can display the code for the identifying stuff of the application, if needed.

推荐答案

我遇到了同样的问题。我的解决方案是:


1.首先打开xcode项目中的所有方向:

I had same problem. My solution is:

1.First of all turn on all orientations in your xcode project:

2.在 AppDelegate.m 添加:

2.In AppDelegate.m add:

-(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是否已经嵌入youtube播放器放置 UIWebView ,并检查视频是否正在播放(即视频是全屏模式)。

注意:我使用 UINavigationController ,如果不这样做,则必须进行一些更改才能获得当前 viewController。



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

In above code, everytime system asks for supportedInterfaceOrientationsForWindow you check if current viewController is that where you've put UIWebView with youtube player embedded and also check if video is playing now (namely video is in fullscreen mode).
NOTE: I use UINavigationController, if you don't, you must make some changes to get current viewController.

3.In viewController where I I've put UIWebView for youtube embedded player (In my case it is VideoViewController), in it's header file add method:

+(BOOL)isVideoPlaying;



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


4.In VideoViewController.m add static variable:

static BOOL _isVideoPlaying = NO;



5.在 viewDidLoad 中添加addObserver通知,以便了解视频开始播放 willExitPlaying


5.In viewDidLoad add addObserver for notifications, in order to know, when video started to play and willExitPlaying:

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



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


6. Also, add notification selector methods:

-(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及更高版本。

希望它适合您!


All this tricks works well for me, supporting iOS 5 and later.
Hope it works for you!

这篇关于IOS6风景在一个唯一的肖像iPhone应用程序中播放来自uiwebview的嵌入式YouTube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆