iPad上MPMoviePlayerController全屏问题

13

我想在视图控制器中展示一个MPMoviePlayerController,并让用户使用默认控件切换全屏,就像YouTube应用一样。我在一个简单的示例中使用了以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.player = [[MPMoviePlayerController alloc] init];
    self.player.contentURL = theURL;
    self.player.view.frame = self.viewForMovie.bounds;
    self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.viewForMovie addSubview:player.view];
    [self.player play];
}

这个方法在用户全屏播放视频并旋转设备并点击屏幕时会出现问题,状态栏会显示在错误的位置,如下面的截图所示。

screenshot

我正在使用iPad的选项卡应用程序模板。我只添加了viewDidLoad,view变量和一个UIView来显示电影播放器。

我做错了什么?


我也遇到了同样的问题。还没有解决方法吗? - samvermette
我也是。有人成功了吗? - V1ru8
你有得到任何答案吗? - Jingwei
7个回答

3

是的,我也遇到了这个问题。显然这是MPMoviePlayerController本身存在的一个bug。

在我的应用中,我采取的解决方法是在退出全屏模式时自己调整状态栏:

- (void)playerDidExitFullscreen:(NSNotification *)notification {
    MPMoviePlayerController *moviePlayer = (MPMoviePlayerController *) notification.object;

    if (moviePlayer == self.player) {
        UIApplication *app = [UIApplication sharedApplication];
        if (app.statusBarOrientation != self.interfaceOrientation) {
            [app setStatusBarOrientation:self.interfaceOrientation animated:NO];
        }
    }
}

这并不能解决全屏模式下的问题,但是它可以在之后解决。

当然要注意将函数添加到通知中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];

2

你的 shouldAutorotateToInterfaceOrientation:interfaceOrientation 方法是否返回所有支持的方向的 YES 值?

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

如果你提供更多代码将更有帮助。


是的。我在项目中添加的唯一代码是上面的viewDidLoad和XIB中的viewForMovie。这是iPad的默认选项卡栏应用程序。 - hpique

1

我曾经遇到过同样的问题,花了半天时间才解决。当iPad处于竖屏方向时,每当我使用示例代码(或在网络上找到的任何代码)开始播放视频时,视频和控制栏都会被格式化为竖屏,因此在屏幕上随意移动。

无论如何,以下方法对我有效。

  /* Call the code like below:
        int iLandscape;
        if( newOrientation==UIInterfaceOrientationLandscapeLeft || newOrientation==UIInterfaceOrientationLandscapeRight )
             iLandscape=1;

        [self PlayVideo:iLandscape fullscreen:1]
    */
        //////////////////////////////////////////////////////////////////////////
        - (void)PlayVideo:(int)iLandscape fullscreen:(int)iFullScreen 
        {
            NSString *url = [[NSBundle mainBundle] pathForResource:@"myvideofile" ofType:@"m4v"];

        if( iFullScreen==0 )
        {
            MPMoviePlayerController *player2 = 
                [[MPMoviePlayerController alloc] 
                    initWithContentURL:[NSURL fileURLWithPath:url]];

            [[NSNotificationCenter defaultCenter] 
                addObserver:self
                   selector:@selector(movieFinishedCallback:)
                       name:MPMoviePlayerPlaybackDidFinishNotification
                     object:player2];

            //---play partial screen---
            player2.view.frame = CGRectMake(0, 0, m_iScreenWidth, m_iScreenHeight);
            [self addSubview:player2.view];
            //---play movie---
            [player2 play];
        }   
        else
        {
            MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] 
                initWithContentURL:[NSURL fileURLWithPath:url]];

            [[NSNotificationCenter defaultCenter] 
                addObserver:self
                   selector:@selector(movieFinishedCallback:)
                       name:MPMoviePlayerPlaybackDidFinishNotification
                     object:[playerViewController moviePlayer]];

            if( iLandscape )
            {
                playerViewController.view.frame = CGRectMake(0, 0, m_iScreenWidth, m_iScreenHeight);
            }
            [self addSubview:playerViewController.view];
            //play movie
            MPMoviePlayerController *player = [playerViewController moviePlayer];
            player.scalingMode=MPMovieScalingModeAspectFit;
            [player play];
        }
    }


    //////////////////////////////////////////////////////////////////////////
    - (void) movieFinishedCallback:(NSNotification*) aNotification 
    {
        MPMoviePlayerController *player = [aNotification object];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];    
        [player autorelease];    
        [player.view removeFromSuperview];
    }

1

你在使用接口构建器来制作用户界面吗?如果是,请确保在视图属性检查器中将视图的方向设置为“横向”。


现在它在另一个方向上失败了。 - hpique

0

找到了。

我曾经遇到过同样的问题 - 这是我所做的。我建议逐步将代码添加到您的项目中,以便确切地了解它的工作原理。

首先 - 我将事物放在纵向模式下。

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

然后我将电影推到状态栏上。注意 - 这假定视频具有4x3的宽高比。

theVideo = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath : path]];
float aspectRatio = (3.0f/4.0f);
float theMovieHeight = [self view].bounds.size.width * aspectRatio;
[[theVideo view] setFrame:(CGRectMake(0, [self view].bounds.size.height - theMovieHeight, [self view].bounds.size.width, theMovieHeight ))]; 

然后,在应用程序启动的地方(在我的项目中,它在didFinishLaunchingWithOptions函数中) - 无论如何,您只需要访问窗口对象。

float aspectRatio = (3.0f/4.0f);
float theMovieHeight = self.window.bounds.size.width * aspectRatio;
float theSpaceAboveTheMovie = self.window.bounds.size.height - theMovieHeight;
float whereTheMovieShouldBeCentered = (self.window.bounds.size.height - theMovieHeight) / 2;

CGAffineTransform theTransform = CGAffineTransformMakeTranslation(0,0);

theTransform = CGAffineTransformScale(theTransform, 1.0f/aspectRatio, 1.0f/aspectRatio);
theTransform = CGAffineTransformTranslate(theTransform, -whereTheMovieShouldBeCentered, 0);
theTransform = CGAffineTransformRotate(theTransform, M_PI / 2);
theTransform = CGAffineTransformTranslate(theTransform, 0, -theSpaceAboveTheMovie);

[self.window setTransform:theTransform];

记住仿射变换是按相反的顺序完成的。所以如果你想知道每个变换在做什么(我建议你应该),请注释掉前三个

这里,您应该看到电影和状态栏在页面上居中显示

//  theTransform = CGAffineTransformScale(theTransform, 1.0f/aspectRatio, 1.0f/aspectRatio);
//  theTransform = CGAffineTransformTranslate(theTransform, -whereTheMovieShouldBeCentered, 0);
//  theTransform = CGAffineTransformRotate(theTransform, M_PI / 2);
    theTransform = CGAffineTransformTranslate(theTransform, 0, -theSpaceAboveTheMovie);

然后是前两个

在这里,您应该看到电影和状态栏旋转并不再居中

//  theTransform = CGAffineTransformScale(theTransform, 1.0f/aspectRatio, 1.0f/aspectRatio);
//  theTransform = CGAffineTransformTranslate(theTransform, -whereTheMovieShouldBeCentered, 0);
    theTransform = CGAffineTransformRotate(theTransform, M_PI / 2);
    theTransform = CGAffineTransformTranslate(theTransform, 0, -theSpaceAboveTheMovie);

在这里,你应该看到它被旋转并居中显示

//  theTransform = CGAffineTransformScale(theTransform, 1.0f/aspectRatio, 1.0f/aspectRatio);
    theTransform = CGAffineTransformTranslate(theTransform, -whereTheMovieShouldBeCentered, 0);
    theTransform = CGAffineTransformRotate(theTransform, M_PI / 2);
    theTransform = CGAffineTransformTranslate(theTransform, 0, -theSpaceAboveTheMovie);

而且它们都可以旋转和全屏显示

你可以在这里下载我的示例代码。


0
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];

这段代码可能会对你有所帮助。


0

试试这个

 - (void)willEnterFullscreen:(NSNotification*)notification {
     NSLog(@"willEnterFullscreen");

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
  }

 - (void)enteredFullscreen:(NSNotification*)notification {
     NSLog(@"enteredFullscreen");
 }

- (void)willExitFullscreen:(NSNotification*)notification {
    NSLog(@"willExitFullscreen");

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];


}

- (void)exitedFullscreen:(NSNotification*)notification {
    NSLog(@"exitedFullscreen");

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接