4

I am using YTPlayerView inside UICollectionView(Horizontal scroll) with one item visible at a time on the screen. I have added a UIView inside custom cell and changed the class name to YTPlayerView in Identity inspector.

For YTPlayerView I have enabled AutoPlay and it is playing well. But, when I scroll to next or previous item I want to stop the player. I tried stopping the player in 'didEndDisplayingCell' by getting custom cell object with NSIndexPath. But YTPlayerView instance with custom cell object returns (null) in 'didEndDisplayingCell'. So I am not able to stop video play. How can I stop video play when scroll to next / prev item?

And I have 3 different custom UICollectionView cells to display text, images and videos (YTPlayerView or AVPlayer).

EDIT I tried to get the custom video cell object in 'cellForItemAtIndexPath' by saving previous visible cell indexpath. And I am able to get the custom cell object, but when I try to get the instance of YTPlayerView it is returning new object for YTPlayerView.

Can anyone suggest me the right way to handle youtube videos in UICollectionView.

SriKanth
  • 459
  • 7
  • 27
  • In your `UICollectionViewCell`, in `prepareForReuse`, can you stop the player? – Larme May 17 '16 at 09:40
  • Do you have a custom `UICollectionViewCell` class? If yes, you can override `prepareForReuse`. – Larme May 17 '16 at 09:45
  • `prepareForReuse` not getting called because I have 3 different cells 1) Image 2)Text 3)Youtube video. Can u pls suggest any other option @Larme – SriKanth May 17 '16 at 10:58
  • prepareForReuse is going to get called if you subclass your YouTube video cell – Lefteris May 17 '16 at 17:18
  • yes, `prepareForReuse` is getting called if I have Youtube videos (Custom video cells) continuously without any text or images cells. `YTPlayerView` automatically stopped the previous video if the next/prev cell also displays youtube video otherwise it is not stopping to play. @Lefteris – SriKanth May 19 '16 at 06:27

1 Answers1

1

Ok,

Now I've understood the problem, however when I try my self, I get a perfectly valid cell reference in the - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath delegate.

It shouldn't be null there. You should be getting this each time a cell disappears, where you should check the type of the cell that is hiding!

If the cell is a type of your YouTube cell then only stop the video playback. Here is a rough sample:

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell isKindOfClass:[YouTubeVideoCell class]]) {
        YouTubeVideoCell *youtubeCell = (YouTubeVideoCell*)cell;
        [youtubeCell stopVideo];
    }
}

Where YouTubeVideoCell should be your custom YTPlayerView cell subclass and stopVideo should be a public method of the cell where inside it you stop the YTPlayerView video.

Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • Instead of `[cell stopVideo]` , I stopped the video by `[cell.youTubePlayerView pauseVideo]`. `youTubePlayerView` is an instance of player view in custom cell. @Lefteris – SriKanth May 19 '16 at 09:29
  • Works both ways. If you are making the YTPlayerView, public in the cell, that's perfectly fine! – Lefteris May 19 '16 at 10:56