YTPlayerView里面的UITableViewCell

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

Youtube APIbuild议当通过调用[cell.youtubeView cueVideoById:f.videoID startSeconds:0 suggestedQuality:kYTPlaybackQualityHighRes];重新使用YTPlayerView加载内容时[cell.youtubeView cueVideoById:f.videoID startSeconds:0 suggestedQuality:kYTPlaybackQualityHighRes]; 代替。 不幸的是,这种方法根本不会加载YouTube内容。

有人遇到同样的问题? 任何已知的scheme?

我想加载第一次与loadWithVideoId然后cueVideoById内容,但没有奏效。

我有在UIColectionViewCell实例中使用YTPlayerView实例的这个问题。 最终,我必须做的是停止didEndDisplayingCell[self.playerView stopVideo] )中的video。 然后在prepareForReuse (或者可选地在cellForRowAtIndexPath ), playerView你的playerView ,并在cellForRowAtIndexPath重新初始化播放器,并通过id加载video。

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

在我的情况下,当YTPlayerView被绘制在重用的 UICollectionViewCell中(在第一个启动的单元格中正确加载)时,它变成黑色。

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

我检查了白色 YTPlayerView是不是加载的video,所以我build议检查-loadWithVideoId:或在某个地方正确调用类似的方法。

Swift 3

我遇到过同样的问题。 我的问题是方法加载(withVideoId:videoId)多次调用(因为cellForRow调用多次滚动)。

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

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

 var isAlreadyPlaying : Bool = false 

2)当你想播放你的video,我们设置我们的布尔值,以避免播放几次:

 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 } } 

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

考虑到你只能播放一个video,因为两个YTPlayer的几个实例不会同时播放,你只需要隐藏重复使用的单元格与video的缩略图。 只需将UIImageView放在UITollectionViewCell xib的子类中的YTplayerView上面,并在其中设置缩略图:

 -(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文件,你只需要实现一个button,点击时启动video…

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

…并configuration这个自定义单元格的可重用性,完美地为本地obvjects工作:

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

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

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