MPMoviePlayerViewController rotate问题

整个工程支持UIInterfaceOrientationPortrait,但是MPMoviePlayerViewController必须支持横屏播放功能,解决思路是继承MPMoviePlayerViewController,添加UIDeviceOrientationDidChangeNotification,用比较人性化的UIView动画切换效果强制屏幕切换,并且注意调整UIStatusBar的方向,代码如下:

@interface MyMovieViewController : MPMoviePlayerViewController
@end

@implementation MyMovieViewController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}

//为了支持iOS6
-(BOOL)shouldAutorotate
{
    return YES;
}

//为了支持iOS6
-(NSUInteger)supportedInterfaceOrientations
{
    return 0;
}

- (id) initWithContentURL:(NSURL *)contentURL {
    self = [super initWithContentURL:contentURL];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    }
    return self;
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (void)detectOrientation {
    UIDeviceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.4];
    [UIView setAnimationCurve:2];
    
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
        self.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
        self.view.bounds = CGRectMake(0, 0, 480, 320);
    } else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
        self.view.bounds = CGRectMake(0, 0, 480, 320);
    } else if(toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        self.view.transform = CGAffineTransformMakeRotation(M_PI);
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortraitUpsideDown];
        self.view.bounds = CGRectMake(0, 0, 320, 480);
    } else if(toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        self.view.transform = CGAffineTransformMakeRotation(0);
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
        self.view.bounds = CGRectMake(0, 0, 320, 480);
    }
    
    [UIView commitAnimations];
    
    [[UIApplication sharedApplication] setStatusBarOrientation:[self interfaceOrientation] animated:NO];
}

@end


实例化:
MPMoviePlayerViewController *playerVC = [[[MyMovieViewController alloc] initWithContentURL:[NSURL fileURLWithPath:document.filePath]] autorelease];


你可能感兴趣的:(controller)