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.
I had this problem using YTPlayerView instances in UIColectionViewCell instances. Ultimately what I had to do was stop the video in didEndDisplayingCell ([self.playerView stopVideo]). Then in prepareForReuse (or optionally in cellForRowAtIndexPath), nil out your playerView, and in cellForRowAtIndexPath 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
var isAlreadyPlaying : Bool = false
2) When you want to play your video, we set our boolean to avoid playing it several times :
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With