iPhone cannot rotate movie to landscape mode using MPMoviePlayerViewController
Asked Answered
D

1

7

[update]

As was recommended I changed all the parent view controllers to support all orientations. My app structure is as follows: AppDelegate > RootViewController > Videos > VideoDetails > MPMoviePlayerViewController.

The video will play in landscape if I change all these to support all orientations. But supporting all orientations is not what I want and causes other issues. Is there any other work around or anything else I can do?

Thanks

[/update]

I have an portrait based iPhone app that displays videos using a custom subclass of MPMoviePlayerViewController. When the user presses play I create an instance of this class and present it modally as follows:

- (IBAction) playPressed:(id)sender {

NSString *filepath = [[NSBundle mainBundle] pathForResource:self.currentVideoModel.videoFileName ofType:@"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];

// MovieViewController is just a simple subclass of MPMoviePlayerViewController
self.moviePlayerController = [[MovieViewController alloc] initWithContentURL:fileURL]; 

// full screen code.
[self.moviePlayerController.moviePlayer setScalingMode:MPMovieScalingModeFill];
[self.moviePlayerController.moviePlayer setFullscreen:TRUE];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerController];

[self presentMoviePlayerViewControllerAnimated:self.moviePlayerController];
}

The problem is that it plays fine in portrait but when I turn the iPhone to landscape the video still plays in portrait and not landscape :( All the view controllers in the app only support portrait orientation.

My MPMoviePlayerViewController subclass only overrides the following method to allow for orientation changes but it has no affect:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

I've even tried to programmatically rotate the video but with absolutely no luck, it always stays in portrait mode.

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
    return true;
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI * 2)];
    return true;
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
    [self.view setTransform:CGAffineTransformIdentity];
    return true;
}
else return false;

}

Difficulty answered 24/1, 2012 at 4:47 Comment(5)
Rotation support is sort of heirarchical. So if you're presenting a movie player controller and it's not letting you rotate, my first debugging step would be to check EVERY view controller that leads to your movie player for the shouldAutorotate.. method and make it just return YES; (you can do this incrementally if you like to save extra hassle later). Then switch them one at a time back to portrait until it locks to portrait, and you know which ones UIKit is checking when trying to rotate yours. Go from there.Inapposite
@darvids0n thanks. I dont really want to let any other view support anything other than portrait but I guess I'll give it a try and see what happens.Difficulty
@darvids0n I updated my question. is there anything you can suggest? many thanks for any advice.Difficulty
I ran into the same issue developing an app, but I opted for the "redesign UI to support all orientations" route rather than trying to get it to separate rotation support out properly. So there's not much else I'll be able to help you with (hence not posting my comment as an answer). Good luck though!Inapposite
got it sorted @darvids0n thanks for all your help!Difficulty
D
9

[EDIT] The below solution worked perfectly on iOS5 but no longer works on iOS6. I may get some time to look into this issue in the future hopefully :( [/EDIT]

OK I fixed it. It was all to do with my mis-understanding of how iOS notifies an app of orientation changes. I thought it broadcasts out any orientation change but it doesn't, it follows your view hierarchy and it is up to you to tell any child view controllers of an orientation change. This was my undoing.

My app consisted of the following set up:

window > RootViewController > tabbar controller > nav controller > view controller > MPMoviePlayerViewController

I subclassed the tabbar controller to only return true for portrait mode. I returned this from the root view controllers shouldAutoRotateToOrientation method. This ensured that all views will be portrait only.

Then I presented the movie modally using the presentMoviePlayerViewControllerAnimated method called from the RootViewController. This automatically called the custom MPMoviePlayerViewController's shouldAutoRotateToOrientation method which was set to YES for both landscape and portrait :)

Difficulty answered 26/1, 2012 at 1:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.