在播放错误时关闭MPMoviePlayerViewController

时间:2010-10-25 21:02:50

标签: iphone dismiss mpmovieplayer mpmoviewcontroller

我的MPMoviePlayerViewController有问题:如果控制器无法在指定的网址找到电影,则会显示一个白色的屏幕,我无法将其关闭。

这就是我启动电影播放器​​的方式:

- (void) playVideo:(NSString*)path 
{
 NSURL* url = [NSURL URLWithString:path];

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

 double osversion = [[[UIDevice currentDevice] systemVersion] doubleValue];
 if (osversion >= 3.2) 
 {
  mplayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

  if (mplayerVC)
  {
   mplayerVC.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
   [mplayerVC.moviePlayer play];
   mplayerVC.moviePlayer.shouldAutoplay = TRUE;

  [self presentMoviePlayerViewControllerAnimated:mplayerVC];

  //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];    
  }

 }  
}

这就是moviePlayBackDidFinish:方法看起来像这样的


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

     NSError* error = [[notification userInfo] valueForKey:@"error"];
     if (error != nil)
     {
      // Movie ended with an error
      DLog(@"error=%@", error);
     }
     else 
     {
      // Movie ended successfully
     }

     [self dismissMoviePlayerViewControllerAnimated];
     SAFE_DEL(mplayerVC);
    }

只有在URL指向错误时才会出现白屏

1 个答案:

答案 0 :(得分:3)

别介意的人,我明白了。

显然在moviePlaybackDidFinish方法中你必须调用

[player stop];
在解雇控制器之前

上面,播放器是这样获得的MPMoviePlayerController对象:

MPMoviePlayerController *player = [notification object];