如何在 iOS 的 UITableViewCell 中播放视频(自动播放)
Posted
技术标签:
【中文标题】如何在 iOS 的 UITableViewCell 中播放视频(自动播放)【英文标题】:How do I play video (autoplay) in UITableViewCell in iOS 【发布时间】:2013-09-28 12:00:48 【问题描述】:我正在UITableViewCell
播放视频。为此,我使用以下代码:
- (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;
视频可以正常播放,但我想一次只播放一个视频。播放完全可见的单元格的视频。
【问题讨论】:
请查看以下链接:***.com/q/34767717/6088680 @San007 你的代码会自动播放单元格中的所有视频吗? 【参考方案1】:使用这两种滚动和处理播放视频的方法。当tableview停止滚动时,这两种方法都会以任何一种方式调用
-(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
【讨论】:
什么是converRect方法?【参考方案2】: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];
【讨论】:
我不想在点击事件上做,我想在单元格可见时自动播放视频。 因此,如果你想这样玩,你不应该在 cellforrowatindexpath 中编写代码,而是在 viewWillAppear 中编写代码,并为数组使用 for 循环并从数组的索引播放它。获取歌曲的全部时间,并在它完成时从该数组中加载下一首歌曲。仅将表格用于显示目的,其中包含一些正在播放歌曲的逻辑。 你没有得到我的问题,谢谢你的努力。以上是关于如何在 iOS 的 UITableViewCell 中播放视频(自动播放)的主要内容,如果未能解决你的问题,请参考以下文章
如何在 UITableViewCell 后台实现实时 iOS 7 模糊效果
如何在 iOS 中异步向 UITableViewCell 添加视图?
如何在 iOS 13.3 上使用 xib 显示 UITableViewCell?
如何阻止 UITableView 在 iOS 7 中剪辑 UITableViewCell 内容