在UIWebView视频播放期间允许UIInterfaceOrientationLandscape

时间:2013-06-19 01:37:19

标签: iphone objective-c uiwebview

我的iPhone应用程序是仅限纵向导向的应用程序,在我的应用程序中,我的UITableViewUIWebView中有UITableCellUIWebView显示嵌入的YouTube视频。当我点击视频播放时,它进入全屏模式。我需要做的是,允许用户旋转他们的设备并以横向模式播放视频。然后,当视频停止时,只允许再次使用肖像。我设置为在视频进入全屏并且全屏显示时收听通知。但我不知道如何以编程方式允许用户旋转界面方向。

所以基本上我在传递通知时调用了2个方法

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];

-(void)youTubeStarted:(NSNotification *)notification{
    // Entered fullscreen code goes here.
}

-(void)youTubeFinished:(NSNotification *)notification{
    // Left fullscreen code goes here.
}

我会在这两种方法中放置什么才能在视频播放过程中改变方向?

3 个答案:

答案 0 :(得分:19)

我明白了。在我的两种方法中:

-(void)youTubeStarted:(NSNotification *)notification{
   // Entered Fullscreen code goes here..
   AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
   appDelegate.fullScreenVideoIsPlaying = YES;
}

-(void)youTubeFinished:(NSNotification *)notification{
   // Left fullscreen code goes here...
   AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
   appDelegate.fullScreenVideoIsPlaying = NO;

   //CODE BELOW FORCES APP BACK TO PORTRAIT ORIENTATION ONCE YOU LEAVE VIDEO.
   [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    //present/dismiss viewcontroller in order to activate rotating.
    UIViewController *mVC = [[UIViewController alloc] init];
    [self presentModalViewController:mVC animated:NO];
    [self dismissModalViewControllerAnimated:NO];
}

我通过设置AppDelegate的BOOL属性在上述方法中访问了我的App委托。然后我在AppDelegate.m中调用了下面的应用程序方法:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if (self.fullScreenVideoIsPlaying == YES) {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
else {
    if(self.window.rootViewController){
        UIViewController *presentedViewController = [[(UINavigationController  *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presentedViewController supportedInterfaceOrientations];

    }
    return orientations;
}
}

self.fullScreenVideoIsPlaying是我在AppDelegate.h文件中设置为属性的BOOL

我希望这可以帮助别人在我失去的5个小时内搞清楚。

答案 1 :(得分:2)

如果您只想全屏支持横向播放。从iOS 8开始,来自UIWebView的视频在AVPlayerViewController内播放。

在AppDelegate中检查呈现窗口是否包含AVPlayerViewController

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if let w = window, let root = w.rootViewController {

        if root.childViewControllers.first is AVPlayerViewController {  
            return UIInterfaceOrientationMask.allButUpsideDown
        }
    }

    return UIInterfaceOrientationMask.portrait
}

答案 2 :(得分:0)

您可能拥有一个全局状态变量,可能在您的应用委托或存储应该可全局访问的变量的任何位置,然后根据电影播放器​​设置为truefalse是全屏还是不全屏。在调用旋转应用程序的回调中,您可以检查变量的状态,并根据返回情况确定是否允许旋转。