0

我有一个扩展 MPMoviePlayerController 的自定义类,因此它可以随手机方向旋转。我的应用程序在播放视频时不支持在任何其他视图中旋转,这很好用......有一段时间。在删除并重新安装应用程序之前,它们随机只会以纵向播放。有没有其他人见过这样的事情?一直在追这个,每次我认为它已修复时,它都会返回。

谢谢。

4

1 回答 1

0

我做了完全一样的。我的应用程序是基于标签的,除了电影播放器​​之外不支持旋转。这是我的代码,我在播放持续旋转的电影时使用。我曾使用电影播放器​​视图不旋转MPMoviePlayer 完成按钮问题链接来提出此解决方案。祝你好运!

用法:

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

自定义电影播放器​​.h

#import <MediaPlayer/MediaPlayer.h>

@interface CustomMoviePlayer : UIViewController 

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

@end

自定义电影播放器​​.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
于 2012-09-20T14:14:32.097 回答