2016-01-13 156 views
18

我做了什麼: -的TableView細胞視頻自動播放

我已經添加了播放視頻的代碼時,細胞完全可見 ,當我滾動向上或向下再次和戲劇重裝的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]; 

    } 
} 
+0

也許通過設置'actionAtItemEnd'暫停,與國際志願者組織檢查停頓的狀態,如果'player.currentTime == player.currentItem檢查.duration'(這就是你想要的),如果是這樣的話,就玩下一個項目。換句話說,在物品之間創建一個微暫停... – Larme

+0

試試這個答案: [http://stackoverflow.com/questions/9831485/best-way-to-check-if-uitableviewcell-is-completely-visible ](http://stackoverflow.com/questions/9831485/best-way-to-check-if-uitableviewcell-is-completely-visible) –

回答

14

每個UITableViewCell子類有你VideoTableCell的方法prepareForReuse:所以只需添加一個方法:

-(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:檢查,如果電池是完全可見。如果單元格完全可見,則播放視頻,否則停止/暫停。

4

試試這個方法的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