user3758257
user3758257

Reputation: 69

iOS only video in landscape view

I'm developing a new app in xcode and the app is in portrait view, but the video should be able to be in portrait and landscape view. I programmed this code but it doesn't work 100%

AppDelegate.h

#import <MediaPlayer/MediaPlayer.h>
@property (strong, nonatomic) MPMoviePlayerViewController *VideoPlayer;

AppDelegate.m

@synthesize VideoPlayer;

- (NSUInteger)application:(UIApplication *)application
supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    if ([[self.window.rootViewController presentedViewController]
         isKindOfClass:[MPMoviePlayerViewController class]]) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else {

        if ([[self.window.rootViewController presentedViewController]
             isKindOfClass:[UINavigationController class]]) {

            // look for it inside UINavigationController
            UINavigationController *nc = (UINavigationController *)[self.window.rootViewController presentedViewController];

            // is at the top?
            if ([nc.topViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
                return UIInterfaceOrientationMaskAllButUpsideDown;

                // or it's presented from the top?
            } else if ([[nc.topViewController presentedViewController]
                        isKindOfClass:[MPMoviePlayerViewController class]]) {
                return UIInterfaceOrientationMaskAllButUpsideDown;
            }
        }
    }

    return UIInterfaceOrientationMaskPortrait;
}

The problem with this code is if user closes video player while it was watching video in landscape mode the whole app turns to landscape view even though I disabled it in Xcode GUI, after closing video player(app is in landscape view) if user rotate device to portrait it switches to portrait view and after that it stays in portrait (regardless device rotation). How can I make that app switches to portrait view even if user closes video player while he/she was watching video in landscape mode?

Thanks!

Upvotes: 2

Views: 1303

Answers (2)

John Pang
John Pang

Reputation: 2503

You almost got it right by adding the function in AppDelegate. The only problem is when user back from landscape video, the app become video. The solution is just here:

UIViewController *vc = [[self.window.rootViewController presentedViewController];
if ([vc isKindOfClass:[MPMoviePlayerViewController class]] &&
    ![vc isBeingDismissed]) {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

The key here is to double check if the presented view controller is being dismiss (exiting).

Upvotes: 0

l0gg3r
l0gg3r

Reputation: 8954

After long researches, I finally figured out the solution.

1) Enable All orientations for your application. enter image description here

2) Subclass your root Navigation controller, and implement this 2 methods

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

3) Subclass from MPMoviePlayerViewController

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

4) Now you should present subclassed MoviePlayerController, and all the stuff should work!

Upvotes: 1

Related Questions