首页 文章

MPMoviePlayerController全屏旋转,而父视图控制器仅支持纵向方向

提问于
浏览
12

这个问题只是我问题的一部分 . 我正在为我现有的应用程序实现iOS6轮换和方向支持 .

所以我有一个ViewController,它包含一个嵌入在ViewController视图中的MPMoviePlayerController(我的应用程序需要它) . 用户可以播放视频并在嵌入视图中查看,或使用默认播放器控件单击全屏按钮,播放器进入全屏模式 .

现在,我已将视图控制器限制为仅使用iOS6提供的新旋转API支持纵向方向 .

// New Autorotation support.
- (BOOL)shouldAutorotate;
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

这非常有效 . ViewController仅支持纵向和用户在嵌入视图中播放影片 .

现在问题出现了,当用户进入全屏模式时 . 在全屏模式下,当我旋转模拟器/设备时,电影将继续旋转 . 当我在 shouldAutorotatesupportedInterfaceOrientations 中使用断点以全屏模式播放电影时旋转设备时,这两种方法仍然分别返回 NOUIInterfaceOrientationMaskPortrait ,但电影仍在旋转...

为什么会这样? ....这是我的问题的一部分......第二部分是我希望当用户进入全屏模式时电影以横向模式进入 . 我希望电影播放器锁定横向模式,直到用户按下DONE按钮 .

请帮忙 ....

8 回答

  • 1

    您可以在 AppDelegate 中尝试以下功能:

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
    
    }
    

    你可以为这两种模式制定条件 .

    例如,如果媒体播放器是全屏幕的话

    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;

    否则 return UIInterfaceOrientationMaskPortrait;

    我没试过,但我想,它应该适用于你的情况 .

    谢谢

  • 0

    为清楚起见,这里是完整的代码(它全部进入你的app代理):

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(willExitFullscreen:)
                                                     name:MPMoviePlayerWillExitFullscreenNotification
                                                   object:nil];
    
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(willEnterFullscreen:)
                                                 name:MPMoviePlayerWillEnterFullscreenNotification
                                               object:nil];
    }
    
    - (void)willEnterFullscreen:(NSNotification*)notification
    {
        NSLog(@"willEnterFullscreen");
        isFullScreen = YES;
    }
    
    - (void)willExitFullscreen:(NSNotification*)notification
    {
        NSLog(@"willExitFullscreen");
        isFullScreen = NO;
    }
    
    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if (isFullScreen)
            return UIInterfaceOrientationMaskLandscapeLeft;
        else
            return UIInterfaceOrientationMaskPortrait;
    }
    

    isFullScreen是要在AppDelegate.h中声明的BOOL

  • 22

    我建议改用 MPMoviePlayerViewController . 对它进行子类化并实现 supportedInterfaceOrientations 方法并返回 UIInterfaceOrientationMaskLandscape .

    您可能还必须实现 shouldAutorotateToInterfaceOrientation: 方法 .

    请参阅类参考:MPMoviePlayerViewController

    Edit: 你也可以看一下这篇文章:iphone - force MPMoviePlayerController to play video in landscape mode

  • 0

    这耗费了我一段时间,我得到了许多不同的可怕错误,但最终我最终没有通过 MPMoviePlayerController 而是 MPMoviePlayerViewController . 在呈现之前,我只是旋转了属性的 self.playerView . 此外,我添加了 NSNotification ,它将在视频结束后返回主控制和主ViewController . 这是我如何执行它:

    [[NSNotificationCenter defaultCenter] removeObserver:self.playerView
                                                            name:MPMoviePlayerPlaybackDidFinishNotification
                                                          object:self.playerView.moviePlayer];
    
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(movieFinishedCallback:)
                                                         name:MPMoviePlayerPlaybackDidFinishNotification
                                                       object:self.playerView.moviePlayer];
    
            self.playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:docUrl];
            self.playerView.view.frame = CGRectMake(10, 10, self.frame.size.width-20, 180);
            [self.playerView.moviePlayer prepareToPlay];
    
            if(IS_IPHONE_6P)
            {
                [self.playerView.view setBounds:CGRectMake(0, 0, 736, 414)];
                [self.playerView.view setCenter:CGPointMake(212, 368)];
            }
            else if(IS_IPHONE_6)
            {
                [self.playerView.view setBounds:CGRectMake(0, 0, 375, 667)];
                [self.playerView.view setCenter:CGPointMake(187, 333)];
            }
            else if (IS_IPHONE_5)
            {
                [self.playerView.view setBounds:CGRectMake(0, 0, 736, 414)];
                [self.playerView.view setCenter:CGPointMake(160, 284)];
            }
            else
            {
                [self.playerView.view setBounds:CGRectMake(0, 0, 480, 320)];
                [self.playerView.view setCenter:CGPointMake(160, 240)];
            }
    
            [self.playerView.view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
            self.playerView.modalPresentationStyle = UIModalPresentationFormSheet;
            self.playerView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
            [self presentViewController:self.playerView animated:YES completion:nil];
    

    回调 movieFinishedCallback :如下,

    - (void)movieFinishedCallback:(NSNotification*)aNotification
    {
        // Obtain the reason why the movie playback finished
        NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    
        // Dismiss the view controller ONLY when the reason is not "playback ended"
        if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
        {
            MPMoviePlayerController *moviePlayer = [aNotification object];
            [[NSNotificationCenter defaultCenter] removeObserver:self
                                                            name:MPMoviePlayerPlaybackDidFinishNotification
                                                          object:moviePlayer];
    
            NSLog(@"Video Closed");
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^
            {
                [self dismissViewControllerAnimated:NO completion:nil];
                self.playerView = nil;
    
            });
        }
    }
    

    这对我有用 . 希望能帮助到你 .

  • 7

    在项目中,选择名称项目和右侧窗口选择信息选项卡 . 在自定义ios目标属性中添加键并选择键:“初始界面方向”设置值:纵向(底部主页按钮)

    • 重建你的项目 - >好的
  • 0

    对于iOS 6,您可以use this answer .

    但是如果你支持<iOS 6需要不同的方法 .

    您必须创建自定义导航控制器,并使用根控制器和旋转方法添加创建方法 .

    它看起来像是:m fileh file .

    并且在你的AppDelegate中必须调用init的方法:

    在h文件中:

    #import "IORNavigationController.h"
    

    @property (nonatomic, retain) IORNavigationController*  navigationController;
    

    在m文件中:

    self.navigationController = [[[MyNavigationController alloc] initWithRootViewController:start] autorelease];
    
  • 1

    用这个

    moviePlayerController.view.transform = CGAffineTransformMakeRotation(M_PI/2);
    

    它适用于ios 7

  • 0

    只需将此代码添加到您的视图控制器即可

    -(NSUInteger)supportedInterfaceOrientations {
    
        return UIInterfaceOrientationMaskPortrait;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return UIInterfaceOrientationPortrait;
    }
    

相关问题