【问题标题】:YTPlayerView inside UITableViewCellUITableViewCell 内的 YTPlayerView
【发布时间】:2014-06-14 06:20:45
【问题描述】:

我的 UITableViewCell 子类中有一个 YTPlayerView。在我的 UIViewController 中,我从 tableViewDelagate willDisplayCell 方法中调用 [cell.youtubeView loadWithVideoId:f.videoID];。问题是,当我的 tableView 中有许多单元格时,其中一些保持白色而不是 YouTube 内容!

Youtube API recommends重用YTPlayerView加载内容时调用 [cell.youtubeView cueVideoById:f.videoID startSeconds:0 suggestedQuality:kYTPlaybackQualityHighRes]; 代替。不幸的是,这种方法根本不加载 YouTube 内容。

有人遇到过同样的问题吗?任何已知的解决方案?

我想在第一次加载内容时使用 loadWithVideoId 然后 cueVideoById 但这没有用。

【问题讨论】:

    标签: ios uitableview youtube youtube-api ytplayerview


    【解决方案1】:

    斯威夫特 3

    我遇到了同样的问题。我的问题是方法 load(withVideoId: videoId) 被多次调用(因为 cellForRow 在滚动时被多次调用)。

    我通过创建一个布尔值并确保 load(withVideoId: videoId) 只被调用一次来解决这个问题:

    1) 在单元格类中,创建一个全局布尔值

    var isAlreadyPlaying : Bool = false
    

    2)当您想播放视频时,我们会设置布尔值以避免多次播放:

    func loadVideo() {
    
        //Making sure we are not playing the video several time
        if isAlreadyPlaying == false {
    
            //Creating vars (optional)
            let playerVars = ["playsinline": 1, "autoplay": 1, "autohide": 1, "controls" : 1, "showinfo" : 0, "modestbranding" : 1, "rel" : 0]
    
            // Launching the video 
            youtTubeView?.load(withVideoId: videoId, playerVars : playerVars)
    
            // Force to not play the video again
            isAlreadyPlaying = true
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      这是另一种方法,纯粹主义者不会喜欢它,但我不知道如何正确释放/重新初始化在我的自定义 UICollectionViewCell 中创建的 YTPlayerView。

      考虑到您一次只能播放一个视频,因为 YTPlayer 的两个多个实例不会同时播放,您只需要隐藏带有视频缩略图的重复使用的单元格。只需在 UICollectionViewCell xib 的子类中的 YTplayerView 上方放置一个 UIImageView,并在其中设置缩略图:

      -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
      
         //... load customcell xib, manage cellId etc. 
      
         // set video id that will be read by the custom cell 
         [cell setVideoId:cellVideoId];
      
         // set above UIImage 
         NSData * thumbnailData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: videoThumbnailURL]];
         UIImage * videoThumbnail = [UIImage imageWithData: thumbnailData];
         [cell.thumbnailImage setImage:videoThumbnail];
         return cell;
      }
      

      在那里,在自定义 UICollectionViewCell .m 文件中,您只需要实现一个按钮,当点击时启动视频...

      - (IBAction)clickPlay:(id)sender {
          if (videoId != nil){        
              [ccellYTPlayerView loadWithVideoId:videoId playerVars:playerVars];
      
              // Hide thumbnail image to reveal the YTPlayerView
              [thumbnailImage setHidden:true];
          }
      }
      

      ... 并配置此自定义单元格的可重用性,这对于本机对象完美无缺:

      -(void) prepareForReuse{
          [super prepareForReuse];
      
          // preparing UIImage to host another thumbnail
          thumbnailImage.image = nil;
          [thumbnailImage setHidden:false];
      }
      

      这样,YTplayers 可以出现在其他单元格中,没有人会看到它,因为它被缩略图隐藏了,并且产生的声音不是问题,因为用户会假设它来自原始单元格(不再出现在屏幕上)因为滚动)。

      我知道这不是很干净,但到目前为止它对我有帮助,YTPlayer 实例并不那么容易回收。

      【讨论】:

        【解决方案3】:

        我在 UICollectionView 中使用了 YTPlayerView(与您的情况类似)。

        在我的例子中,YTPlayerView 在 reused UICollectionViewCell 中绘制时变黑(在第一次启动的单元格上正确加载)。

        YTPlayerView 变黑是因为-loadWithVideoId:playerVars: 在第一次调用的方法完成之前被调用了两次。

        我检查了 white YTPlayerView 是针对未加载的视频,所以我建议检查-loadWithVideoId: 或在某处正确调用的类似方法。

        【讨论】:

        • 我确认黑色的情况。
        【解决方案4】:

        我在UIColectionViewCell 实例中使用YTPlayerView 实例时遇到了这个问题。最终我要做的是停止didEndDisplayingCell ([self.playerView stopVideo]) 中的视频。然后在prepareForReuse(或可选在cellForRowAtIndexPath)中,清空你的playerView,在cellForRowAtIndexPath中重新初始化播放器并通过id加载视频。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-12-22
          • 2014-10-12
          • 2019-11-27
          • 2013-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多