【问题标题】:MPMoviePlayerController scaling mode issues after exit full screen退出全屏后 MPMoviePlayerController 缩放模式问题
【发布时间】:2015-08-08 10:26:02
【问题描述】:

我只是使用 MPMoviePlayerController 播放视频...我的代码是

-(void)playMovie:(NSURL *)url
{
    moviePlayer =
    [[MPMoviePlayerController alloc]
     initWithContentURL:url];
    if (IDIOM==IPAD) {
        [moviePlayer.view setFrame:CGRectMake(22,100, 720, 300)];
    }
    else
    {
        (IS_IPHONE_5)? [moviePlayer.view setFrame:CGRectMake(22, 70, 280, 150)]:[moviePlayer.view setFrame:CGRectMake(22, 40, 260, 140)];
    }
    [_scrollView addSubview:moviePlayer.view];
    moviePlayer.scalingMode =MPMovieScalingModeFill;
    [moviePlayer prepareToPlay];
    [moviePlayer play];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidEnterFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidExitFullScreen:) name:MPMoviePlayerDidExitFullscreenNotification object:Nil];

}

-(void)moviePlayerDidEnterFullscreen :(id)sender
{
    NSLog(@"fullscreen");
   [moviePlayer play];
    moviePlayer.scalingMode =MPMovieScalingModeFill;

}

- (void) moviePlayerDidExitFullScreen:(id)sender {

    NSLog(@"exit full screen");
    [moviePlayer play];
    moviePlayer.scalingMode =MPMovieScalingModeFill;

}

在这里,当我最初播放视频时,视频将处于“MPMovieScalingModeFill”模式...但我的问题是,如果我按全屏,它会全屏显示视频..当我按退出“全屏”时,我的视频模式将变为到“MPMovieScalingModeAspectFit”模式。但我需要始终处于“MPMovieScalingModeFill”模式。我的代码有什么问题..请帮助我...

【问题讨论】:

  • 您不应在全屏通知中触发play。
  • 但它停止播放所以我在全屏通知中再次调用播放
  • 这完全没有意义。从全屏切换或切换到全屏时,它不应停止播放。您的代码中一定有一些您没有向我们展示的内容会停止播放。我的猜测是,你的“viewWill/DidAppear”/“viewWill/DidDisappear”中有一些东西——这些外观通知在离开或进入全屏时触发,因为全屏模式完全隐藏了你的 viewController 的视图。
  • 是的,我在 DidDisappear 中做到了在我离开控制器时停止播放器,还有其他更好的方法可以在我离开控制器时停止播放器吗?
  • 一种常见的方法是在您的viewDidDisappear 中检查MPMoviePlayerController 的fullscreen 属性。如果fullscreen 处于活动状态,请不要调用stop。

标签: ios iphone objective-c mpmovieplayercontroller mpmoviewcontroller


【解决方案1】:

我相信这会生成MPMoviePlayerScalingModeDidChangeNotification。

[[NSNotificationCenter defaultCenter] addObserver:self 
                selector:@selector(movieScalingModeDidChange:) 
                name:MPMoviePlayerScalingModeDidChangeNotification 
                object:nil];

来源:Apple Doc

MPMoviePlayerScalingModeDidChangeNotification

当电影播放器​​的缩放模式发生变化时发布。没有 userInfo 字典。 缩放模式可以以编程方式或通过用户交互进行更改。要设置或检索电影播放器​​的缩放模式,请访问其 scalingMode 属性。状态已更改的电影播放器​​可用作与通知关联的对象。

【讨论】:

    【解决方案2】:

    先将 ScalingMode 设置为 None,然后将 ScalingMode 设置为 AspectFill

    Swift 代码:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "moviePlayerExitFullscreen:", name: MPMoviePlayerDidExitFullscreenNotification, object: self.moviePlayer)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "moviePlayerEnterFullscreen:", name: MPMoviePlayerWillEnterFullscreenNotification, object: self.moviePlayer)
    
    func moviePlayerEnterFullscreen (notification : NSNotification)
    {
        self.moviePlayer.scalingMode = MPMovieScalingMode.None
        self.moviePlayer.scalingMode = MPMovieScalingMode.AspectFill
    }
    
    func moviePlayerExitFullscreen (notification : NSNotification)
    {
        self.moviePlayer.scalingMode = MPMovieScalingMode.None
        self.moviePlayer.scalingMode = MPMovieScalingMode.AspectFill
    }
    

    【讨论】:

      【解决方案3】:

      这不是“理想”的解决方案,但它确实有效!基本上,一旦您退出全屏,MPMoviePlayerController 实例就会全部搞砸,并将缩放属性重置为 MPMovieScalingModeFill 无论何时何地都无济于事(我尝试了各种各样的东西,一个小时后放弃了)。最简单的解决方案是删除 MPMoviePlayerController 并在每次退出全屏时简单地分配一个新的 MPMoviePlayerController 实例(不理想,但完全有效):

      - (void)viewDidAppear:(BOOL)animated
      {
          [super viewDidAppear:NO];
          if (self.moviePlayer != nil)
              [self.moviePlayer.view removeFromSuperview];
          self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL];
          self.moviePlayer.view.frame = CGRectMake(#, #, #, #);
          self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
          self.moviePlayer.shouldAutoplay = NO;
          [self.moviePlayer setContentURL:self.videoURL];
          [self.moviePlayer prepareToPlay];
          [self.moviePlayer setScalingMode:MPMovieScalingModeFill];
          [self.view addSubview:self.moviePlayer.view];
      }
      

      PS:不要忘记在 viewDidAppear 上调用 super 或遭受各种不可预见的混乱(iOS 开发中很常见的错误)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        相关资源
        最近更新 更多