iOS 7 MPMoviePlayerController搜索转发按钮将视频带到结束并显示黑屏

时间:2013-10-24 15:57:21

标签: ios objective-c mpmovieplayercontroller

我在iOS 7中遇到了MPMoviePlayerController的问题。我进入全屏,然后在搜索转发按钮(>> |)上点击(只需点击一下),视频播放结束并给出黑屏标题上的“正在加载”文字。

我注册了“ MPMoviePlayerPlaybackStateDidChangeNotification ”的通知。

**[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayerPlaybackStateDidChange:)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:self.player];**

单击向前搜索按钮不会被触发。

同时注册“ MPMoviePlayerPlaybackDidFinishNotification

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

单击“搜索转发”按钮时,我收到“ MPMovieFinishReasonPlaybackEnded ”事件。 谁知道原因?这是苹果中的一个错误吗?

我需要停止在单击时显示黑屏的行为,或者只是禁用单击“向前搜索”按钮以便不会发生任何事情。

任何人都知道如何实现这个目标吗?

3 个答案:

答案 0 :(得分:2)

我通过完全删除MPMoviePlayer对象,将其设置为nil,将其从超级视图中删除并使用原始视频Url重新添加来修复此问题。代码如下:

- (void)addPlayerForUrl:(NSURL *)url {
  self.player = [[MPMoviePlayerController alloc] initWithContentURL:url];
  self.player.view.frame = self.videoView.bounds;
  self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  self.player.controlStyle = MPMovieControlStyleDefault;
  [self.videoView insertSubview:self.player.view atIndex:0];

  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(moviePlayerLoadStateDidChangedNotification:)
                                               name:MPMoviePlayerReadyForDisplayDidChangeNotification
                                             object:self.player];

  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(moviePlayerPlaybackStateDidChangeNotification:)
                                               name:MPMoviePlayerPlaybackStateDidChangeNotification
                                             object:self.player];
}


#pragma mark - Notifications

- (void)moviePlayerLoadStateDidChangedNotification:(NSNotification *)notification {
  self.isVideoPreloaded = YES;
  self.videoPlayButton.hidden = YES;
  self.photoImageView.hidden = YES;
  self.videoLoadingImageView.hidden = YES;
}
- (void)moviePlayerPlaybackStateDidChangeNotification:(NSNotification *)notification {
      NSURL *url = self.player.contentURL;
      switch (self.player.playbackState) {
        case MPMoviePlaybackStateSeekingBackward:
        case MPMoviePlaybackStateSeekingForward:
          break;
        case MPMoviePlaybackStatePlaying:
          self.videoPlayButton.hidden = YES;

          if (!self.isVideoPreloaded) {
            self.videoLoadingImageView.hidden = NO;
            [self.videoLoadingImageView startAnimating];
          } else {
            self.videoLoadingImageView.hidden = YES;
          }
          break;
        case MPMoviePlaybackStatePaused:
        case MPMoviePlaybackStateStopped:
          self.videoPlayButton.hidden = NO;
          self.videoLoadingImageView.hidden = YES;
          [self.player endSeeking];
          [self.player.view removeFromSuperview];
          [self.player setFullscreen:NO];
          self.player = nil;
          [self addPlayerForUrl:url];
          break;
        default:
          break;
      }
    }

注意我如何在moviePlayerPlaybackStateDidChangeNotification中的switch语句之前保留NSURL。这样,我可以重新初始化并重新添加MPMoviePlayer对象。

不过,如果你想知道的话,我的mpmovieplayer就在tableviewCell上。希望这有帮助,如果您有疑问,请告诉我。祝你好运!

答案 1 :(得分:2)

单击快进或快退按钮时,将调用MPMoviePlayerLoadStateDidChangeNotification。你应该检查一下loadState,然后再给它一个视频的路径,再给它准备一次。

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

    MPMoviePlayerController *moviePlayer = notification.object;
    MPMovieLoadState loadState = moviePlayer.loadState;

    if(loadState == MPMovieLoadStateUnknown) {
        moviePlayer.contentURL = [NSURL fileURLWithPath:videoPath]
        [moviePlayer prepareToPlay];
    }

    .....
}

答案 2 :(得分:0)

你获得MPMovieFinishReasonPlaybackEnded的原因是因为播放到达了视频的结尾(对不起,如果这很明显)。因此,您的寻求前瞻性行动似乎一直在寻找视频的结尾。您可以使用MPMoviePlaybackStateSeekingForward检查播放状态。

快速解决方案可能是创建自己的前进按钮,在指定的时间(即5秒)内向前搜索。但也许这不是您正在寻找的功能。