退出全屏后MPMoviePlayerController缩放模式问题

时间:2014-01-09 15:15:07

标签: ios iphone objective-c mpmovieplayercontroller mpmoviewcontroller

我只是使用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”模式。我的代码出错了。请帮助我......

3 个答案:

答案 0 :(得分:3)

我相信这会生成MPMoviePlayerScalingModeDidChangeNotification

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

来源:Apple Doc

<强> MPMoviePlayerScalingModeDidChangeNotification

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

答案 1 :(得分:3)

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

Swift Code:

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
}

答案 2 :(得分:1)

这不是“理想”的解决方案,但它有效!基本上,一旦你退出全屏幕,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开发中一个非常常见的错误)