如何在iOS中的UITableViewCell中播放视频(自动播放) [英] How do I play video (autoplay) in UITableViewCell in iOS

查看:845
本文介绍了如何在iOS中的UITableViewCell中播放视频(自动播放)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 UITableViewCell 中播放视频。为此,我使用以下代码:

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.

推荐答案

使用这两种方法滚动和处理播放视频。当tableview停止滚动时,这两种方法将以任一方式调用

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
        }
    }
}

这篇关于如何在iOS中的UITableViewCell中播放视频(自动播放)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆