【问题标题】:TableView Cell video autoplayTableView Cell 视频自动播放
【发布时间】:2016-04-18 11:42:45
【问题描述】:

我做了什么:-

我添加了在单元格完全可见时播放视频的代码 当我向下或向上滚动时,它会再次重新加载 tableview 并播放 再看视频。但是,我的要求不同。

我真正想要的:

我想播放视频直到完全向后或向前单元格 可见的。当用户向下或向上滚动时,它不会影响,直到 向后或向前单元格完全可见。

设计

Table Cell Layout Description
-> Video Table Cell (Fix height 393) 
    -> Content View
    -> Main view - (as per Content view of Table view Cell)
        -> Title View (0, 0, MainView.width, 57)
        -> Video View (0, 57, MainView.width, 200);
        -> Description View (0, 257, MainView.width, 136) 

编码:

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];

    }
}

【问题讨论】:

标签: ios objective-c uitableview uiscrollview tableviewcell


【解决方案1】:

每个UITableViewCell 子类都有一个方法prepareForReuse:,所以在你的VideoTableCell 中添加一个方法:

-(void)prepareForReuse {
   [super prepareForReuse];
   [self.videoPlayer play];
}

在你的控制器中实现一个委托方法:- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

   [cell stopVideo];
}

stopVideo 中添加[self.videoPlayer pause][cell.videoPlayer stop]

【讨论】:

    【解决方案2】:

    为 UITableViewCell 编写一个类别以检查单元格是否完全可见:

    UITableViewCell+FullyVisible

     -(BOOL) isFullyVisible {
    
    UITableView *tableview = (UITableView *)[self parents:[UITableView class]];
    CGRect rect = [tableview rectForRowAtIndexPath:[tableview indexPathForCell:self]];
    rect = [tableview convertRect:rect toView:tableview.superview];
    return CGRectContainsRect(tableview.frame, rect);
    }
    

    cellForRowAtIndexpath: 中检查单元格是否完全可见。如果单元格完全可见,则播放视频,否则停止/暂停。

    【讨论】:

      【解决方案3】:

      在 UITableView 中试试这个方法

      func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
          // stop video
      }
      
      func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
          // play video
      }
      

      应该能帮到你。

      如果这不能完全满足您的要求,请尝试覆盖 scrollViewDidScroll more details there

      【讨论】:

        猜你喜欢
        • 2015-10-11
        • 1970-01-01
        • 2013-02-13
        • 2021-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多