gpt4 book ai didi

ios - 如何在 iOS 的 UITableViewCell 中播放视频(自动播放)

转载 作者:可可西里 更新时间:2023-11-01 03:35:21 25 4
gpt4 key购买 nike

我正在 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;
}

视频播放正常,但我只想一次播放一个视频。播放完全可见的单元格视频。

最佳答案

使用滚动和处理播放视频这两种方法。当 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
}
}
}

关于ios - 如何在 iOS 的 UITableViewCell 中播放视频(自动播放),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19066713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com