6

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

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

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

我正在考虑第一次加载内容,loadWithVideoId然后cueVideoById,但没有奏效。

4

4 回答 4

2

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

于 2015-05-02T18:51:45.230 回答
2

斯威夫特 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
    }

}
于 2017-07-12T13:04:46.460 回答
2

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

就我而言,YTPlayerView 在重用的UICollectionViewCell 中绘制时变黑(在第一次启动的单元格上正确加载)。

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

我检查了白色YTPlayerView 是否未加载视频,因此我建议检查-loadWithVideoId:或在某处正确调用的类似方法。

于 2015-09-24T22:57:50.243 回答
0

这是另一种方法,纯粹主义者不会喜欢它,但我不知道如何正确释放/重新初始化在我的自定义 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 实例并不那么容易回收。

于 2016-11-08T05:34:08.317 回答