Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YTPlayerView inside UITableViewCell

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.

like image 919
pprochazka72 Avatar asked Apr 28 '14 15:04

pprochazka72


3 Answers

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.

like image 109
JAL Avatar answered Nov 04 '22 11:11

JAL


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.

like image 41
chanil Avatar answered Nov 04 '22 11:11

chanil


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
    }

}
like image 2
Ugo Marinelli Avatar answered Nov 04 '22 12:11

Ugo Marinelli