电影播放器​​不停在表格单元格中

时间:2014-02-20 07:30:37

标签: ios objective-c uitableview mpmovieplayercontroller

在我的项目中,当单元格可见视频播放时,在自定义单元格上显示视频。

- (void)checkVisibilityOfCell:(CustomCell *)cell inScrollView:(UIScrollView *)aScrollView {

      CGRect cellRect = [aScrollView convertRect:cell.frame toView:aScrollView.superview];

      if (CGRectContainsRect(aScrollView.frame, cellRect)){
          videoPlayingCell  = (CustomCell *)cell;
          [cell notifyCompletelyVisible];
      }else{
          [cell notifyNotCompletelyVisible];
      }
}

在“notifyCompletelyVisible”方法调用时,完全可见的单元格和自定义单元格视频自动播放即可正常工作但是当我尝试去其他视图或标签时视频连续播放我也停止视频时消失了表视图但它正在播放< / p>

-(void)notifyCompletelyVisible
{
    NSURL *aUrl = [NSURL URLWithString:[dictObj valueForKey:@"videourl"]];
    if([self isAlreadySeenVideo:[dictObj valueForKey:@"videourl"]])
    {
        NSURL *docUrl = [NSURL fileURLWithPath:[self getVideofromDoc:[dictObj valueForKey:@"videourl"]]];
        _moviePlayer =  [[MPMoviePlayerController alloc]initWithContentURL:docUrl];

    } else {

        _moviePlayer =  [[MPMoviePlayerController alloc]initWithContentURL:aUrl];
    }
    asyvideoImage.hidden = YES;

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

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieDidExitFullScreen:)                                                  name:MPMoviePlayerDidExitFullscreenNotification                                                object:_moviePlayer];

    _moviePlayer.controlStyle = MPMovieControlStyleNone;
    _moviePlayer.shouldAutoplay = YES;
    _moviePlayer.view.frame = asyvideoImage.frame;
    _moviePlayer.scalingMode=MPMovieScalingModeFill;
    [self.contentView insertSubview:_moviePlayer.view belowSubview:tvImageView];
     [_moviePlayer prepareToPlay];
     [_moviePlayer setFullscreen:NO animated:NO];

    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(pauseVideo:)];
    [self addGestureRecognizer:singleFingerTap];


 } 

我的notifyNotCompletelyVisible方法是

 -(void)notifyNotCompletelyVisible;
 {
      [_moviePlayer stop];
      [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(movieDidExitFullScreen:)                                                  name:MPMoviePlayerDidExitFullscreenNotification                                                object:_moviePlayer];
}

所以当我转到另一个视图或标签时如何停止视频

感谢

1 个答案:

答案 0 :(得分:0)

只需在您需要的UIViewController中创建一个电影播放器​​实例,并确保在导航到另一个VC或标签时该实例被破坏。