应用自然大小后,MPMoviePlayerController搜索栏不会出现

时间:2015-03-06 06:28:00

标签: ios objective-c iphone video mpmovieplayercontroller

我正在使用MPMoviePlayerController播放视频文件,并且我可以在MPMovieNaturalSizeAvailableNotification的帮助下应用视频的自然尺寸。但添加此通知后,搜索栏未出现在controlStyle中(所有其他按钮都存在)。如果我对视频帧进行硬编码(即不考虑自然高度),我就不会遇到这个问题。

enter image description here

这是我的代码

// Declartion class level

     MPMoviePlayerController *moviePlayer;    
      float naturalHeight;


- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
   moviewPlayer  = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieReadyForDisplay:)
                                                 name:MPMoviePlayerReadyForDisplayDidChangeNotification
                                               object:moviePlayer];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieNaturalSizeAvailable:)
                                                 name:MPMovieNaturalSizeAvailableNotification
                                               object:moviePlayer];


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

       [self.view addSubview:moviePlayer.view];

        moviePlayer.controlStyle = MPMovieControlStyleFullscreen;


    moviePlayer.fullscreen = NO; 

    moviePlayer.scalingMode = MPMovieScalingModeFill; 

    moviePlayer.shouldAutoplay = NO;



  //I am able to get the seek bar here if I set the static height . But I want natural height here.
   //  moviePlayer.frame = CGRectMake(0, 0, self.view.frame.size.width , naturalHeight);
}

-(void) movieNaturalSizeAvailable:(NSNotification *)notification{

    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMovieNaturalSizeAvailableNotification object:player];

    naturalHeight = player.naturalSize.height;

    if(naturalHeight > self.view.frame.size.height) {
        naturalHeight = self.view.frame.size.height;
    }

}

-(void) movieReadyForDisplay:(NSNotification *)notification{

    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerReadyForDisplayDidChangeNotification object:player];

    NSLog(@">> %f ", naturalHeight); //OK here

    [player view].frame = CGRectMake(0, 0, self.view.frame.size.width , naturalHeight);
    player.view.center = self.view.center;

    [player prepareToPlay];
    [player play];

}


- (void)moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];

    [player stop];
    [player.view removeFromSuperview];

}


@end

我认为这个问题可能是因为自然尺寸的计算 视频播放器在设置其组件帧后发生。如果我不使用自然尺寸,它可以很好地工作。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

试试这个

在开始视频之前

player.controlStyle =  MPMovieControlStyleNone;

当电影开始播放时,请设置

player.controlStyle = MPMovieControlStyleFullscreen;

您可以从MPMediaPlayback协议获得播放回调。