Play video on UITableViewCell when it is completely visible

You can use

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    isScrolling = NO;
    [homeTabl reloadData];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)aScrollView
{
    isScrolling = YES;
    [homeTabl reloadData];
    index = -1;
}

And in cellForRowatindexpath

if (fullvisible && index == indexPath.row) {
        if (!isScrolling) {

            NSLog(@"video index---%d",indexPath.row);

            if (index == indexPath.row) {
                NSLog(@"video index---%d",indexPath.row);
                cell.videoActivity.hidden = NO;

                // if (index == indexPath.row) {
                NSURL *url = [NSURL URLWithString:[[responsearray objectAtIndex:indexPath.row]valueForKey:@"feed_video"]];

                dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

                dispatch_async(queue, ^{
                    cell.videoItem = [AVPlayerItem playerItemWithURL:url];

                    dispatch_sync(dispatch_get_main_queue(), ^{
                        cell.videoPlayer = [AVPlayer playerWithPlayerItem:cell.videoItem];
                        cell.avLayer = [AVPlayerLayer playerLayerWithPlayer:cell.videoPlayer];
                        cell.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
                        [cell.videoItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
                        [cell.videoItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];

                        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidBufferPlaying:) name:AVPlayerItemPlaybackStalledNotification object:nil];
                        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

                        cell.avLayer.frame = CGRectMake(5, 9, 310, 310);
                        [cell.contentView.layer addSublayer:  cell.avLayer];
                        [ cell.videoPlayer play];
                        [cell.contentView addSubview:cell.videoActivity];
                    });
                });
                //                }
                //                else{
                //                    cell.videoActivity.hidden = YES;
                //                    cell.videoPlayer = nil;
                //                    [cell.avLayer removeFromSuperlayer];
                //                    cell.videoItem = nil;
                //                    [cell.videoPlayer pause];
                //                }
            }
        }}

    else{

        cell.videoActivity.hidden = YES;
        cell.videoPlayer = nil;
        [cell.avLayer removeFromSuperlayer];
        cell.videoItem = nil;
        [cell.videoPlayer pause];

    }

Leave a Comment