微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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];

    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐