【问题标题】:Hide StatusBar from MPMoviePlayerController从 MPMoviePlayerController 隐藏状态栏
【发布时间】:2011-04-08 20:51:51
【问题描述】:

我整天都在为一个非常烦人的问题而苦苦挣扎,希望能在这个板上找到帮助。

我正在使用 MPMoviePlayerController 在 iPad 上播放全屏电影,但我不知道如何删除状态栏,尽管我竭尽全力让它下地狱。

这是我用来显示电影的方法的代码:

-(void)launchVideoFromButton:(id)sender{

         NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"movie01" ofType:@"m4v"];
         NSURL *videoPathURL = [NSURL fileURLWithPath:videoPath];
         moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoPathURL];

         [self.view addSubview:moviePlayer.view];

         moviePlayer.shouldAutoplay = YES;
         moviePlayer.movieSourceType = MPMovieSourceTypeFile;


         [moviePlayer setFullscreen:YES animated:YES];
         moviePlayer.controlStyle = MPMovieControlStyleFullscreen;

         NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
         [notificationCenter addObserver:self selector:@selector(moviePlayerEvent:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];

    }



    -(void)moviePlayerEvent:(NSNotification*)aNotification{

         [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
         NSLog(@"%i", [UIApplication sharedApplication].statusBarHidden);

    }

在控制台中,我可以看到当电影出现但状态栏仍然存在时,moviePlayerEvent 被触发:[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO] 似乎无法操作。我一直在尝试使用其他 MPMoviePlayerController 通知,但没有成功。

谁能帮我解决这个问题?

提前致谢。

【问题讨论】:

    标签: iphone ipad mpmovieplayercontroller statusbar


    【解决方案1】:

    不幸的是,在遇到这个问题后,通过研究和大量实验,我确定在全屏模式下隐藏 iOS 状态栏几乎是不可能的。无论您做什么,当显示全屏播放器控件时,状态栏也会显示(它不会尊重setStatusBarHidden:YES)。嵌入式播放器控件不是这种情况,但用户可以轻松地在嵌入式和全屏模式之间切换,因此您不能真正使用它来保持控件显示时没有状态栏。

    当然,至少当控件淡出时状态栏会消失...

    【讨论】:

    • +1 表示正确答案。请提交有关此问题的错误报告。
    • 太笨了,我的状态栏是空的,我无法摆脱它。
    【解决方案2】:

    不要将电影播放器​​的视图添加到您的主视图中;相反,按如下方式显示电影播放器​​(为简洁/清晰起见,省略了一些步骤):

    moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    
    // Register for the playback finished notification.
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(myMovieFinishedCallback:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:moviePlayerViewController.moviePlayer];
    
    
    //Present
        [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
    
        // Play the movie!
        self.moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
        [self.moviePlayerViewController.moviePlayer play];
    
    
    
    // When the movie is done, release the controller.
    -(void)myMovieFinishedCallback:(NSNotification*)aNotification
    {
    
        //NSLog(@"playback terminated");
    
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:moviePlayerViewController.moviePlayer];
    
    
        [moviePlayerViewController release], moviePlayerViewController = nil;
    
    
    }
    

    【讨论】:

      【解决方案3】:

      使用 MPMoviePlayerViewController 对我有用, 设置以下

      [moviePlayerController.moviePlayer setFullscreen:YES animated:NO];
      moviePlayerController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
      

      在此之前:

      [self presentViewController:moviePlayerController animated:NO completion:^{ }];
      

      以及紧随其后的:

      moviePlayerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
      

      以防万一,我也这样做了:

      [[NSNotificationCenter defaultCenter] addObserver:self
                                                   selector:@selector(moviePlayerLoadStateDidChange:)
                                                       name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
      
      ...
      
      
      - (void)moviePlayerLoadStateDidChange:(NSNotification *)notification {
      
      
          if ([[moviePlayerController moviePlayer] loadState] == MPMovieLoadStateStalled) {
      
          } else if([[moviePlayerController moviePlayer] loadState] != MPMovieLoadStateUnknown) {
      
              [moviePlayerController moviePlayer].controlStyle = MPMovieControlStyleNone;
      
              ...
          }
      }
      

      所以,没有状态栏,没有控件……只有纯视频。 )

      (在 iOS 5.1 设备和 6.0 模拟器上测试)。

      【讨论】:

      • 很伤心的消息...我一开始没有注意到,但似乎当我们在电影播放中隐藏所有内容时,电影关闭时不会出现状态栏。这显然是iOS中的一些错误,因为它不能消失,因为我没有直接做任何与它相关的事情......然后当我打开一些模态控制器时它就会出现,例如,它可能会“刷新”应用程序上下文什么的。无论如何......为了让状态栏仍然出现,我只是抓住电影结束的最后一刻并执行 moviePlayerController.movi​​ePlayer.controlStyle = MPMovieControlStyleFullscreen;
      • 我还必须手动停止视频 [moviePlayerController.movi​​ePlayer stop];在手动关闭电影控制器后,否则当控制器已经消失时,我要么留下黑屏,要么在后台播放视频声音。因此,iOS 的行为非常“有趣”。此处描述了我如何捕捉视频“将完成”的那一刻:stackoverflow.com/a/13318267/691660。祝我们所有人好运! )
      【解决方案4】:

      状态栏确实隐藏了,但随着播放控件再次出现。

        -(void)viewDidLoad:{
              [super viewDidLoad];
              MPMoviePlayerViewController *moviePlayerViewController =
                      [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
      
              [[NSNotificationCenter defaultCenter] addObserver:self 
                       selector:@selector(playbackStateChange:)
                       name:MPMoviePlayerLoadStateDidChangeNotification
                       object:moviePlayerViewController.moviePlayer];
          }
          -(void)playbackStateChange:(NSNotification*)notification{
              if([[UIApplication sharedApplication]respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
              [[UIApplication sharedApplication] setStatusBarHidden:YES 
                             withAnimation:UIStatusBarAnimationNone];
              else 
                  [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
      }
      

      【讨论】:

        【解决方案5】:

        使用 MPMovieControlModeVolumeHidden 对我不起作用,唯一起作用的是 MPMovieControlModeVolumeOnly 与全屏视频:

        myMoviePlayer.controlStyle = MPMovieControlModeVolumeOnly;
        [myMoviePlayer setFullscreen:YES];
        

        另外,我将电影视图作为子视图添加到父视图:

        [parentView addSubview:myMoviePlayer.view];
        

        我的应用应该没有状态栏,为了向后兼容,我在应用委托上使用了以下代码:

        if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
            [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
        else
            [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
        

        【讨论】:

          【解决方案6】:

          这不是答案,我遇到了同样的问题。不过有一部分我可以更新..

          状态栏仅在控件显示时显示。

          点击电影,隐藏控件和状态栏,再次点击,显示控件,状态栏也回来了。

          我还在启动电影之前以编程方式隐藏状态栏。

          [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];

          这是我添加电影的方式:

          [[[UIApplication sharedApplication] keyWindow] addSubview: movieView];

          【讨论】:

            【解决方案7】:

            对于那些遇到此问题的人,我找到了自己的解决方案,可能会有所帮助。仅当您的应用程序的其余部分未显示状态栏并且您在电影播放完毕并返回您的界面而不是在播放期间尝试再次隐藏它时才适用。

            如果您的 MPMoviePlayerController 被作为子视图添加到 UIView 中,该 UIView 被推送到导航控制器视图堆栈中,您可以使用该父视图控制器的 viewWillDisappear 方法来帮助您。

            在该方法中,您可以将控件样式更改为无,这将在视图消失之前清除所有电影播放器​​控件,并且如果您已经将其设置为隐藏状态栏,也会将其清除。这对用户来说是完全不可见的,因为视图正在离开屏幕并且他们不再与它进行交互。

            【讨论】:

              【解决方案8】:

              我有同样的问题,但我添加到我的 info.plistStatus bar is initially hidden - Boolean - YES 并且它有效!
              顺便说一句,我使用的是 iOS 5.1,Xcode 4.3.2。

              【讨论】:

              • 这将起作用,除非您想要应用的状态栏,而不是电影。
              • 我将Status bar is initially hidden 设置为YES(OpenGL ES 应用程序),但是当我以模态方式呈现MPMoviePlayerController 时,状态栏会出现。
              • 我也将状态栏隐藏设置为“是”,但我仍然得到一个状态栏。 rcw3的回答是正确的,如果你用的是全屏控件,是不能隐藏状态栏的。
              【解决方案9】:

              我不知道我的解决方案是否适用于您的问题,但它适用于我的设置,即运行 iOS 5.1 的第四代 ipod。

              我的应用程序根本不显示状态栏,并且在 info.plist 文件中相应的条目“状态栏最初隐藏”设置为 YES。

              我还直接将 MPMoviePlayerController 视图添加到其父视图。这是设置电影播放器​​的代码:

              moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theurl];
              [moviePlayer.view setFrame:frame]; // This is set to (0, 0, 320, 480)
              [moviePlayer prepareToPlay];
              [moviePlayer setShouldAutoplay:YES]; 
              moviePlayer.fullscreen = TRUE;
              moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
              [self.view addSubview:moviePlayer.view];
              
              [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; 
              

              moviePlayer 是一个类变量。

              当播放器播放完毕或观众按下moviePlayer控制器的“完成”按钮时,会调用playbackFinished:方法:

              - (void)playBackFinished:(NSNotification *)notif{
              moviePlayer.controlStyle = MPMovieControlStyleNone;
              [moviePlayer stop];
              [moviePlayer.view removeFromSuperview];
              
              [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
              [moviePlayer release];
              moviePlayer = nil;
              }
              

              其中moviePlayer的控件样式设置为MPMovieControlStyleNone以防止任何控件,但本质上是当moviePlayer从其父视图中移除时状态栏不会显示。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2011-04-29
                • 2014-10-30
                • 2017-03-20
                • 2013-11-12
                相关资源
                最近更新 更多