ZFCollectionViewController.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // ZFCollectionViewController.m
  3. // ZFPlayer_Example
  4. //
  5. // Created by 任子丰 on 2018/6/21.
  6. // Copyright © 2018年 紫枫. All rights reserved.
  7. //
  8. #import "ZFCollectionViewController.h"
  9. #import "ZFCollectionViewCell.h"
  10. #import "ZFTableData.h"
  11. #import <ZFPlayer/ZFAVPlayerManager.h>
  12. #import <ZFPlayer/ZFPlayerControlView.h>
  13. #import <ZFPlayer/UIView+ZFFrame.h>
  14. #import <ZFPlayer/ZFPlayerConst.h>
  15. static NSString * const reuseIdentifier = @"collectionViewCell";
  16. @interface ZFCollectionViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
  17. @property (nonatomic, strong) NSMutableArray <ZFTableData *>*dataSource;
  18. @property (nonatomic, strong) UICollectionView *collectionView;
  19. @property (nonatomic, strong) ZFPlayerController *player;
  20. @property (nonatomic, strong) ZFPlayerControlView *controlView;
  21. @end
  22. @implementation ZFCollectionViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. self.view.backgroundColor = [UIColor whiteColor];
  26. [self.view addSubview:self.collectionView];
  27. [self requestData];
  28. /// playerManager
  29. ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
  30. // ZFIJKPlayerManager *playerManager = [[ZFIJKPlayerManager alloc] init];
  31. /// player的tag值必须在cell里设置
  32. self.player = [ZFPlayerController playerWithScrollView:self.collectionView playerManager:playerManager containerViewTag:kPlayerViewTag];
  33. self.player.controlView = self.controlView;
  34. self.player.shouldAutoPlay = YES;
  35. @zf_weakify(self)
  36. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  37. @zf_strongify(self)
  38. if (self.player.playingIndexPath.row < self.dataSource.count - 1) {
  39. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.player.playingIndexPath.row+1 inSection:0];
  40. [self playTheVideoAtIndexPath:indexPath scrollAnimated:YES];
  41. } else {
  42. [self.player.currentPlayerManager replay];
  43. }
  44. };
  45. /// 停止的时候找出最合适的播放
  46. self.player.zf_scrollViewDidEndScrollingCallback = ^(NSIndexPath * _Nonnull indexPath) {
  47. @zf_strongify(self)
  48. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  49. };
  50. /*
  51. /// 滑动中找到适合的就自动播放
  52. /// 如果是停止后再寻找播放可以忽略这个回调
  53. /// 如果在滑动中就要寻找到播放的indexPath,并且开始播放,那就要这样写
  54. self.player.zf_playerShouldPlayInScrollView = ^(NSIndexPath * _Nonnull indexPath) {
  55. @zf_strongify(self)
  56. if ([indexPath compare:self.player.playingIndexPath] != NSOrderedSame) {
  57. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  58. }
  59. };
  60. */
  61. }
  62. - (void)viewWillLayoutSubviews {
  63. [super viewWillLayoutSubviews];
  64. self.collectionView.frame = self.view.bounds;
  65. }
  66. - (void)viewDidAppear:(BOOL)animated {
  67. [super viewDidAppear:animated];
  68. @zf_weakify(self)
  69. [self.player zf_filterShouldPlayCellWhileScrolled:^(NSIndexPath *indexPath) {
  70. @zf_strongify(self)
  71. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  72. }];
  73. }
  74. #pragma mark - 转屏和状态栏
  75. - (BOOL)shouldAutorotate {
  76. return NO;
  77. }
  78. - (UIStatusBarStyle)preferredStatusBarStyle {
  79. return UIStatusBarStyleDefault;
  80. }
  81. - (BOOL)prefersStatusBarHidden {
  82. return NO;
  83. }
  84. #pragma mark - private method
  85. - (void)requestData {
  86. NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
  87. NSData *data = [NSData dataWithContentsOfFile:path];
  88. NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  89. self.dataSource = @[].mutableCopy;
  90. NSArray *videoList = [rootDict objectForKey:@"list"];
  91. for (NSDictionary *dataDic in videoList) {
  92. ZFTableData *data = [[ZFTableData alloc] init];
  93. [data setValuesForKeysWithDictionary:dataDic];
  94. [self.dataSource addObject:data];
  95. }
  96. }
  97. /// play the video
  98. - (void)playTheVideoAtIndexPath:(NSIndexPath *)indexPath scrollAnimated:(BOOL)animated {
  99. ZFTableData *data = self.dataSource[indexPath.row];
  100. if (animated) {
  101. [self.player playTheIndexPath:indexPath assetURL:[NSURL URLWithString:data.video_url] scrollPosition:ZFPlayerScrollViewScrollPositionCenteredVertically animated:YES];
  102. } else {
  103. [self.player playTheIndexPath:indexPath assetURL:[NSURL URLWithString:data.video_url]];
  104. }
  105. [self.controlView showTitle:data.title
  106. coverURLString:data.thumbnail_url
  107. fullScreenMode:ZFFullScreenModeLandscape];
  108. }
  109. #pragma mark - UIScrollViewDelegate 列表播放必须实现
  110. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  111. [scrollView zf_scrollViewDidEndDecelerating];
  112. }
  113. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  114. [scrollView zf_scrollViewDidEndDraggingWillDecelerate:decelerate];
  115. }
  116. - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
  117. [scrollView zf_scrollViewDidScrollToTop];
  118. }
  119. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  120. [scrollView zf_scrollViewDidScroll];
  121. }
  122. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  123. [scrollView zf_scrollViewWillBeginDragging];
  124. }
  125. #pragma mark UICollectionViewDataSource
  126. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  127. return self.dataSource.count;
  128. }
  129. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  130. ZFCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
  131. cell.data = self.dataSource[indexPath.row];
  132. @zf_weakify(self)
  133. cell.playBlock = ^(UIButton *sender) {
  134. @zf_strongify(self)
  135. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  136. };
  137. return cell;
  138. }
  139. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  140. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  141. }
  142. - (UICollectionView *)collectionView {
  143. if (!_collectionView) {
  144. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  145. CGFloat margin = 5;
  146. CGFloat itemWidth = self.view.frame.size.width;
  147. CGFloat itemHeight = itemWidth*9/16 + 30;
  148. layout.itemSize = CGSizeMake(itemWidth, itemHeight);
  149. layout.sectionInset = UIEdgeInsetsMake(10, margin, 10, margin);
  150. layout.minimumLineSpacing = 5;
  151. layout.minimumInteritemSpacing = 5;
  152. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  153. _collectionView.delegate = self;
  154. _collectionView.dataSource = self;
  155. _collectionView.backgroundColor = [UIColor whiteColor];
  156. [_collectionView registerClass:[ZFCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
  157. }
  158. return _collectionView;
  159. }
  160. - (ZFPlayerControlView *)controlView {
  161. if (!_controlView) {
  162. _controlView = [ZFPlayerControlView new];
  163. }
  164. return _controlView;
  165. }
  166. @end