ZFNotAutoPlayViewController.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // ZFNotAutoPlayViewController.m
  3. // ZFPlayer
  4. //
  5. // Created by 任子丰 on 2018/4/1.
  6. // Copyright © 2018年 紫枫. All rights reserved.
  7. //
  8. #import "ZFNotAutoPlayViewController.h"
  9. #import <ZFPlayer/ZFAVPlayerManager.h>
  10. #import <ZFPlayer/ZFPlayerControlView.h>
  11. #import <ZFPlayer/ZFIJKPlayerManager.h>
  12. #import "ZFPlayerDetailViewController.h"
  13. #import <ZFPlayer/UIView+ZFFrame.h>
  14. #import <ZFPlayer/ZFPlayerConst.h>
  15. #import "ZFTableViewCell.h"
  16. #import "ZFTableData.h"
  17. static NSString *kIdentifier = @"kIdentifier";
  18. @interface ZFNotAutoPlayViewController () <UITableViewDelegate,UITableViewDataSource,ZFTableViewCellDelegate>
  19. @property (nonatomic, strong) UITableView *tableView;
  20. @property (nonatomic, strong) ZFPlayerController *player;
  21. @property (nonatomic, strong) ZFPlayerControlView *controlView;
  22. @property (nonatomic, strong) NSMutableArray *dataSource;
  23. @end
  24. @implementation ZFNotAutoPlayViewController
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. self.view.backgroundColor = [UIColor whiteColor];
  28. [self.view addSubview:self.tableView];
  29. [self requestData];
  30. /// playerManager
  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. self.player.shouldAutoPlay = NO;
  37. /// 1.0是完全消失的时候
  38. self.player.playerDisapperaPercent = 1.0;
  39. @zf_weakify(self)
  40. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  41. @zf_strongify(self)
  42. [self.player stopCurrentPlayingCell];
  43. };
  44. }
  45. - (void)viewWillAppear:(BOOL)animated {
  46. [super viewWillAppear:animated];
  47. self.player.viewControllerDisappear = NO;
  48. }
  49. - (void)viewWillDisappear:(BOOL)animated {
  50. [super viewWillDisappear:animated];
  51. self.player.viewControllerDisappear = YES;
  52. }
  53. - (void)viewWillLayoutSubviews {
  54. [super viewWillLayoutSubviews];
  55. CGFloat y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
  56. CGFloat h = CGRectGetMaxY(self.view.frame);
  57. self.tableView.frame = CGRectMake(0, y, self.view.frame.size.width, h-y);
  58. }
  59. - (void)requestData {
  60. NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
  61. NSData *data = [NSData dataWithContentsOfFile:path];
  62. NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  63. self.dataSource = @[].mutableCopy;
  64. NSArray *videoList = [rootDict objectForKey:@"list"];
  65. for (NSDictionary *dataDic in videoList) {
  66. ZFTableData *data = [[ZFTableData alloc] init];
  67. [data setValuesForKeysWithDictionary:dataDic];
  68. ZFTableViewCellLayout *layout = [[ZFTableViewCellLayout alloc] initWithData:data];
  69. [self.dataSource addObject:layout];
  70. }
  71. }
  72. - (BOOL)shouldAutorotate {
  73. return NO;
  74. }
  75. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  76. return UIInterfaceOrientationMaskPortrait;
  77. }
  78. - (UIStatusBarStyle)preferredStatusBarStyle {
  79. return UIStatusBarStyleDefault;
  80. }
  81. - (BOOL)prefersStatusBarHidden {
  82. return NO;
  83. }
  84. #pragma mark - UIScrollViewDelegate 列表播放必须实现
  85. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  86. [scrollView zf_scrollViewDidEndDecelerating];
  87. }
  88. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  89. [scrollView zf_scrollViewDidEndDraggingWillDecelerate:decelerate];
  90. }
  91. - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
  92. [scrollView zf_scrollViewDidScrollToTop];
  93. }
  94. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  95. [scrollView zf_scrollViewDidScroll];
  96. }
  97. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  98. [scrollView zf_scrollViewWillBeginDragging];
  99. }
  100. #pragma mark - UITableViewDataSource
  101. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  102. return self.dataSource.count;
  103. }
  104. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  105. ZFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier];
  106. [cell setDelegate:self withIndexPath:indexPath];
  107. cell.layout = self.dataSource[indexPath.row];
  108. [cell setNormalMode];
  109. return cell;
  110. }
  111. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  112. /// 如果正在播放的index和当前点击的index不同,则停止当前播放的index
  113. if (self.player.playingIndexPath != indexPath) {
  114. [self.player stopCurrentPlayingCell];
  115. }
  116. /// 如果没有播放,则点击进详情页会自动播放
  117. if (!self.player.currentPlayerManager.isPlaying) {
  118. [self playTheVideoAtIndexPath:indexPath];
  119. }
  120. /// 到详情页
  121. ZFPlayerDetailViewController *detailVC = [ZFPlayerDetailViewController new];
  122. detailVC.player = self.player;
  123. /// 详情页返回的回调
  124. detailVC.detailVCPopCallback = ^{
  125. if (self.player.currentPlayerManager.playState == ZFPlayerPlayStatePlayStopped) {
  126. [self.player stopCurrentPlayingCell];
  127. } else {
  128. [self.player addPlayerViewToCell];
  129. }
  130. };
  131. /// 详情页点击播放的回调
  132. detailVC.detailVCPlayCallback = ^{
  133. [self zf_playTheVideoAtIndexPath:indexPath];
  134. };
  135. [self.navigationController pushViewController:detailVC animated:YES];
  136. }
  137. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  138. ZFTableViewCellLayout *layout = self.dataSource[indexPath.row];
  139. return layout.height;
  140. }
  141. #pragma mark - ZFTableViewCellDelegate
  142. - (void)zf_playTheVideoAtIndexPath:(NSIndexPath *)indexPath {
  143. [self playTheVideoAtIndexPath:indexPath];
  144. }
  145. #pragma mark - private method
  146. /// play the video
  147. - (void)playTheVideoAtIndexPath:(NSIndexPath *)indexPath {
  148. ZFTableViewCellLayout *layout = self.dataSource[indexPath.row];
  149. [self.player playTheIndexPath:indexPath assetURL:[NSURL URLWithString:layout.data.video_url]];
  150. [self.controlView showTitle:layout.data.title
  151. coverURLString:layout.data.thumbnail_url
  152. fullScreenMode:layout.isVerticalVideo?ZFFullScreenModePortrait:ZFFullScreenModeLandscape];
  153. }
  154. #pragma mark - getter
  155. - (UITableView *)tableView {
  156. if (!_tableView) {
  157. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  158. [_tableView registerClass:[ZFTableViewCell class] forCellReuseIdentifier:kIdentifier];
  159. _tableView.delegate = self;
  160. _tableView.dataSource = self;
  161. if (@available(iOS 11.0, *)) {
  162. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  163. } else {
  164. self.automaticallyAdjustsScrollViewInsets = NO;
  165. }
  166. _tableView.estimatedRowHeight = 0;
  167. _tableView.estimatedSectionFooterHeight = 0;
  168. _tableView.estimatedSectionHeaderHeight = 0;
  169. }
  170. return _tableView;
  171. }
  172. - (ZFPlayerControlView *)controlView {
  173. if (!_controlView) {
  174. _controlView = [ZFPlayerControlView new];
  175. _controlView.prepareShowLoading = YES;
  176. _controlView.prepareShowControlView = YES;
  177. _controlView.showCustomStatusBar = YES;
  178. }
  179. return _controlView;
  180. }
  181. @end