I have a YTPlayerView inside of my subclass of UITableViewCell. In my UIViewController I call [cell.youtubeView loadWithVideoId:f.videoID];
from my tableViewDelagate willDisplayCell
method. The problem is that when I have many cells in my tableView some of them stay white instead of the YouTube content!
Youtube API recommends when reusing the YTPlayerView to load the content by calling
[cell.youtubeView cueVideoById:f.videoID startSeconds:0 suggestedQuality:kYTPlaybackQualityHighRes];
instead. Unfortunately, this method doesn't load YouTube content at all.
Anybody came across the same issue? Any known solution?
I was thinking to load first time the content with loadWithVideoId
and then cueVideoById
but that didn't work.
Here is another approach, purists won't like it, but I couldn't find out how to properly free/re-init the YTPlayerView created in my custom UICollectionViewCell.
Considering you will only play one video at time, because two several instances of YTPlayer won't play simultaneously, you just have to hide reused cells with video's thumbnail. Simply put an UIImageView above the YTplayerView in your sublclass of UICollectionViewCell xib, and set the thumbnail in it:
There, in custom UICollectionViewCell .m file you just need to implement a button that launch the video when clicked ...
... and configure reusability of this custom cell, which works flawlessly for native obvjects :
This way, YTplayers can appear in other cells, no one will see it because hidden by the thumbnail, and the sound produced is not a problem because user will assume that it comes from the original cell (which is not on screen anymore because of scrolling).
I know that's not really clean, but it helped me so far, YTPlayer instance is not so easy to recycle.
I had this problem using
YTPlayerView
instances inUIColectionViewCell
instances. Ultimately what I had to do was stop the video indidEndDisplayingCell
([self.playerView stopVideo]
). Then inprepareForReuse
(or optionally incellForRowAtIndexPath
), nil out yourplayerView
, and incellForRowAtIndexPath
re-init the player and load the video by id.I used YTPlayerView in UICollectionView(Similar to your case).
In my case, YTPlayerView got black when it was drawn in reused UICollectionViewCell(load properly on firstly initiated cell).
YTPlayerView got black is because
-loadWithVideoId:playerVars:
is called twice before the firstly invoked method is complete.I checked white YTPlayerView is for not loaded video, so I recommend checking
-loadWithVideoId:
or similar methods called properly in somewhere.Swift 3
I had the same issue. My problem was that the method load(withVideoId: videoId) was called several times (because of the cellForRow called several times while scrolling).
I solved this by creating a Boolean and making sure the load(withVideoId: videoId) is called only once :
1) In the cell class, create a global Boolean
2) When you want to play your video, we set our boolean to avoid playing it several times :