iOS 5及更高版本:在MPMoviePlayerController中启用横向模式

时间:2013-07-31 06:00:22

标签: ios objective-c ios5 video

我的应用程序完全处于纵向模式。 (iOS 5及以上版本) 我有一个使用MPMoviePlayerController播放的视频,现在在这个视频中我希望当用户旋转iPhone时,视频应该进入横向模式(全屏)。 当视频结束时,视频应再次进入纵向模式。 代码:

    -(void)PlayVideo:(NSURL*)videoUrl
{


    moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoUrl];
    [moviePlayerController.view setFrame:CGRectMake(6, 69, 309, 196)];
    [self.view addSubview:moviePlayerController.view];
    //    moviePlayerController.fullscreen = YES;


    moviePlayerController.controlStyle = MPMovieControlStyleNone;
    [self.view bringSubviewToFront:self.shareView];
    [self.view bringSubviewToFront:self.qualityView];

    [moviePlayerController play];

    // Register to receive a notification when the movie has finished playing.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];

}

应用程序的其余部分,我只想要肖像。 我如何实现这一目标?

3 个答案:

答案 0 :(得分:4)

首先,您需要将支持界面方向设置为纵向以及横向

As shows here

现在在每个UIViewController中都需要覆盖这些方法 -

iOS 5 -

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
    return NO;
}

iOS 6

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

在UIViewController中,您要添加MPMoviePlayerController覆盖 -

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

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

答案 1 :(得分:0)

我用视频播放器的样本创建了Xcode项目。它可以在横向模式下全屏显示视频。你可以download it。我希望它对你有所帮助。

第二。而不是

[self.view bringSubviewToFront:self.shareView];
[self.view bringSubviewToFront:self.qualityView];

你应该这样写:

    [moviePlayerController.view insertSubview: self.shareView
                                      atIndex: 2];

    [moviePlayerController.view insertSubview: self.qualityView
                                      atIndex: 2];

然后shareView和qualityView出现在电影播放器​​的顶部。

答案 2 :(得分:-1)

有两个选项

1-我们需要在播放通知上旋转moviePlayerController(这将全部以编程方式)

2-我们需要旋转视图,这意味着moviePlayerController感觉superview处于横向模式,因此moviePlayerController自动进入景观

[[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation] 

此方法不存在

但我们可以做 -

- (void)lanscapeView{
    // check current orientation
    if ([[UIApplication sharedApplication] statusBarOrientation] != UIInterfaceOrientationLandscapeLeft) {
        // no, the orientation is wrong, we must rotate the UI
        self.navigationController.view.userInteractionEnabled = NO;
        [UIView beginAnimations:@"newAlbum" context:NULL];
        [UIView setAnimationDelegate:self];
        // when rotation is done, we can add new views, because UI orientation is OK
        [UIView setAnimationDidStopSelector:@selector(addAlbum)];
        // setup status bar
        [[UIApplication sharedApplication]     setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft  animated:NO];
        // rotate main view, in this sample the view of navigation controller is the root view in main window
        [self.navigationController.view setTransform: CGAffineTransformMakeRotation(M_PI / 2)];
        // set size of view
        [self.navigationController.view setFrame:CGRectMake(0, 0, 748, 1024)];
        [UIView commitAnimations];
    } 
    else {
    //do else stuff
    }
}