ZFSmallPlayViewController.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //
  2. // ZFSmallPlayViewController.m
  3. // ZFPlayer
  4. //
  5. // Created by 任子丰 on 2018/4/1.
  6. // Copyright © 2018年 紫枫. All rights reserved.
  7. //
  8. #import "ZFSmallPlayViewController.h"
  9. #import <ZFPlayer/ZFAVPlayerManager.h>
  10. #import <ZFPlayer/ZFPlayerControlView.h>
  11. #import <ZFPlayer/ZFIJKPlayerManager.h>
  12. #import <ZFPlayer/UIView+ZFFrame.h>
  13. #import <ZFPlayer/ZFPlayerConst.h>
  14. #import "ZFPlayerDetailViewController.h"
  15. #import "ZFUtilities.h"
  16. #import "ZFTableViewCell.h"
  17. #import "ZFTableData.h"
  18. #import "ZFAutoPlayerViewController.h"
  19. static NSString *kIdentifier = @"kIdentifier";
  20. @interface ZFSmallPlayViewController () <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. @property (nonatomic, strong) UIActivityIndicatorView *activity;
  26. @end
  27. @implementation ZFSmallPlayViewController
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. self.view.backgroundColor = [UIColor whiteColor];
  31. [self.view addSubview:self.tableView];
  32. [self.view addSubview:self.activity];
  33. [self requestData];
  34. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Push" style:UIBarButtonItemStylePlain target:self action:@selector(pushNewVC)];
  35. /// playerManager
  36. ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
  37. // ZFIJKPlayerManager *playerManager = [[ZFIJKPlayerManager alloc] init];
  38. /// player的tag值必须在cell里设置
  39. self.player = [ZFPlayerController playerWithScrollView:self.tableView playerManager:playerManager containerViewTag:kPlayerViewTag];
  40. self.player.controlView = self.controlView;
  41. /// 移动网络依然自动播放
  42. self.player.WWANAutoPlay = YES;
  43. /// 1.0是完全消失的时候
  44. self.player.playerDisapperaPercent = 1.0;
  45. /// 0.0是刚开始显示的时候
  46. self.player.playerApperaPercent = 0.0;
  47. @zf_weakify(self)
  48. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  49. @zf_strongify(self)
  50. [self.controlView resetControlView];
  51. [self.player stopCurrentPlayingCell];
  52. };
  53. /// 停止的时候找出最合适的播放
  54. self.player.zf_scrollViewDidEndScrollingCallback = ^(NSIndexPath * _Nonnull indexPath) {
  55. @zf_strongify(self)
  56. if (!self.player.playingIndexPath) {
  57. [self playTheVideoAtIndexPath:indexPath];
  58. }
  59. };
  60. /// 以下设置滑出屏幕后不停止播放
  61. self.player.stopWhileNotVisible = NO;
  62. CGFloat margin = 20;
  63. CGFloat w = ZFPlayer_ScreenWidth/2;
  64. CGFloat h = w * 9/16;
  65. CGFloat x = ZFPlayer_ScreenWidth - w - margin;
  66. CGFloat y = ZFPlayer_ScreenHeight - h - margin;
  67. self.player.smallFloatView.frame = CGRectMake(x, y, w, h);
  68. }
  69. - (void)viewWillAppear:(BOOL)animated {
  70. [super viewWillAppear:animated];
  71. self.player.viewControllerDisappear = NO;
  72. }
  73. - (void)viewWillDisappear:(BOOL)animated {
  74. [super viewWillDisappear:animated];
  75. self.player.viewControllerDisappear = YES;
  76. }
  77. - (void)viewWillLayoutSubviews {
  78. [super viewWillLayoutSubviews];
  79. CGFloat y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
  80. CGFloat h = CGRectGetMaxY(self.view.frame)-y;
  81. self.tableView.frame = CGRectMake(0, y, self.view.frame.size.width, h);
  82. self.activity.center = self.view.center;
  83. }
  84. - (void)willMoveToParentViewController:(UIViewController *)parent {
  85. if (!parent) {
  86. self.tableView.delegate = nil;
  87. [self.player stopCurrentPlayingCell];
  88. }
  89. }
  90. - (void)requestData {
  91. [self.activity startAnimating];
  92. @zf_weakify(self)
  93. /// 模拟网络请求
  94. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  95. [self.activity stopAnimating];
  96. NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
  97. NSData *data = [NSData dataWithContentsOfFile:path];
  98. NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  99. self.dataSource = @[].mutableCopy;
  100. NSArray *videoList = [rootDict objectForKey:@"list"];
  101. for (NSDictionary *dataDic in videoList) {
  102. ZFTableData *data = [[ZFTableData alloc] init];
  103. [data setValuesForKeysWithDictionary:dataDic];
  104. ZFTableViewCellLayout *layout = [[ZFTableViewCellLayout alloc] initWithData:data];
  105. [self.dataSource addObject:layout];
  106. }
  107. [self.tableView reloadData];
  108. /// 找到可播放的cell
  109. [self.player zf_filterShouldPlayCellWhileScrolled:^(NSIndexPath *indexPath) {
  110. @zf_strongify(self)
  111. [self playTheVideoAtIndexPath:indexPath];
  112. }];
  113. });
  114. }
  115. - (void)pushNewVC {
  116. ZFAutoPlayerViewController *autoVC = [[ZFAutoPlayerViewController alloc] init];
  117. [self.navigationController pushViewController:autoVC animated:YES];
  118. }
  119. - (BOOL)shouldAutorotate {
  120. return NO;
  121. }
  122. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  123. return UIInterfaceOrientationMaskPortrait;
  124. }
  125. - (UIStatusBarStyle)preferredStatusBarStyle {
  126. return UIStatusBarStyleDefault;
  127. }
  128. - (BOOL)prefersStatusBarHidden {
  129. return NO;
  130. }
  131. #pragma mark - UIScrollViewDelegate 列表播放必须实现
  132. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  133. [scrollView zf_scrollViewDidEndDecelerating];
  134. }
  135. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  136. [scrollView zf_scrollViewDidEndDraggingWillDecelerate:decelerate];
  137. }
  138. - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
  139. [scrollView zf_scrollViewDidScrollToTop];
  140. }
  141. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  142. [scrollView zf_scrollViewDidScroll];
  143. }
  144. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  145. [scrollView zf_scrollViewWillBeginDragging];
  146. }
  147. #pragma mark - UITableViewDataSource
  148. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  149. return self.dataSource.count;
  150. }
  151. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  152. ZFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier];
  153. [cell setDelegate:self withIndexPath:indexPath];
  154. cell.layout = self.dataSource[indexPath.row];
  155. [cell setNormalMode];
  156. return cell;
  157. }
  158. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  159. /// 如果正在播放的index和当前点击的index不同,则停止当前播放的index
  160. if (self.player.playingIndexPath != indexPath) {
  161. [self.player stopCurrentPlayingCell];
  162. }
  163. /// 如果没有播放,则点击进详情页会自动播放
  164. if (!self.player.currentPlayerManager.isPlaying) {
  165. [self playTheVideoAtIndexPath:indexPath];
  166. }
  167. /// 到详情页
  168. ZFPlayerDetailViewController *detailVC = [ZFPlayerDetailViewController new];
  169. detailVC.player = self.player;
  170. /// 详情页返回的回调
  171. detailVC.detailVCPopCallback = ^{
  172. [self.player addPlayerViewToCell];
  173. };
  174. /// 详情页点击播放的回调
  175. detailVC.detailVCPlayCallback = ^{
  176. [self zf_playTheVideoAtIndexPath:indexPath];
  177. };
  178. [self.navigationController pushViewController:detailVC animated:YES];
  179. }
  180. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  181. ZFTableViewCellLayout *layout = self.dataSource[indexPath.row];
  182. return layout.height;
  183. }
  184. #pragma mark - ZFTableViewCellDelegate
  185. - (void)zf_playTheVideoAtIndexPath:(NSIndexPath *)indexPath {
  186. [self playTheVideoAtIndexPath:indexPath];
  187. }
  188. #pragma mark - private method
  189. /// play the video
  190. - (void)playTheVideoAtIndexPath:(NSIndexPath *)indexPath {
  191. ZFTableViewCellLayout *layout = self.dataSource[indexPath.row];
  192. [self.player playTheIndexPath:indexPath assetURL:[NSURL URLWithString:layout.data.video_url]];
  193. [self.controlView showTitle:layout.data.title
  194. coverURLString:layout.data.thumbnail_url
  195. fullScreenMode:layout.isVerticalVideo?ZFFullScreenModePortrait:ZFFullScreenModeLandscape];
  196. }
  197. #pragma mark - getter
  198. - (UITableView *)tableView {
  199. if (!_tableView) {
  200. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  201. [_tableView registerClass:[ZFTableViewCell class] forCellReuseIdentifier:kIdentifier];
  202. _tableView.delegate = self;
  203. _tableView.dataSource = self;
  204. _tableView.tableFooterView = [UIView new];
  205. if (@available(iOS 11.0, *)) {
  206. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  207. } else {
  208. self.automaticallyAdjustsScrollViewInsets = NO;
  209. }
  210. _tableView.estimatedRowHeight = 0;
  211. _tableView.estimatedSectionFooterHeight = 0;
  212. _tableView.estimatedSectionHeaderHeight = 0;
  213. }
  214. return _tableView;
  215. }
  216. - (ZFPlayerControlView *)controlView {
  217. if (!_controlView) {
  218. _controlView = [ZFPlayerControlView new];
  219. }
  220. return _controlView;
  221. }
  222. - (UIActivityIndicatorView *)activity {
  223. if (!_activity) {
  224. _activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  225. _activity.hidesWhenStopped = YES;
  226. }
  227. return _activity;
  228. }
  229. @end