0

I have a custom class that is extending MPMoviePlayerController so it can rotate with the phone orientation. My app does not support rotation in any view other while playing videos, and this works great... for a while. They randomly will ONLY play in Portrait orientation until the app is deleted and reinstalled. Has anyone else seen anything like this? Been chasing this one for a while, and every time I think it's fixed, it returns.

Thanks.

iveytron
  • 1,669
  • 2
  • 12
  • 11
  • Point one, do not subclass MPMoviePlayerController. Point two, present the viewController that manages MPMoviePlayerController modally and let that viewController handle the orientation. – Till Sep 20 '12 at 21:41
  • Any specific reason not to subclass MPMoviePlayerController? Also, I am presenting it modally. – iveytron Sep 21 '12 at 16:41

1 Answers1

0

I have done exactly same. My application is tab based and doesn't support rotation except movie player. Here is my code, I am using while playing movie that rotates consistently. I had used Movie player view doesn't rotate and MPMoviePlayer done button issue links to come up with this solution. Good luck!

Usage:

CustomMoviePlayer* cPlayer = [[CustomMoviePlayer alloc] init];
[cPlayer playMovie:movieFilePath onViewController:self];

CustomMoviePlayer.h

#import <MediaPlayer/MediaPlayer.h>

@interface CustomMoviePlayer : UIViewController 

@property(nonatomic, retain)  MPMoviePlayerController* moviePlayer;
-(void)playMovie:(NSString*)filePath onViewController:(UIViewController*)view;

@end

CustomMoviePlayer.m

#import "CustomMoviePlayer.h"
#define degreesToRadian(x) (M_PI * (x) / 180.0)
#define radianToDegrees(x) ((x) * 180.0/M_PI)

@implementation CustomMoviePlayer

-(void)playMovie:(NSString*)filePath onViewController:(UIViewController*)controller {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotateMoviePlayer) name: UIDeviceOrientationDidChangeNotification object:nil];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];   

    NSURL *url = [NSURL fileURLWithPath:filePath];

    self.moviePlayer =  [[MPMoviePlayerController alloc] initWithContentURL:url];
    self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
    self.moviePlayer.shouldAutoplay = YES;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];

    [controller.view addSubview:moviePlayer.view];
    [self.moviePlayer setFullscreen:YES animated:YES];
}

- (void)moviePlaybackComplete:(NSNotification *)notification {
     [self cleanupOnMovieComplete];
}

- (void)MPMoviePlayerDidExitFullscreen:(NSNotification *)notification {
     [self cleanupOnMovieComplete];
}

- (void)cleanupOnMovieComplete {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];

    if(_moviePlayer != nil) {
        [_moviePlayer.view removeFromSuperview];
        [_moviePlayer release];
        _moviePlayer = nil;
    }

    /* Transform window to identity */
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];

    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    if (!window)
        window = [[UIApplication sharedApplication].windows objectAtIndex:0];

    [window setTransform:CGAffineTransformIdentity];
}

-(void)rotateMoviePlayer {
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

     if (orientation != UIDeviceOrientationUnknown) {

         CGAffineTransform transform = CGAffineTransformMakeRotation(degreesToRadian(0));

         UIWindow *window = [UIApplication sharedApplication].keyWindow;

        if (!window)
            window = [[UIApplication sharedApplication].windows objectAtIndex:0];

        switch (orientation) {
            case UIDeviceOrientationLandscapeLeft:
                transform = CGAffineTransformMakeRotation(M_PI / 2); 
                [window setTransform:transform];
                break;
            case UIDeviceOrientationLandscapeRight:
                transform = CGAffineTransformMakeRotation(-M_PI / 2);
                [window setTransform:transform];
                break;
            case UIDeviceOrientationPortraitUpsideDown:
                transform = CGAffineTransformMakeRotation(M_PI);
                [window setTransform:transform];
                break;  
            case UIDeviceOrientationPortrait:
                [window setTransform:CGAffineTransformIdentity];
                break;
         }
    }   
}

@end
Community
  • 1
  • 1
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139