Allow MPMoviePlayerViewController to play in landscape while preserving presenting view controller's orientation

StackOverflow https://stackoverflow.com/questions/22920079

I have a MPMoviePlayerViewController (a subclass rather, XCDYouTubeVideoPlayerViewController) that I'm presenting with the following code

LPVideo *video = [_videos objectAtIndex: indexPath.row];
XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier: video.code];
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];

My issue is that while the entire app is locked in Portrait mode I still want the user to play videos in landscape, so I've put this in my AppDelegate

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
else
{
    return UIInterfaceOrientationMaskPortrait;
}
}

This works fine in allowing the user to watch the video in portrait orientation; however if the video player is dismissed in portrait mode the viewcontroller that presented has also switched to portrait, which is something I don't want happening.

I have tried all sorts of things, like implementing supportedInterfaceOrientations and shouldAutorotate in my main UINavigationController, but it doesn't even prevent landscape orientation.

I know this is possible because I've seen apps doing this, but I can't figure out how. I've seen some solutions that involve listening to orientation changes and applying transformations to the player view but it seems needlessly complicated.

I also tried "forcing" rotation upon return via viewWillAppear:animated but while the code is called it doesn't help.

I've tried both this

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *view = [window.subviews objectAtIndex:0];
[view removeFromSuperview];
[window addSubview:view];

And this

[UIViewController attemptRotationToDeviceOrientation];

Both achieve nothing.

有帮助吗?

解决方案 2

Question is not that clear, if you wanna play video in all orientations then use "UIInterfaceOrientationMaskAll" instead of "UIInterfaceOrientationMaskAllButUpsideDown". If it doesn't work then check if any of the libraries you are using, changing the orientation.

其他提示

I fixed the issue by checking isBeingDismissed:

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if ([[window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]] && ![[self.window.rootViewController presentedViewController] isBeingDismissed]) {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        } else {
            return UIInterfaceOrientationMaskPortrait;
        }
    }

but hey, I'm using Swift:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> UIInterfaceOrientationMask {
    //If the video is being presented, let the user change orientation, otherwise don't.
    if let presentedViewController = window.rootViewController?.presentedViewController? {
        if (presentedViewController.isKindOfClass(MPMoviePlayerViewController) && !presentedViewController.isBeingDismissed()) {
            return .AllButUpsideDown
        }
    }
    return .Portrait
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top