3

我只是使用 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”模式。我的代码有什么问题。请帮助我...

4

3 回答 3

3

我相信这会产生MPMoviePlayerScalingModeDidChangeNotification.

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

资料来源:苹果文档

MPMoviePlayerScalingModeDidChangeNotification

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

于 2014-01-09T16:07:10.523 回答
3

首先将 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
}
于 2015-08-05T06:44:36.377 回答
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 开发中非常常见的错误)

于 2014-02-23T20:47:57.253 回答