OGeek|极客世界-中国程序员成长平台

标题: ios - 如何防止单元格的 avplayer - Tableview 重新初始化 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:31
标题: ios - 如何防止单元格的 avplayer - Tableview 重新初始化

我陷入了一个大问题。 我有一个带有视频播放器的 tableview - AVPlayer 我有 10 个单元格,每个单元格都在播放视频。但问题是当我 scroll tableview 播放器重新初始化并从中播放视频 开始。我想阻止这种重新初始化并播放像推特这样的视频 我们离开的最后一场比赛应该尝试什么代码?我尝试了多个代码但没有成功

    Code is here

    if (Arr_avPlayer.count > indexPath.row){
        Cell_TVC * cell = (Cell_TVC *)[Arr_avPlayer 
      objectAtIndex:indexPath.row];
        return cell;
    }

     //    Cell_TVC *cell = (Cell_TVC *)[tableView 
        dequeueReusableCellWithIdentifier"Cell_TVC" 
        forIndexPath:indexPath];

         **Cell_TVC *cell = [[Cell_TVC alloc] init];**

           //    Cell_TVC *cell =  [tableView 

                 dequeueReusableCellWithIdentifier"Cell_TVC"];
          //    if (!cell) {
          //        cell = [[Cell_TVC 

                 alloc]initWithStyle:UITableViewCellStyleDefault 
                         reuseIdentifier"Cell_TVC"];
         //              
         //    }



      AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL 
      fileURLWithPath:[self filepath:[myIntegers 
      objectAtIndex:indexPath.row]]] options:nil];

      cell.avPlayerItem = [[AVPlayerItem alloc] initWithAsset:asset];
      cell.avPlayer = [[AVPlayer alloc] 
      initWithPlayerItem:cell.avPlayerItem];

    cell.avPlayerLayer =  [[AVPlayerLayer alloc] init];
    cell.avPlayerLayer = [AVPlayerLayer 
    playerLayerWithPlayer:cell.avPlayer];
    cell.lblTime.text = @"0.0";

    CGRect contentViewFrame = cell.contentView.frame;
    contentViewFrame.origin.x = 0;
    contentViewFrame.origin.y = 25;
    contentViewFrame.size.width = APP_width;
    contentViewFrame.size.height = APP_width * 0.55;
    cell.contentView.frame = contentViewFrame;
    cell.contentView.backgroundColor = [UIColor redColor];

    UILabel *lbl = [[UILabel 
     alloc]initWithFrame:CGRectMake((cell.contentView.frame.size.width / 2 
    ) - 100, contentViewFrame.size.height + 10, 200, 100)];

    //lbl.text = [NSString stringWithFormat"%02f:%02f", dMinutes11, 
                 dSeconds11];

    lbl.tag = indexPath.row;

    [cell.avPlayerLayer setFrame:cell.contentView.frame];
    [cell.contentView.layer addSublayer:cell.avPlayerLayer];

    [Arr_avPlayer insertObject:cell atIndex:indexPath.row];
    [cell.contentView addSubview:lbl];


    if i write the cell like below

    Cell_TVC *cell = [[Cell_TVC alloc] init];

     i am not able to acess the property of cell and when i use like 
    below

     if (!cell)
     {
               cell = [[Cell_TVC 
                 alloc]initWithStyle:UITableViewCellStyleDefault 
                 reuseIdentifier"Cell_TVC"];

     }

播放器索引更改和视频获取更改其索引 如何解决这个问题?



Best Answer-推荐答案


如果您使用的是 UItableview,它的行为很明显。 UItableview 不会将所有单元格保存在内存中。随着单元格从屏幕上消失,tableview 将其删除并在您向下/向上滚动时再次添加(当新单元格出现在屏幕上时)。 您无法更改 UItableview 的这种行为。

有一个丑陋的解决方案可用:将您的表格 View 调整为 height = HeightOfCell * numberOfCell. 但不建议这样做。因为这可能会造成内存问题。

更好的解决方案是跟踪 Seek 时间。您可以使用 didEndDisplayingCell将 Player 的当前寻道时间的值存储在将删除的单元格中。当该索引路径的单元格再次出现时(cellforrow),您可以将播放器搜索到存储的值。

关于ios - 如何防止单元格的 avplayer - Tableview 重新初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43518825/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4