dzl
dzl

Reputation: 906

MPMoviePlayerViewController doesn't auto-rotate

I'm having some strange behavior where my MPMoviePlayerViewController isn't auto-rotating when the orientation changes. However, I recreated the same view hierarchy in a fresh project and when the MPMoviePlayerViewController player was up, it rotated to every orientation. I've scoured the project looking for anything that might be setting the orientation explicitly, but there is nothing.

I'll lay out all the relevant information here and the things that I've tried so far.

The view hierarchy currently looks like this:

Every class in the view hierarchy responds to shouldAutorotateToInterfaceOrientation with YES only for UIInterfaceOrientationPortrait.

Things I've tried:

If the movie player properly rotates in a fresh project with same view hierarchy, what could possible be getting in the way here. Any ideas as to where the orientation might be getting stuck?

Upvotes: 22

Views: 8062

Answers (12)

G. Hazarath Reddy
G. Hazarath Reddy

Reputation: 57

Add this method in AppDelegate.m

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

if(!ISIPAD)

  {

    if ([[self.window.rootViewController presentedViewController]   isKindOfClass:[MPMoviePlayerViewController class]] && ![[self.window.rootViewController presentedViewController] isBeingDismissed])

    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else
    {
        return UIInterfaceOrientationMaskPortrait;
    }
}
return UIInterfaceOrientationMaskAllButUpsideDown ;

}

Upvotes: 0

slobodans
slobodans

Reputation: 859

I had lot of pain with MPMoviePlayerViewController, which should be only controller able to rotate from portrait to landscape and vice versa. It was iOS7, iOS8 application with storyboard.

Here is solution:

  1. Application should enable all possible required orientationsAs on the image
  2. Every UIViewController that need to support just portrait mode, should implement next methods, like this

    -(BOOL)shouldAutorotate
    {
        return YES;
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    
  3. MPMoviePlayerViewController should be extended and next methods should be overriden like this

    -(BOOL)shouldAutorotate
    {
      return YES;
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
      return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    
  4. Use presentMoviePlayerViewControllerAnimated to display MPMoviePlayerViewController

Upvotes: 0

Fabio Napodano
Fabio Napodano

Reputation: 1247

this guide helped me checking some steps I was missing in order to allow only the video player to be viewed in landscape, leaving the rest of the application to be fixed in portrait mode:

iOS6 and autorotation tip

Upvotes: 0

Aleksander Azizi
Aleksander Azizi

Reputation: 9877

You should try this (Worked for me):

Declare in .h file:

BOOL landscape;

.m file:

-(IBAction)PlayMovie:(NSString *)movieName {
    landscape = YES;
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:movieName ofType:@"mp4"];
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
    MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    theMovie.scalingMode = MPMovieScalingModeAspectFill;
    [theMovie play];
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:moviePlayer];
}

-(void)dismissMoviePlayerViewControllerAnimated {
    landscape = NO;
    [self dismissModalViewControllerAnimated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// You can change the return for your needs.
    if (landscape == YES) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else { return NO; }
}

What I did here is Creating my movie view and set the "landscape" BOOL to YES.

Then the "shouldAutorotateToInterfaceOrientation" will detect this and autorotate your view.

And when the movie is finished I set the "landscape" to NO so that the view rotates back.

Upvotes: 0

Snowcrash
Snowcrash

Reputation: 86317

I had a similar problem.

Solved by:

  1. enabling all orientations in Supported Interface Orientations (Target > Summary)
  2. now your app will start rotating in all Orientations, if you dont want this, then skip step 1, only add following method in appDelegate

    • (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ return UIInterfaceOrientationMaskAll; }
  3. removing shouldAutorotateToInterfaceOrientation everywhere

  4. adding following method to view controller for supporting Orientations u needed for your app

    • (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
  5. you can add the same method as in step 4 in MPMoviePlayerViewController's subclass for whatever Orientations u needed for video player

Upvotes: 0

dzl
dzl

Reputation: 906

Solution:

For anyone that might encounter this, the reason the video wasn't rotating was that I was accidentally adding the RootViewController has the window's rootViewController, rather than the UINavigationController.

self.window.rootViewController = navController; 

is correct

self.window.rootViewController = rootViewController;

is not

Thank you guys for your help and input along the way.

Upvotes: 1

Danra
Danra

Reputation: 9926

A few ideas:

  • Is the UINavigationController set as the rootViewController property of your app's UIWindow? You didn't mention that. You should do this rather then adding the navigation controller's view to the window.
  • In case you are building the entire hierarchy at once, try breaking it down. You can add a button to each stage which adds the next view controller to the hierarchy.
  • Try removing any animations from the view controller hierarchy construction. Doing multiple animations at the same time could be trouble. For example, it's not allowed to push two view controllers in a UINavigationController one after another, with animated:YES. You might have a similar issue.
  • Make sure you build your entire view controller hierarchy on the main thread.
  • Make sure there is no other view controller "taking charge" of the rotation (as @MihaiFratu wrote - this is so common a reason for rotation issues that I had to repeat it :-) ).

Upvotes: 1

Solid Soft
Solid Soft

Reputation: 1932

there are some reasons behind at your query..

***You are calling MPMoviePlayerViewController.. so apply AutoOrientation on "Feed" View Controller And try to call by PushViewController..

***Use MPMoviePlayerController instead of MPMoviePlayerViewController and add subview in to FeedViewController..

Sample code for MPMoviePlayerController--

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"filename" ofType:@"type"]];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
videoPlayer.controlStyle = MPMovieControlStyleNone; 
videoPlayer.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.height, self.view.frame.size.width);
[videoPlayer prepareToPlay];
videoPlayer.view.center = self.view.center;
videoPlayer.fullscreen = YES;
[self.view addSubview:videoPlayer.view];
[videoPlayer play];

***Check your Xcode target setting and apply enable All orientation..

Upvotes: 0

Mihai Fratu
Mihai Fratu

Reputation: 7663

I know this might be a stupid suggestion but make sure that the shouldAutorotateToInterfaceOrientation method in your MPMoviePlayerViewController subclass is getting called. Maybe something went wrong there...

Also make sure that you don't have 2 subviews added to the main UIWindow as specified here:

Q: Why won't my UIViewController rotate with the device?

[...]

  • The view controller's UIView property is embedded inside UIWindow but alongside an additional view controller.

I think that might give you some problems too. You can find more information about what could go wrong in the link above.

Upvotes: 2

kallol
kallol

Reputation: 319

I will suggest you not to use presentMoviePlayerViewControllerAnimated, rather than add as subview. I think it will fix your problem nicely.

MPMoviePlayerViewController *mpviewController = [[MPMoviePlayerViewController alloc]
                    initWithContentURL:[NSURL fileURLWithPath:self.filePath]];
[self.view addSubview:mpviewController.view];
[self setWantsFullScreenLayout:YES]; 

And remove the mpviewController.view when MPMoviePlayerPlaybackDidFinishNotification detected. Let me see your success...

Upvotes: 7

brimat
brimat

Reputation: 166

Are you using storyboards? Compare the orientation settings for your UIViewControllers and your UINavigationController between your broken project and your test project. The "Orientation" setting on the attributes inspector may be locking you into one orientation.

You mentioned shouldAutorotateToInterfaceOrientation: and your plist settings so I won't get into that...

Upvotes: 0

Mr. Berna
Mr. Berna

Reputation: 10655

I've found that MPMoviePlayerViewController objects will honor the project's Info.plist settings for supported interface orientations. In a project of mine I was only allowing landscape views in that file so the movie player will not rotate, even when it answered YES to landscape orientations in shouldAutorotateToInterfaceOrientation:.

Edit: OK, grasping at straws: Do you implement automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers in any of your UIViewController subclasses? If so, and it returns NO, your subclasses must forward the appropriate methods to any child controllers upon orientation change.

Otherwise is there any way to see your code?

Upvotes: 3

Related Questions