ZFMixViewController.m 8.7 KB

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