13

我在 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 ”事件。有谁知道原因吗?这是苹果的错误吗?

我需要停止这种在单击时显示黑屏的行为,或者只是禁用单击前进按钮,这样就不会发生任何事情。

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

4

3 回答 3

2

当您单击快进或快退按钮时,将调用 MPMoviePlayerLoadStateDidChangeNotification。您应该检查 loadState 并为其提供视频的路径并再次 prepareToPlay。

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

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

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

    .....
}
于 2014-10-31T07:31:01.497 回答
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 上。希望这会有所帮助,如果您有任何问题,请告诉我。祝你好运!

于 2014-08-26T21:10:25.947 回答
0

你得到 MPMovieFinishReasonPlaybackEnded 的原因是因为播放到了视频的结尾(如果这很明显,很抱歉)。因此,您的前向搜索操作似乎一直在搜索视频的结尾。您可以使用 来检查播放状态MPMoviePlaybackStateSeekingForward

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

于 2013-10-24T16:48:48.670 回答