ZFAutoPlayerViewController.m 8.5 KB

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