MPMoviePlayerViewController完成按钮不会删除播放器

时间:2013-02-08 13:10:53

标签: iphone ios mpmovieplayercontroller

我的应用必须在Ios5及更高版本中运行。我正在向mainWindow添加MPMoviePlayerViewController当点击一个按钮时,moviePlayerController的Done按钮没有从窗口中删除moviePlayerController。

我的代码是

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"001 ATSW" ofType:@"m4v"]];
   self.player =  [[MPMoviePlayerViewController alloc]
                initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self         selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

[self.player.view setFrame:[UIScreen mainScreen].bounds];
[[[UIApplication sharedApplication] keyWindow] addSubview:self.player.view];
[[self.player moviePlayer] play];
  }
 -(void)videoPlayBackDidFinish:(NSNotification*)notification  {

  [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

[self.player.moviePlayer stop];
self.player = nil;
[self.player.view removeFromSuperview];
  }

如何在单击“完成”按钮时删除moviePlayerController。

请帮助。

2 个答案:

答案 0 :(得分:0)

如文件中所述:

  

要关闭模态显示的电影播放器​​视图控制器,请调用dismissMoviePlayerViewControllerAnimated方法。

与Apple的其他预配置控制器(例如邮件撰写控制器或图像选择器控制器)一样,您有责任解除控制器。

答案 1 :(得分:0)

您可能需要做的是实际使用

presentMoviePlayerViewControllerAnimated:(MPMoviePlayerViewController *)playerVC

显示MPMoviewPlayerViewController的方法,而不是直接插入视图。因为MPMoviewPlayerViewController在按下完成按钮时试图解除自己。从我所看到的情况来看,没有好的方法来听取这一点并像电影结束那样去做。

这可能取决于您的应用的构建方式,但有点像

[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentMoviePlayerViewControllerAnimated:player];

您应该能够替换所有代码以插入视图maualy,然后只使用

[[[[UIApplication sharedApplication] keyWindow] rootViewController] dismissMoviePlayerViewControllerAnimated];