7

I am playing a video in UITableViewCell. For that I am using the following code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *VideoCellIdentifier = @"VideoCell";

    NSDictionary *_response_data = [self.response objectAtIndex:indexPath.row];

    VideoCustomCell *cell = (VideoCustomCell *) [tableView dequeueReusableCellWithIdentifier:VideoCellIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects;
        topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"VideoCustomCell" owner:self options:nil];
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (VideoCustomCell *) currentObject;
                cell.delegate  = self;
                break;
            }
        }
    }

    avPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:[_response_data valueForKey:@"media_id"]]] retain];
    avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
    avPlayerLayer.frame = cell.video_player_view.layer.bounds;
    avPlayerLayer.videoGravity = AVLayerVideoGravityResize;
    [cell.video_player_view.layer addSublayer: avPlayerLayer];
    [avPlayer play];

    return cell;
}

The video is playing properly, but I want to play only one video at a time. Play the video of the cell which is fully visible.

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
San007
  • 762
  • 1
  • 9
  • 30

2 Answers2

3

Use this two method of scrolling and handle play video. This two method will call in either way when tableview stop scrolling

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if(![scrollView isDecelerating] && ![scrollView isDragging]){

        [self playVideo];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if(!decelerate){

        [self playVideo];
    }
}


-(void)playVideo
{
    if(aryTableData.count==0){
        return;
    }

    for(UITableViewCell *cell in [tblView visibleCells])
    {
        VideoCell *ccell = (VideoCell*)cell;

        CGRect ccellRect = [APP_DEL.window convertRect:ccell.bounds fromView:ccell];

        // NSLog(@"--Cell frame %f",ccellRect.origin.y);

        //Set Condition of cell visible within some range
        if(ccellRect.origin.y>-200)
        {
            // Handle Video Play

            int row = [[tblView indexPathForCell:ccell] row];
            NSString *strUrl = [[aryTableData objectAtIndex:row] valueForKey:@"video_url"] ;
            [ccell startVideoFromURL:strUrl]; //You can handle video play in cell or table view
        }
    }
}
Kiran Patel
  • 944
  • 10
  • 16
-5

VideoTableCell.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface VideoTableCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIView *viewForVideo;
@property (strong, nonatomic) IBOutlet UIImageView *imgThumb;
@property (strong, nonatomic) IBOutlet UIButton *btnPlay;
@property (strong, nonatomic) AVPlayerItem* videoItem;
@property (strong, nonatomic) AVPlayer* videoPlayer;
@property (strong, nonatomic) AVPlayerLayer* avLayer;

@end

VideoTableCell.m

#import "VideoTableCell.h"

@implementation VideoTableCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.avLayer setFrame:CGRectMake(self.viewForVideo.frame.origin.x, self.viewForVideo.frame.origin.y, self.viewForVideo.frame.size.width,  self.viewForVideo.frame.size.height)];
}

@end

VideoVC.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface VideoVC : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tblData;

@end

VideoVC.m

#import "VideoVC.h"
#import "VideoTableCell.h"

@interface VideoVC ()
{
    NSArray *arrVideo ;
    bool isScrolling;
    int index;
    BOOL fullvisible ;
}
@end  

@implementation VideoVC

- (void)viewDidLoad
{
    [super viewDidLoad];    

    arrVideo = [[NSArray alloc]initWithObjects:@"http://video/1.mp4",@"http://video/2.mp4", @"http://video/3.mp4", @"http://video/4.mp4", @"http://video/5.mp4", nil];
    fullvisible = YES;   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrVideo.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoTableCell"];
    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"VideoTableCell" owner:self options:nil]objectAtIndex:0];
    }

    int temp =  [self getVisibleIndex];
    if (temp == indexPath.row && fullvisible)
    {
        cell.imgThumb.hidden = YES ;
        //NSLog(@"fullvisible == 1");
        NSURL *url = [NSURL URLWithString:[arrVideo objectAtIndex:indexPath.row]];

        cell.videoItem = [AVPlayerItem playerItemWithURL:url];
        cell.videoPlayer = [AVPlayer playerWithPlayerItem:cell.videoItem];
        cell.avLayer = [AVPlayerLayer playerLayerWithPlayer:cell.videoPlayer];

        [cell.avLayer setBackgroundColor:[UIColor whiteColor].CGColor];
        // [cell.avLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
        [cell.contentView.layer addSublayer:cell.avLayer];
        [cell.videoPlayer play];

        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    else
    {
        cell.imgThumb.hidden = NO ;
        cell.videoPlayer = nil;
        [cell.avLayer removeFromSuperlayer];
        cell.videoItem = nil;
        [cell.videoPlayer pause];
    }
    return cell ;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 393 ;
}

- (int)getVisibleIndex
{   
    for (NSIndexPath *indexPath in _tblData.indexPathsForVisibleRows)                                     
    {
        CGRect cellRect = [_tblData rectForRowAtIndexPath:indexPath];
        BOOL isVisible = CGRectContainsRect(_tblData.bounds, cellRect);
        if (isVisible)
        {
            index = (int)indexPath.row ;
        }
    }
    return index ;
}

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{

    NSArray* cells = _tblData.visibleCells;
    for (VideoTableCell* cell in cells)
    {
            NSIndexPath *path = [_tblData indexPathForCell:cell];
            index = (int) path.row;
            fullvisible = YES;
            [_tblData reloadData];
    }
}
abdullahselek
  • 7,893
  • 3
  • 50
  • 40
user2826529
  • 179
  • 2
  • I don't want to do on click event,I want to auto play video when cell visible. – San007 Sep 28 '13 at 17:04
  • So if you want to play like that you should not write your code in cellforrowatindexpath and write it in viewWillAppear and take for loop for array and play it from index of array. Get the full time of song and as it completes load next song from that array. Use table only for display purpose with some logic that wich song is playing. – user2826529 Sep 29 '13 at 05:26
  • 1
    You are not getting my problem,Thanks for your effort. – San007 Sep 29 '13 at 10:14