MPMoviePlayer完成按钮单击更改UIView高度

时间:2011-11-29 16:50:51

标签: iphone xcode cocoa-touch ios4

我正在使用MPMoviePlayer来显示视频。我进入全屏,当点击完成按钮时,我希望它从我的视图中删除整个电影播放器​​。

现在,当我单击完成按钮时它会关闭MPMoviePlayer但它会改变我的UIView高度的大小。有没有解决办法删除MPMoviePlayer而不影响我的视图大小。

1 个答案:

答案 0 :(得分:2)

创建一个videoPlayer视图控制器(MPMoviePlayerViewController子类)并将其添加到根视图控制器。

videoPlayerViewController = [[VideoViewController alloc] initWithContentURL:[NSURL fileURLWithPath:resourcePath]];
[rootViewController presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
videoPlayer.controlStyle = MPMovieControlStyleFullscreen;

然后添加通知:

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

然后是收到通知时调用的方法。

- (void) stopVideo:(NSNotification*) aNotification {
  [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:[videoPlayerViewController moviePlayer]];
  [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:[videoPlayerViewController moviePlayer]];
  MPMoviePlayerController *player = [aNotification object];
  [player stop];
}