3

我想全屏查看 MPMoviePlayerController。它可以是水平视图。MPMoviePlayerController 只能在纵向视图中。这是我尝试过的代码:

创建 MPMoviePlayerController:

    NSURL *movieURL = [NSURL URLWithString:previewString];
    movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];


    [movieController.view setFrame:CGRectMake(10,130, 275 , 150)];
    movieController.view.backgroundColor = [UIColor grayColor];
    [detailview addSubview:movieController.view];

    [movieController prepareToPlay];
    movieController.shouldAutoplay = NO;

    [detailview addSubview:movieController.view];

我试过上面的代码,但是当moviecontroller全屏时,它只能在纵向模式下查看。无法在横向模式下查看。我只希望全屏模式下的电影控制器可以在横向模式下查看,而无需在纵向模式下查看其他视图。我应该怎么办?

4

4 回答 4

4

在你的 .h 文件中

@property (strong, nonatomic) MPMoviePlayerController *moviePlayer;

并在您的 .m 文件中合成该属性

@synthesize moviePlayer = _moviePlayer;

我的资源文件夹中有电影文件,请相应地更改您的名称:

            _moviePlayer =  [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video001" ofType:@"MOV"]]];
           [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(moviePlayBackDidFinish:)
                                                         name:MPMoviePlayerPlaybackDidFinishNotification
                                                       object:_moviePlayer];
            _moviePlayer.controlStyle = MPMovieControlStyleDefault;
            _moviePlayer.shouldAutoplay = YES;
            [_moviePlayer prepareToPlay];
            [self.view addSubview:_moviePlayer.view];
            [_moviePlayer setFullscreen:YES animated:YES];
            [_moviePlayer stop];
            [_moviePlayer play];

为了关闭全屏,我有这样的方法:

- (void) moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];

    if ([player
         respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [player.view removeFromSuperview];
    }
}

希望对你有效。

于 2013-09-13T04:42:17.360 回答
4

是的,您可以使用两个通知观察者来更改完整方向。

首先,将两个通知观察者添加到您的AppDelegate didFinishLaunchingWithOptions方法中:

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

二、添加方法和属性

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

三、重写supportedInterfaceOrientationsForWindow方法,你可以返回任何你想要的方向

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.allowRotation) {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    }
    return UIInterfaceOrientationMaskPortrait;
}
于 2014-04-11T05:13:27.863 回答
1

您的方法面临的挑战是,只需将子视图添加到您的视图中,处理旋转事件就是您的责任。

听起来你应该MPMoviePlayerViewController改用:

MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];

[self presentMoviePlayerViewControllerAnimated:playerController];

[playerController.moviePlayer play];

如果您在应用程序中正确设置了方向选项,则旋转将由播放器控制器处理,并将播放纵向和横向内容。如果您的应用当前不支持横向,则电影播放器​​控制器也不支持。

于 2013-09-13T15:03:08.717 回答
1

我在我的一个应用程序中以横向模式使用了以下代码。

    [moviePlayerController.view setFrame:CGRectMake(0, 0, 1024, 768)];
    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    moviePlayerController.repeatMode = MPMovieRepeatModeOne;
    moviePlayerController.controlStyle = MPMovieControlStyleNone;
    [moviePlayerController play];
于 2013-09-13T02:39:20.817 回答