12
votes

this question is only one part of my problem. I am implementing iOS6 rotation and orientation support for my existing application.

So I have a ViewController that contains a MPMoviePlayerController embedded in ViewController view ( my application requires it ). User can play the video and see it in the embedded view or click on full screen button using the default player controls and player goes to full screen mode.

Now I have restricted the view controller to only support portrait orientation using the new rotation APIs provided by iOS6.

// New Autorotation support.
- (BOOL)shouldAutorotate;
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

this works pretty well. the ViewController only supports portrait and user play the movie in embedded view.

Now the problem comes, when User goes into full screen mode. In full screen mode, the movie is keep on rotating, when i rotate the simulator/device. When i rotate the device while movie being played in full screen mode with breakpoints in shouldAutorotate and supportedInterfaceOrientations , it still comes in these both methods which return NO and UIInterfaceOrientationMaskPortrait respectively, but still the movie is rotating ...

Why is this happening? .... this is one part of my question ... the 2nd part is I want the movie to enter in landscape mode when the user goes to full-screen mode. and I want the movie player to lock in landscape mode until user presses the DONE button.

Please help ....

8

8 Answers

22
votes

you can try below function in AppDelegate:

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

}

you can make condition here for both mode.

such as if media player is in full screen then

return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;

otherwise return UIInterfaceOrientationMaskPortrait;

i have not tried it but i think, it should work in your case.

thanks

7
votes

For clarity, here is the complete code (it ALL goes inside your app delegate):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(willExitFullscreen:)
                                                 name:MPMoviePlayerWillExitFullscreenNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(willEnterFullscreen:)
                                             name:MPMoviePlayerWillEnterFullscreenNotification
                                           object:nil];
}

- (void)willEnterFullscreen:(NSNotification*)notification
{
    NSLog(@"willEnterFullscreen");
    isFullScreen = YES;
}

- (void)willExitFullscreen:(NSNotification*)notification
{
    NSLog(@"willExitFullscreen");
    isFullScreen = NO;
}

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (isFullScreen)
        return UIInterfaceOrientationMaskLandscapeLeft;
    else
        return UIInterfaceOrientationMaskPortrait;
}

isFullScreen is a BOOL to be declared in AppDelegate.h

1
votes

I would suggest to use a MPMoviePlayerViewController instead. Subclass it and implement the supportedInterfaceOrientations method and return UIInterfaceOrientationMaskLandscape.

You might also have to implement the shouldAutorotateToInterfaceOrientation: method.

See the class reference: MPMoviePlayerViewController

Edit: You might also take a look at this post: iphone - force MPMoviePlayerController to play video in landscape mode

1
votes

This consumed me for a while and I got so many different horrifying errors, but eventually I ended up not doing it through MPMoviePlayerController but MPMoviePlayerViewController. I just rotated the self.playerView which is a property, before presenting it. Also I added the NSNotification that will lead back to the main control and the main ViewController after the video finishes. Here's how I went about executing it:

        [[NSNotificationCenter defaultCenter] removeObserver:self.playerView
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:self.playerView.moviePlayer];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(movieFinishedCallback:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:self.playerView.moviePlayer];

        self.playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:docUrl];
        self.playerView.view.frame = CGRectMake(10, 10, self.frame.size.width-20, 180);
        [self.playerView.moviePlayer prepareToPlay];

        if(IS_IPHONE_6P)
        {
            [self.playerView.view setBounds:CGRectMake(0, 0, 736, 414)];
            [self.playerView.view setCenter:CGPointMake(212, 368)];
        }
        else if(IS_IPHONE_6)
        {
            [self.playerView.view setBounds:CGRectMake(0, 0, 375, 667)];
            [self.playerView.view setCenter:CGPointMake(187, 333)];
        }
        else if (IS_IPHONE_5)
        {
            [self.playerView.view setBounds:CGRectMake(0, 0, 736, 414)];
            [self.playerView.view setCenter:CGPointMake(160, 284)];
        }
        else
        {
            [self.playerView.view setBounds:CGRectMake(0, 0, 480, 320)];
            [self.playerView.view setCenter:CGPointMake(160, 240)];
        }

        [self.playerView.view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
        self.playerView.modalPresentationStyle = UIModalPresentationFormSheet;
        self.playerView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

        [self presentViewController:self.playerView animated:YES completion:nil];

And the callback movieFinishedCallback: is as follows,

- (void)movieFinishedCallback:(NSNotification*)aNotification
{
    // Obtain the reason why the movie playback finished
    NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

    // Dismiss the view controller ONLY when the reason is not "playback ended"
    if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
    {
        MPMoviePlayerController *moviePlayer = [aNotification object];
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:moviePlayer];

        NSLog(@"Video Closed");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^
        {
            [self dismissViewControllerAnimated:NO completion:nil];
            self.playerView = nil;

        });
    }
}

This worked for me. Hope it helps.

0
votes

in your project, select name project and right window select info tab. in custom ios target properties add key and select key: "Initial interface orientation" set value: Portrait (bottom home button)

  • rebuild your project -> ok
0
votes

For iOS 6 you can use this answer.

But if you supports < iOS 6 need a different approach.

You must create custom navigation controller and to it add methods for creation with root controller and method for rotation.

It will look like: m file and h file.

And in your AppDelegate must call method for init:

In h file:

#import "IORNavigationController.h"

and

@property (nonatomic, retain) IORNavigationController*  navigationController;

In m file:

self.navigationController = [[[MyNavigationController alloc] initWithRootViewController:start] autorelease];
0
votes

use this

moviePlayerController.view.transform = CGAffineTransformMakeRotation(M_PI/2);

it work with ios 7

0
votes

Just add this code to yours view controller

-(NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}