ZFWChatViewController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // ZFWChatViewController.m
  3. // ZFPlayer_Example
  4. //
  5. // Created by 紫枫 on 2020/7/11.
  6. // Copyright © 2020 紫枫. All rights reserved.
  7. //
  8. #import "ZFWChatViewController.h"
  9. #import <ZFPlayer/ZFPlayer.h>
  10. #import <ZFPlayer/ZFAVPlayerManager.h>
  11. #import <ZFPlayer/ZFIJKPlayerManager.h>
  12. #import <ZFPlayer/ZFPlayerControlView.h>
  13. #import <ZFPlayer/UIView+ZFFrame.h>
  14. #import "ZFTableViewCell.h"
  15. #import "ZFTableData.h"
  16. #import <AVFoundation/AVFoundation.h>
  17. #import "ZFWeChatControlView.h"
  18. static NSString *kIdentifier = @"kIdentifier";
  19. @interface ZFWChatViewController () <UITableViewDelegate,UITableViewDataSource,ZFTableViewCellDelegate>
  20. @property (nonatomic, strong) UITableView *tableView;
  21. @property (nonatomic, strong) ZFPlayerController *player;
  22. @property (nonatomic, strong) ZFWeChatControlView *controlView;
  23. @property (nonatomic, strong) NSMutableArray *dataSource;
  24. @end
  25. @implementation ZFWChatViewController
  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. playerManager.scalingMode = ZFPlayerScalingModeAspectFill;
  34. /// player的tag值必须在cell里设置
  35. self.player = [ZFPlayerController playerWithScrollView:self.tableView playerManager:playerManager containerViewTag:kPlayerViewTag];
  36. self.player.controlView = self.controlView;
  37. /// 0.4是消失40%时候
  38. self.player.playerDisapperaPercent = 0.4;
  39. /// 0.6是出现60%时候
  40. self.player.playerApperaPercent = 0.6;
  41. /// 移动网络依然自动播放
  42. self.player.WWANAutoPlay = YES;
  43. /// 续播
  44. self.player.resumePlayRecord = YES;
  45. /// 禁止掉滑动手势
  46. self.player.disableGestureTypes = ZFPlayerDisableGestureTypesPan;
  47. /// 竖屏的全屏
  48. self.player.orientationObserver.fullScreenMode = ZFFullScreenModePortrait;
  49. /// 隐藏全屏的状态栏
  50. self.player.orientationObserver.fullScreenStatusBarHidden = YES;
  51. self.player.orientationObserver.fullScreenStatusBarAnimation = UIStatusBarAnimationNone;
  52. /// 全屏的填充模式(全屏填充、按视频大小填充)
  53. self.player.orientationObserver.portraitFullScreenMode = ZFPortraitFullScreenModeScaleAspectFit;
  54. /// 禁用竖屏全屏的手势(点击、拖动手势)
  55. self.player.orientationObserver.disablePortraitGestureTypes = ZFDisablePortraitGestureTypesNone;
  56. @zf_weakify(self)
  57. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  58. @zf_strongify(self)
  59. [self.player.currentPlayerManager replay];
  60. };
  61. /// 停止的时候找出最合适的播放
  62. self.player.zf_scrollViewDidEndScrollingCallback = ^(NSIndexPath * _Nonnull indexPath) {
  63. @zf_strongify(self)
  64. if (!self.player.playingIndexPath) {
  65. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  66. }
  67. };
  68. /// 滑动中找到适合的就自动播放
  69. /// 如果是停止后再寻找播放可以忽略这个回调
  70. /// 如果在滑动中就要寻找到播放的indexPath,并且开始播放,那就要这样写
  71. self.player.zf_playerShouldPlayInScrollView = ^(NSIndexPath * _Nonnull indexPath) {
  72. @zf_strongify(self)
  73. if ([indexPath compare:self.player.playingIndexPath] != NSOrderedSame) {
  74. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  75. }
  76. };
  77. }
  78. - (void)viewWillLayoutSubviews {
  79. [super viewWillLayoutSubviews];
  80. CGFloat y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
  81. CGFloat h = CGRectGetMaxY(self.view.frame);
  82. self.tableView.frame = CGRectMake(0, y, self.view.frame.size.width, h-y);
  83. }
  84. - (void)viewDidAppear:(BOOL)animated {
  85. [super viewDidAppear:animated];
  86. @zf_weakify(self)
  87. [self.player zf_filterShouldPlayCellWhileScrolled:^(NSIndexPath *indexPath) {
  88. @zf_strongify(self)
  89. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  90. }];
  91. }
  92. - (void)requestData {
  93. NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
  94. NSData *data = [NSData dataWithContentsOfFile:path];
  95. NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  96. self.dataSource = @[].mutableCopy;
  97. NSArray *videoList = [rootDict objectForKey:@"list"];
  98. for (NSDictionary *dataDic in videoList) {
  99. ZFTableData *data = [[ZFTableData alloc] init];
  100. [data setValuesForKeysWithDictionary:dataDic];
  101. ZFTableViewCellLayout *layout = [[ZFTableViewCellLayout alloc] initWXData:data];
  102. [self.dataSource addObject:layout];
  103. }
  104. }
  105. - (BOOL)shouldAutorotate {
  106. return NO;
  107. }
  108. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  109. return UIInterfaceOrientationMaskPortrait;
  110. }
  111. - (UIStatusBarStyle)preferredStatusBarStyle {
  112. return UIStatusBarStyleDefault;
  113. }
  114. - (BOOL)prefersStatusBarHidden {
  115. return NO;
  116. }
  117. #pragma mark - UIScrollViewDelegate 列表播放必须实现
  118. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  119. [scrollView zf_scrollViewDidEndDecelerating];
  120. }
  121. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  122. [scrollView zf_scrollViewDidEndDraggingWillDecelerate:decelerate];
  123. }
  124. - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
  125. [scrollView zf_scrollViewDidScrollToTop];
  126. }
  127. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  128. [scrollView zf_scrollViewDidScroll];
  129. }
  130. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  131. [scrollView zf_scrollViewWillBeginDragging];
  132. }
  133. #pragma mark - UITableViewDataSource
  134. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  135. return self.dataSource.count;
  136. }
  137. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  138. ZFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier];
  139. cell.coverImageView.contentMode = UIViewContentModeScaleAspectFill;
  140. [cell setDelegate:self withIndexPath:indexPath];
  141. cell.layout = self.dataSource[indexPath.row];
  142. [cell setNormalMode];
  143. return cell;
  144. }
  145. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  146. ZFTableViewCellLayout *layout = self.dataSource[indexPath.row];
  147. return layout.height;
  148. }
  149. #pragma mark - ZFTableViewCellDelegate
  150. - (void)zf_playTheVideoAtIndexPath:(NSIndexPath *)indexPath {
  151. [self playTheVideoAtIndexPath:indexPath scrollAnimated:NO];
  152. [self.player enterPortraitFullScreen:YES animated:YES];
  153. }
  154. #pragma mark - private method
  155. /// play the video
  156. - (void)playTheVideoAtIndexPath:(NSIndexPath *)indexPath scrollAnimated:(BOOL)animated {
  157. ZFTableViewCellLayout *layout = self.dataSource[indexPath.row];
  158. if (animated) {
  159. [self.player playTheIndexPath:indexPath assetURL:[NSURL URLWithString:layout.data.video_url] scrollPosition:ZFPlayerScrollViewScrollPositionCenteredVertically animated:YES];
  160. } else {
  161. [self.player playTheIndexPath:indexPath assetURL:[NSURL URLWithString:layout.data.video_url]];
  162. }
  163. [self.controlView showCoverViewWithUrl:layout.data.thumbnail_url];
  164. CGSize videoSize = CGSizeMake(layout.data.video_width, layout.data.video_height);
  165. self.player.currentPlayerManager.presentationSize = videoSize;
  166. }
  167. #pragma mark - getter
  168. - (UITableView *)tableView {
  169. if (!_tableView) {
  170. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  171. [_tableView registerClass:[ZFTableViewCell class] forCellReuseIdentifier:kIdentifier];
  172. _tableView.delegate = self;
  173. _tableView.dataSource = self;
  174. if (@available(iOS 11.0, *)) {
  175. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  176. } else {
  177. self.automaticallyAdjustsScrollViewInsets = NO;
  178. }
  179. _tableView.estimatedRowHeight = 0;
  180. _tableView.estimatedSectionFooterHeight = 0;
  181. _tableView.estimatedSectionHeaderHeight = 0;
  182. }
  183. return _tableView;
  184. }
  185. - (ZFWeChatControlView *)controlView {
  186. if (!_controlView) {
  187. _controlView = [ZFWeChatControlView new];
  188. }
  189. return _controlView;
  190. }
  191. @end