ZFLightTableViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // ZFLightTableViewController.m
  3. // ZFPlayer
  4. //
  5. // Created by 任子丰 on 2018/4/1.
  6. // Copyright © 2018年 紫枫. All rights reserved.
  7. //
  8. #import "ZFLightTableViewController.h"
  9. #import <ZFPlayer/ZFAVPlayerManager.h>
  10. #import <ZFPlayer/ZFIJKPlayerManager.h>
  11. #import <ZFPlayer/ZFPlayerControlView.h>
  12. #import <ZFPlayer/UIView+ZFFrame.h>
  13. #import <ZFPlayer/ZFPlayerConst.h>
  14. #import "ZFPlayerDetailViewController.h"
  15. #import "ZFTableViewCellLayout.h"
  16. #import "ZFTableViewCell.h"
  17. #import "ZFTableData.h"
  18. static NSString *kIdentifier = @"kIdentifier";
  19. @interface ZFLightTableViewController () <UITableViewDelegate,UITableViewDataSource,ZFTableViewCellDelegate>
  20. @property (nonatomic, strong) UITableView *tableView;
  21. @property (nonatomic, strong) ZFPlayerController *player;
  22. @property (nonatomic, strong) ZFPlayerControlView *controlView;
  23. @property (nonatomic, strong) NSMutableArray *dataSource;
  24. @end
  25. @implementation ZFLightTableViewController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. self.view.backgroundColor = [UIColor whiteColor];
  29. [self.view addSubview:self.tableView];
  30. [self requestData];
  31. /// playerManager
  32. ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
  33. // ZFIJKPlayerManager *playerManager = [[ZFIJKPlayerManager alloc] init];
  34. /// player的tag值必须在cell里设置
  35. self.player = [ZFPlayerController playerWithScrollView:self.tableView playerManager:playerManager containerViewTag:kPlayerViewTag];
  36. self.player.controlView = self.controlView;
  37. /// 消失比例停止播放
  38. self.player.playerDisapperaPercent = 0.2;
  39. self.player.playerApperaPercent = 0.8;
  40. /// 移动网络依然自动播放
  41. self.player.WWANAutoPlay = YES;
  42. @zf_weakify(self)
  43. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  44. @zf_strongify(self)
  45. if (self.player.playingIndexPath.row < self.dataSource.count - 1) {
  46. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.player.playingIndexPath.row+1 inSection:0];
  47. [self playTheVideoAtIndexPath:indexPath scrollAnimated:YES];
  48. } else {
  49. [self.player stopCurrentPlayingCell];
  50. }
  51. };
  52. self.player.zf_playerShouldPlayInScrollView = ^(NSIndexPath * _Nonnull indexPath) {
  53. @zf_strongify(self)
  54. if (indexPath == nil) { /// 没有找到可以播放视频
  55. /// 显示黑色蒙版
  56. ZFTableViewCell *cell1 = [self.tableView cellForRowAtIndexPath:self.player.shouldPlayIndexPath];
  57. [cell1 showMaskView];
  58. }
  59. /// 用来控制cell明暗的
  60. if ([indexPath compare:self.player.shouldPlayIndexPath] != NSOrderedSame) {
  61. /// 显示黑色蒙版
  62. ZFTableViewCell *cell1 = [self.tableView cellForRowAtIndexPath:self.player.shouldPlayIndexPath];
  63. [cell1 showMaskView];
  64. /// 隐藏黑色蒙版
  65. ZFTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  66. [cell hideMaskView];
  67. }
  68. /// 如果当前播放的和找到的是同一个indexPath,则忽略
  69. if ([indexPath compare:self.player.playingIndexPath] != NSOrderedSame) {
  70. /// 滑动中找到适合的就自动播放,如果是停止后在寻找播放可以忽略这句
  71. /// 如果在滑动中就要寻找到播放indexPath,并且开始播放,那就要这样写
  72. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  73. }
  74. };
  75. /// 停止的时候找出最合适的播放
  76. self.player.zf_scrollViewDidEndScrollingCallback = ^(NSIndexPath * _Nonnull indexPath) {
  77. @zf_strongify(self)
  78. if (!self.player.playingIndexPath) {
  79. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  80. }
  81. };
  82. }
  83. - (void)viewWillLayoutSubviews {
  84. [super viewWillLayoutSubviews];
  85. CGFloat y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
  86. CGFloat h = CGRectGetMaxY(self.view.frame);
  87. self.tableView.frame = CGRectMake(0, y, self.view.frame.size.width, h-y);
  88. }
  89. - (void)viewDidAppear:(BOOL)animated {
  90. [super viewDidAppear:animated];
  91. @zf_weakify(self)
  92. [self.player zf_filterShouldPlayCellWhileScrolled:^(NSIndexPath *indexPath) {
  93. @zf_strongify(self)
  94. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  95. }];
  96. }
  97. - (void)requestData {
  98. NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
  99. NSData *data = [NSData dataWithContentsOfFile:path];
  100. NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  101. self.dataSource = @[].mutableCopy;
  102. NSArray *videoList = [rootDict objectForKey:@"list"];
  103. for (NSDictionary *dataDic in videoList) {
  104. ZFTableData *data = [[ZFTableData alloc] init];
  105. [data setValuesForKeysWithDictionary:dataDic];
  106. ZFTableViewCellLayout *layout = [[ZFTableViewCellLayout alloc] initWithData:data];
  107. [self.dataSource addObject:layout];
  108. }
  109. }
  110. - (BOOL)shouldAutorotate {
  111. return NO;
  112. }
  113. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  114. return UIInterfaceOrientationMaskPortrait;
  115. }
  116. - (UIStatusBarStyle)preferredStatusBarStyle {
  117. return UIStatusBarStyleDefault;
  118. }
  119. - (BOOL)prefersStatusBarHidden {
  120. return NO;
  121. }
  122. - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
  123. return UIStatusBarAnimationSlide;
  124. }
  125. #pragma mark - UIScrollViewDelegate 列表播放必须实现
  126. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  127. [scrollView zf_scrollViewDidEndDecelerating];
  128. }
  129. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  130. [scrollView zf_scrollViewDidEndDraggingWillDecelerate:decelerate];
  131. }
  132. - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
  133. [scrollView zf_scrollViewDidScrollToTop];
  134. }
  135. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  136. [scrollView zf_scrollViewDidScroll];
  137. }
  138. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  139. [scrollView zf_scrollViewWillBeginDragging];
  140. }
  141. #pragma mark - UITableViewDataSource
  142. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  143. return self.dataSource.count;
  144. }
  145. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  146. ZFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier];
  147. [cell setDelegate:self withIndexPath:indexPath];
  148. cell.layout = self.dataSource[indexPath.row];
  149. return cell;
  150. }
  151. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  152. /// 如果正在播放的index和当前点击的index不同,则停止当前播放的index
  153. if (self.player.playingIndexPath != indexPath) {
  154. [self.player stopCurrentPlayingCell];
  155. }
  156. /// 如果没有播放,则点击进详情页会自动播放
  157. if (!self.player.currentPlayerManager.isPlaying) {
  158. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  159. }
  160. /// 到详情页
  161. ZFPlayerDetailViewController *detailVC = [ZFPlayerDetailViewController new];
  162. detailVC.player = self.player;
  163. /// 详情页返回的回调
  164. detailVC.detailVCPopCallback = ^{
  165. [self.player addPlayerViewToCell];
  166. };
  167. /// 详情页点击播放的回调
  168. detailVC.detailVCPlayCallback = ^{
  169. [self zf_playTheVideoAtIndexPath:indexPath];
  170. };
  171. [self.navigationController pushViewController:detailVC animated:YES];
  172. }
  173. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  174. ZFTableViewCellLayout *layout = self.dataSource[indexPath.row];
  175. return layout.height;
  176. }
  177. #pragma mark - ZFTableViewCellDelegate
  178. - (void)zf_playTheVideoAtIndexPath:(NSIndexPath *)indexPath {
  179. [self playTheVideoAtIndexPath:indexPath scrollAnimated:YES];
  180. }
  181. #pragma mark - private method
  182. /// play the video
  183. - (void)playTheVideoAtIndexPath:(NSIndexPath *)indexPath scrollAnimated:(BOOL)animated {
  184. ZFTableViewCellLayout *layout = self.dataSource[indexPath.row];
  185. if (animated) {
  186. [self.player playTheIndexPath:indexPath assetURL:[NSURL URLWithString:layout.data.video_url] scrollPosition:ZFPlayerScrollViewScrollPositionTop animated:YES];
  187. } else {
  188. [self.player playTheIndexPath:indexPath assetURL:[NSURL URLWithString:layout.data.video_url]];
  189. }
  190. [self.controlView showTitle:layout.data.title
  191. coverURLString:layout.data.thumbnail_url
  192. fullScreenMode:layout.isVerticalVideo?ZFFullScreenModePortrait:ZFFullScreenModeLandscape];
  193. }
  194. #pragma mark - getter
  195. - (UITableView *)tableView {
  196. if (!_tableView) {
  197. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  198. [_tableView registerClass:[ZFTableViewCell class] forCellReuseIdentifier:kIdentifier];
  199. _tableView.delegate = self;
  200. _tableView.dataSource = self;
  201. if (@available(iOS 11.0, *)) {
  202. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  203. } else {
  204. self.automaticallyAdjustsScrollViewInsets = NO;
  205. }
  206. _tableView.separatorColor = [UIColor darkGrayColor];
  207. [[UITableView appearance] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
  208. [[UITableView appearance] setSeparatorInset:UIEdgeInsetsZero];
  209. [[UITableViewCell appearance] setSeparatorInset:UIEdgeInsetsZero];
  210. if ([UITableView instancesRespondToSelector:@selector(setLayoutMargins:)]) {
  211. [[UITableView appearance] setLayoutMargins:UIEdgeInsetsZero];
  212. [[UITableViewCell appearance] setLayoutMargins:UIEdgeInsetsZero];
  213. [[UITableViewCell appearance] setPreservesSuperviewLayoutMargins:NO];
  214. }
  215. _tableView.estimatedRowHeight = 0;
  216. _tableView.estimatedSectionFooterHeight = 0;
  217. _tableView.estimatedSectionHeaderHeight = 0;
  218. }
  219. return _tableView;
  220. }
  221. - (ZFPlayerControlView *)controlView {
  222. if (!_controlView) {
  223. _controlView = [ZFPlayerControlView new];
  224. _controlView.fastViewAnimated = YES;
  225. _controlView.horizontalPanShowControlView = NO;
  226. _controlView.showCustomStatusBar = YES;
  227. }
  228. return _controlView;
  229. }
  230. @end