Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone cannot rotate movie to landscape mode using MPMoviePlayerViewController

[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;

}

like image 807
bennythemink Avatar asked Jan 24 '12 04:01

bennythemink


People also ask

How to restore rotation on iPhone?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off.


Video Answer


1 Answers

[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 :)

like image 106
bennythemink Avatar answered Sep 30 '22 08:09

bennythemink