iOS 7 MPMoviePlayerController寻找前进按钮将视频带到结束并显示黑屏

前端之家收集整理的这篇文章主要介绍了iOS 7 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”事件触发了单击寻找前进按钮.
任何人都知道为什么?这是苹果的错误吗?

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

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

解决方法

我通过完全删除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;
      }
    }

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

Btw,我的mpmovieplayer是在tableviewCell,如果你想知道.希望这有帮助,让我知道如果你有问题.祝你好运!

猜你在找的iOS相关文章