如何使MPMoviePlayerController全景在横向而不是全屏在纵向

时间:2012-11-28 16:01:32

标签: objective-c ios mpmovieplayercontroller

我的MPMoviePlayerController初始化如下:

//Code in my UIViewController
@property (nonatomic, strong) UIView *myVideoView;
@property (nonatomic, strong) MPMoviePlayerController *myVideoPlayer;

- (void) initializeVideoPlayer
{
    CGRect frame = CGRectMake(0, 70, self.view.frame.size.width, 200);
    self.myVideoView = [[UIView alloc] initWithFrame:frame];
    [self.view addSubview:self.myVideoView];

    NSURL *videoURL = [NSURL fileURLWithPath:path];

    self.myVideoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
    self.myVideoPlayer.controlStyle = MPMovieControlStyleEmbedded;
    self.myVideoPlayer.shouldAutoplay = YES;
    [self.myVideoPlayer.view setFrame: self.myVideoView.bounds];
    [self.myVideoView addSubview: self.myVideoPlayer.view];

    //Play video
    [self.myVideoPlayer prepareToPlay];
    [self.myVideoPlayer play];

}

我的问题是,当用户将手机旋转到横向时,如何以全屏模式播放视频,而当手机处于人像状态时,如何全屏播放视频。

我已尝试将以下内容添加到UIViewController

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
    {
        [self.myVideoPlayer setFullscreen:YES animated:YES];
    }
    else
    {
        [self.myVideoPlayer setFullscreen:NO animated:YES];
    }
}

然而,问题在于,一旦玩家处于全屏状态,willAnimateRotationToInterfaceOrientation将不再被调用;因此,即使用户旋转回肖像,视频仍然是全屏的。

2 个答案:

答案 0 :(得分:3)

尝试使用MPMoviePlayerViewController代替MPMoviePlayerController。只需在UIViewController中初始化它,并像使用普通moviePlayer一样使用其MPMoviePlayerController属性。如果您继承MPMoviePlayerViewController,则可以通过实施willAnimateRotationToInterfaceOrientation等来控制设备旋转时发生的情况。

答案 1 :(得分:0)

在AppDelegate.h中:

@property(nonatomic)BOOL allowRotation;
AppDelegate.m中的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    RootViewController * root = [[RootViewController alloc] init];
    self.window.rootViewController = root;

//add two Notification

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPVisionVideoNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPVisionVideoNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void) moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
    self.allowRotation = YES;
}

- (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
    self.allowRotation = NO;
}

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.allowRotation) {

        return UIInterfaceOrientationMaskLandscapeRight ;
    }
    return UIInterfaceOrientationMaskPortrait;
}

//this can rotate the windows when to fullscreen state