ios – 在UITableViewCell完全可见时播放视频

我有一个包含视频URL的数组,我想在UITableviewcell完全可见时播放这些视频.

我试过这个

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView 
{
    NSArray* cells = hoMetabl.visibleCells;

    for (HomeCell* cell in cells) 
    {
        if (cell.frame.origin.y > offset.y &&
        cell.frame.origin.y + cell.frame.size.height < offset.y + bounds.size.height)
        {
            NSIndexPath *path = [hoMetabl indexPathForCell:cell] ;
            index = path.row;
            fullvisible = YES;
            [hoMetabl reloadData];
        }
        else
        {
            fullvisible = NO;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (fullvisible) 
     {
          NSURL *url = [NSURL URLWithString:[[responsearray objectAtIndex:indexPath.row]valueForKey:@"Feed_video"]];

          cell.videoItem = [AVPlayerItem playerItemWithURL:url];
          cell.videoPlayer = [AVPlayer playerWithPlayerItem:cell.videoItem];
          cell.avLayer = [AVPlayerLayer playerLayerWithPlayer:cell.videoPlayer];
          cell.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
          [cell.videoItem addobserver:self forKeyPath:@"playbackBufferEmpty" options:NSkeyvalueObservingOptionNew context:nil];
          [cell.videoItem addobserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSkeyvalueObservingOptionNew context:nil];

          [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(itemDidBufferPlaying:) name:AVPlayerItemPlaybackStallednotification object:nil];
          [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidplayToEndTimeNotification object:nil];

          cell.avLayer.frame = CGRectMake(5,9,310,310);
          [cell.contentView.layer addSublayer:cell.avLayer];
          [cell.videoPlayer play];
          [cell.contentView addSubview:cell.videoActivity];
     }
     else
     {
         cell.videoPlayer = nil;
         [cell.avLayer removeFromSuperlayer];
         cell.videoItem = nil;
         [cell.videoPlayer pause];
     }
}

这工作正常 – 但它可怕减慢表格视图的滚动速度.
请建议我一些其他更好的方法.

解决方法

您可以使用
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    isScrolling = NO;
    [hoMetabl reloadData];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)aScrollView
{
    isScrolling = YES;
    [hoMetabl reloadData];
    index = -1;
}

并在cellForRowatindexpath中

if (fullvisible && index == indexPath.row) {
        if (!isScrolling) {

            NSLog(@"video index---%d",indexPath.row);

            if (index == indexPath.row) {
                NSLog(@"video index---%d",indexPath.row);
                cell.videoActivity.hidden = NO;

                // if (index == indexPath.row) {
                NSURL *url = [NSURL URLWithString:[[responsearray objectAtIndex:indexPath.row]valueForKey:@"Feed_video"]];

                dispatch_queue_t queue = dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_HIGH,0ul);

                dispatch_async(queue,^{
                    cell.videoItem = [AVPlayerItem playerItemWithURL:url];

                    dispatch_sync(dispatch_get_main_queue(),^{
                        cell.videoPlayer = [AVPlayer playerWithPlayerItem:cell.videoItem];
                        cell.avLayer = [AVPlayerLayer playerLayerWithPlayer:cell.videoPlayer];
                        cell.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
                        [cell.videoItem addobserver:self forKeyPath:@"playbackBufferEmpty" options:NSkeyvalueObservingOptionNew context:nil];
                        [cell.videoItem addobserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSkeyvalueObservingOptionNew context:nil];

                        [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(itemDidBufferPlaying:) name:AVPlayerItemPlaybackStallednotification object:nil];
                        [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidplayToEndTimeNotification object:nil];

                        cell.avLayer.frame = CGRectMake(5,310);
                        [cell.contentView.layer addSublayer:  cell.avLayer];
                        [ cell.videoPlayer play];
                        [cell.contentView addSubview:cell.videoActivity];
                    });
                });
                //                }
                //                else{
                //                    cell.videoActivity.hidden = YES;
                //                    cell.videoPlayer = nil;
                //                    [cell.avLayer removeFromSuperlayer];
                //                    cell.videoItem = nil;
                //                    [cell.videoPlayer pause];
                //                }
            }
        }}

    else{

        cell.videoActivity.hidden = YES;
        cell.videoPlayer = nil;
        [cell.avLayer removeFromSuperlayer];
        cell.videoItem = nil;
        [cell.videoPlayer pause];

    }

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...