2

我有一个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不会再调用了;因此,即使用户旋转回纵向,视频仍处于全屏状态。

4

2 回答 2

3

尝试使用 aMPMoviePlayerViewController而不是MPMoviePlayerController. 只需在您的中初始化它UIViewControllermoviePlayer像使用普通MPMoviePlayerController. 如果您进行子类化,您可以通过实现等MPMoviePlayerViewController来控制设备旋转时发生的情况。willAnimateRotationToInterfaceOrientation

于 2012-11-28T16:07:38.760 回答
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
于 2014-04-11T05:45:11.423 回答