Bautzi89
Bautzi89

Reputation: 346

iOS: How to change orientation of MPMoviePlayerViewController on iPad?

I am working on a universal application that starts with a really short intro video. So I simply add a MPMoviePlayerViewController and modally present it. On the iPhone everything works fine even if I play the video in landscape mode. But on the iPad the video always plays in portrait mode no matter what interface orientation the device currently has. I searched for hours and tried a lot of different things like CGAffineTransformation or adding a new view, but nothing seems to work. Here is the code that I work with:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:videoName ofType:@"mp4"];
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];  

    self.startupController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
    [self.startupController.moviePlayer setControlStyle:MPMovieControlStyleNone];
    [self.startupController.moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
    [self.startupController.moviePlayer setFullscreen:YES];        
    [self presentModalViewController:self.startupController animated:NO];

Do you have any idea how I can fix this issue? In my opinion this should be a simple feature offered by Apple, but sometimes the easiest things are the ones I have to worry about most...

Upvotes: 0

Views: 1722

Answers (1)

Trausti Thor
Trausti Thor

Reputation: 3774

Here is how I did it. First, listen to this notification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

Then implement this :

- (void)detectOrientation {
    NSLog(@"handling orientation");
    [self setMoviePlayerViewOrientation:[[UIDevice currentDevice] orientation]];
}


- (void)setMoviePlayerViewOrientation:(UIDeviceOrientation)orientation {
if (lwvc != nil)
    return;
if (moviePlayerController.playbackState == MPMoviePlaybackStateStopped) return;
//Rotate the view!
CGFloat degree = 0;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
switch (orientation) {
    case UIDeviceOrientationPortrait:
    case UIDeviceOrientationPortraitUpsideDown:
        degree = 0;
        moviePlayerController.view.bounds = CGRectMake(0, 0, 320, height);
        break;
    case UIDeviceOrientationLandscapeLeft:
        degree = 90;
        moviePlayerController.view.bounds = CGRectMake(0, 0, height, 320);
        break;
    case UIDeviceOrientationLandscapeRight:
        degree = -90;
        moviePlayerController.view.bounds = CGRectMake(0, 0, height, 320);
        break;
    default:
        break;
}
lastOrientation = orientation;
CGAffineTransform cgCTM = CGAffineTransformMakeRotation((degree) * M_PI / 180);
moviePlayerController.view.transform = cgCTM;
[UIView commitAnimations];
[[UIApplication sharedApplication] setStatusBarOrientation:orientation];
}

So the trick is just using transformation

Upvotes: 3

Related Questions