MPMoviePlayerController有时会播放音频,而不是淡入时的视频

时间:2014-11-04 13:15:54

标签: ios objective-c uiscrollview mpmovieplayercontroller

我有一个问题,在UIScrollView中我有很多视图(下面的视频控制器),它们充当视频内容的页面(最初显示缩略图视图)。

有时(但并非总是)当我滚动到下一页时,视频的音频开始播放但我没有视觉效果。当我没有淡化视图时,这似乎不会发生。

视频初始化代码为:

  MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] init];

  self.moviePlayer = moviePlayer;

  [moviePlayer setControlStyle:MPMovieControlStyleNone];
  [moviePlayer setRepeatMode:MPMovieRepeatModeOne];
  [moviePlayer.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

  moviePlayer.backgroundView.backgroundColor = [UIColor clearColor];
  moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
  moviePlayer.contentURL = [NSURL URLWithString:[videoData.video src]];

  [self.videoController.view addSubview:self.moviePlayer.view];
  [self.videoController.view bringSubviewToFront:moviePlayer.view];
  [moviePlayer.view setAlpha:0.0f];

  // Movie Observer
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkMovieStatus:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

  // Add tap handler
  UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onPlayerTapped:)];
  singleFingerTap.numberOfTapsRequired = 1;
  singleFingerTap.delegate = self;
  [self.moviePlayer.view addGestureRecognizer:singleFingerTap];

  [moviePlayer prepareToPlay];

播放代码(试图避免视频开头的黑色闪光)是:

- (void)checkMovieStatus:(NSNotification *)notification {
    if(self.moviePlayer.readyForDisplay && (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK)) {

          [self.moviePlayer play];

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

          [UIView animateWithDuration:0.4f animations:^{
              [self.moviePlayer.view setAlpha:1.0f];
          } completion:^(BOOL finished) {
              //[self.moviePlayer play]; // tried here as well
          }];
        }
}

删除滚动视频视图的代码是:

- (void)removeMoviePlayer {
        if(self.moviePlayer) {
                [self.moviePlayer stop];

                [self.moviePlayer.view removeFromSuperview];

                self.moviePlayer = nil;
        }
}

1 个答案:

答案 0 :(得分:0)

我发现了一个相当奇怪的解决方案。在我的情况下,我会滑动到视频,音频将播放,没有图片。如果我擦掉并回来,一切都运行良好,只是为了它的乐趣,我尝试了这个

MPMoviePlayerController *mp = [self.arrayOfScrollView objectAtIndex:xx];
[mp play];
[mp stop];
[mp play];

奇怪的是有效。自从您六个月前发布以来,如果您找到了另一种解决方法,我很乐意了解它。