RQDspVideoDetailViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // RQDspVideoDetailViewController.m
  3. // JSJP
  4. //
  5. // Created by RONGQING on 2022/1/19.
  6. //
  7. #import "RQDspVideoDetailViewController.h"
  8. #import <ZFPlayer/ZFAVPlayerManager.h>
  9. #import <ZFPlayer/ZFIJKPlayerManager.h>
  10. #import <ZFPlayer/ZFPlayerControlView.h>
  11. #import <ZFPlayer/ZFPlayerConst.h>
  12. #import "MPTransition.h"
  13. /// 播放器view的tag,列表中UI控件唯一tag值
  14. #define kPlayerViewTag 100
  15. static NSString *kIdentifier = @"kIdentifier";
  16. @interface RQDspVideoDetailViewController ()
  17. /// viewModel
  18. @property (nonatomic, readonly, strong) RQDspVideoDetailViewModel *viewModel;
  19. @property (nonatomic, readwrite, strong) UIButton *backBtn;
  20. @property (nonatomic, readwrite, strong) RQDspVideoDetailControlView *controlView;
  21. @property (nonatomic, readwrite, strong) NSIndexPath *playingIndexPath;
  22. @property (nonatomic, readwrite, strong) ZFPlayerControlView *preControlView;
  23. @property (nonatomic, readwrite, assign) BOOL isInited;
  24. @property (nonatomic, readwrite, assign) BOOL isPassPlayer;
  25. @property (nonatomic, readwrite, strong) MPPlayerController *player;
  26. @property (nonatomic, readwrite, assign) NSInteger index;
  27. @property (nonatomic, strong) UIView *startView;
  28. @property (nonatomic, strong) UIImage *startImage;
  29. @property (nonatomic, copy) void(^popbackBlock)(void);
  30. //@property (nonatomic, readwrite, strong) ADSuyiSDKDrawvodAd *drawvodAd;
  31. //@property (nonatomic, strong) NSMutableArray<ADSuyiAdapterDrawvodAdView *> *data;
  32. //@property (nonatomic, readwrite, strong) VideosItem *lastVideoItem;
  33. @end
  34. @implementation RQDspVideoDetailViewController
  35. @dynamic viewModel;
  36. #pragma mark - SystemMethod
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. /// 初始化
  40. [self rq_setup];
  41. // [self loadDrawvodAd];
  42. }
  43. - (void)viewWillLayoutSubviews {
  44. [super viewWillLayoutSubviews];
  45. self.backBtn.frame = CGRectMake(15, CGRectGetMaxY([UIApplication sharedApplication].statusBarFrame), 36, 36);
  46. }
  47. - (void)viewDidAppear:(BOOL)animated {
  48. [super viewDidAppear:animated];
  49. [self check];
  50. }
  51. - (void)check {
  52. if (!self.isInited) {
  53. self.isInited = YES;
  54. if (self.isPassPlayer) {
  55. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.playingIndexPath];
  56. UIView *playerView = [cell viewWithTag:10086];
  57. self.player.controlView = self.controlView;
  58. if (playerView) {
  59. [self.player updateNoramlPlayerWithContainerView:playerView];
  60. [self.controlView resetControlView];
  61. RQCommonGroupViewModel *group = self.viewModel.dataSource[self.playingIndexPath.section];
  62. RQDspVideoDetailItemViewModel *itemViewModel = group.itemViewModels.firstObject;
  63. VideosItem *videosItem = itemViewModel.videosItem;
  64. UIViewContentMode imageMode;
  65. if (videosItem.videoWidth >= videosItem.videoHeight) {
  66. imageMode = UIViewContentModeScaleAspectFit;
  67. } else {
  68. imageMode = UIViewContentModeScaleAspectFill;
  69. }
  70. [self.controlView showCoverViewWithUrl:videosItem.coverFileUrl withImageMode:imageMode];
  71. }
  72. } else {
  73. self.player.controlView = self.controlView;
  74. [self playTheVideoAtIndexPath:self.playingIndexPath scrollToTop:NO];
  75. }
  76. }
  77. self.navigationController.delegate = self;
  78. }
  79. //- (void)dealloc {
  80. // for (ADSuyiAdapterDrawvodAdView *adview in self.data) {
  81. // // 5、广告内存回收
  82. // [adview unRegist];
  83. // }
  84. //}
  85. #pragma mark - PrivateMethods
  86. /// 初始化
  87. - (void)rq_setup {
  88. @weakify(self)
  89. self.tableView.pagingEnabled = YES;
  90. self.tableView.showsVerticalScrollIndicator = NO;
  91. self.tableView.scrollsToTop = NO;
  92. self.tableView.contentInset = UIEdgeInsetsZero;
  93. if (@available(iOS 11.0, *)) {
  94. self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  95. } else {
  96. self.automaticallyAdjustsScrollViewInsets = NO;
  97. }
  98. self.index = self.viewModel.indexPath.section;
  99. [self.view addSubview:self.backBtn];
  100. if (!self.player) {
  101. self.player = [MPPlayerController playrWithContainerView:[UIView new]];
  102. self.player.playableArray = [self.viewModel.dataSource.rac_sequence map:^id _Nullable(RQCommonGroupViewModel *group) {
  103. RQDspVideoDetailItemViewModel *itemViewModel = group.itemViewModels.firstObject;
  104. return itemViewModel.videosItem;
  105. }].array;
  106. }else {
  107. for(UIView *subView in self.player.currentPlayerManager.view.subviews) {
  108. [subView removeFromSuperview];
  109. }
  110. self.isPassPlayer = YES;
  111. }
  112. self.player.presentationSizeChanged = ^(id<ZFPlayerMediaPlayback> _Nonnull asset, CGSize size) {
  113. @strongify(self)
  114. if (size.width >= size.height) {
  115. self.player.currentPlayerManager.scalingMode = ZFPlayerScalingModeAspectFit;
  116. } else {
  117. self.player.currentPlayerManager.scalingMode = ZFPlayerScalingModeAspectFill;
  118. }
  119. };
  120. // [self.tableView reloadData];
  121. self.playingIndexPath = [NSIndexPath indexPathForRow:0 inSection:self.index];
  122. [self.tableView scrollToRowAtIndexPath:self.playingIndexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
  123. }
  124. //- (void)loadDrawvodAd {
  125. // // 2、加载沉浸式视频广告
  126. // [self.drawvodAd load:1];
  127. //}
  128. - (void)backClick:(UIButton *)sender {
  129. [self.navigationController popViewControllerAnimated:YES];
  130. }
  131. /// play the video
  132. - (void)playTheVideoAtIndexPath:(NSIndexPath *)indexPath scrollToTop:(BOOL)scrollToTop {
  133. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  134. RQCommonGroupViewModel *group = self.viewModel.dataSource[indexPath.section];
  135. RQDspVideoDetailItemViewModel *itemViewModel = group.itemViewModels.firstObject;
  136. VideosItem *videosItem = itemViewModel.videosItem;
  137. UIView *contanier = [cell viewWithTag:10086];
  138. [self.player updateNoramlPlayerWithContainerView:contanier];
  139. if (RQObjectIsNil(videosItem.adView)) {
  140. [self.player playWithPlayable:videosItem];
  141. }
  142. [self.controlView resetControlView];
  143. UIViewContentMode imageMode;
  144. if (videosItem.videoWidth >= videosItem.videoHeight) {
  145. imageMode = UIViewContentModeScaleAspectFit;
  146. } else {
  147. imageMode = UIViewContentModeScaleAspectFill;
  148. }
  149. [self.controlView showCoverViewWithUrl:videosItem.coverFileUrl withImageMode:imageMode];
  150. self.playingIndexPath = indexPath;
  151. }
  152. - (void)didMoveToParentViewController:(UIViewController *)parent {
  153. if (!parent) {
  154. if (self.isPassPlayer) {
  155. [self.controlView removeFromSuperview];
  156. for (UIView *subView in self.player.currentPlayerManager.view.subviews) {
  157. [subView removeFromSuperview];
  158. }
  159. self.player.controlView = self.preControlView;
  160. if (self.popbackBlock) {
  161. self.popbackBlock();
  162. }
  163. }
  164. }
  165. }
  166. #pragma mark - UIScrollViewDelegate 列表播放必须实现
  167. - (void)cellPlayVideo {
  168. if (self.tableView.visibleCells.count && self.tableView.visibleCells.count == 1) {
  169. UITableViewCell *cell = self.tableView.visibleCells.firstObject;
  170. NSIndexPath *ip = [self.tableView indexPathForCell:cell];
  171. NSComparisonResult result = [ip compare:self.playingIndexPath];
  172. // 判断indexPath是否发生变化
  173. if (ip && result != NSOrderedSame) {
  174. [self.player stop];
  175. [self playTheVideoAtIndexPath:ip scrollToTop:NO];
  176. }
  177. if (ip) {
  178. self.playingIndexPath = ip;
  179. }
  180. }
  181. }
  182. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  183. [self cellPlayVideo];
  184. }
  185. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  186. [self handlePlayerOutofScreen:scrollView];
  187. }
  188. - (void)handlePlayerOutofScreen: (UIScrollView *)scrollView {
  189. // 为了处理快速滑动时,player复用的bug
  190. self.tableView.contentInset = UIEdgeInsetsZero;
  191. UIView *cell = [self.tableView cellForRowAtIndexPath:self.playingIndexPath];
  192. if (!cell && self.playingIndexPath) {
  193. [self.player stop];
  194. return;
  195. }
  196. }
  197. #pragma mark - OverrideMethods
  198. - (UIEdgeInsets)contentInset {
  199. return UIEdgeInsetsZero;
  200. }
  201. - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
  202. RQDspVideoDetailCell *cell = [RQDspVideoDetailCell cellWithTableView:tableView];
  203. // ADSuyiAdapterDrawvodAdView *adView = self.data[indexPath.section];
  204. // cell.adView = adView;
  205. return cell;
  206. }
  207. - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object {
  208. RQDspVideoDetailCell *dspVideoDetailCell = (RQDspVideoDetailCell *)cell;
  209. [dspVideoDetailCell bindViewModel:object];
  210. }
  211. #pragma mark - ADSuyiSDKDrawvodAdDelegate
  212. //- (void)adsy_drawvodAdSuccessToLoad:(ADSuyiSDKDrawvodAd *)drawvodAd drawvodAdArray:(NSArray<ADSuyiAdapterDrawvodAdView *> *)drawvodAdViewArray {
  213. // for (ADSuyiAdapterDrawvodAdView *adView in drawvodAdViewArray) {
  214. // // 3、渲染沉浸式广告
  215. // [adView render];
  216. // }
  217. //}
  218. //
  219. //- (void)adsy_drawvodAdFailToLoad:(ADSuyiSDKDrawvodAd *)drawvodAd errorModel:(ADSuyiAdapterErrorDefine *)errorModel {
  220. //
  221. //}
  222. //
  223. //- (void)adsy_drawvodAdSuccessToRender:(ADSuyiSDKDrawvodAd *)drawvodAd view:(ADSuyiAdapterDrawvodAdView *)drawvodAdView {
  224. // // 4、渲染成功,此时高度正常,资源加载中,可'展示
  225. // NSMutableArray *dataSourceArr = [NSMutableArray arrayWithArray:self.viewModel.dataSource];
  226. // [self.data addObject:drawvodAdView];
  227. // NSInteger i = 8;
  228. // if (self.lastVideoItem) {
  229. // if (self.lastVideoItem.index < dataSourceArr.count) {
  230. // i = self.lastVideoItem.index + arc4random() % (dataSourceArr.count - self.lastVideoItem.index) + 4;
  231. // } else {
  232. // return;
  233. // }
  234. // } else {
  235. // i = (dataSourceArr.count > 6)? 6 + arc4random() % (dataSourceArr.count - 6) : 0;
  236. // }
  237. //
  238. // if (i > dataSourceArr.count) {
  239. // return;
  240. // }
  241. //// VideosItem *videoItem = dataSourceArr[i];
  242. //// videoItem.index = i;
  243. //// videoItem.totalNum = dataSourceArr.count + 1;
  244. // RQCommonGroupViewModel *group = dataSourceArr[i];
  245. // RQDspVideoDetailItemViewModel *dspVideoDetailItemViewModel = group.itemViewModels.firstObject;
  246. // dspVideoDetailItemViewModel.videosItem.adView = drawvodAdView;
  247. // dspVideoDetailItemViewModel.rowHeight = RQ_SCREEN_HEIGHT;
  248. // dspVideoDetailItemViewModel.operation = ^{
  249. // };
  250. // group.itemViewModels = @[dspVideoDetailItemViewModel];
  251. //
  252. // [dataSourceArr replaceObjectAtIndex:i withObject:group];
  253. // self.viewModel.dataSource = dataSourceArr.copy;
  254. // self.lastVideoItem = dspVideoDetailItemViewModel.videosItem;
  255. //}
  256. //
  257. //- (void)adsy_drawvodAdFailToRender:(ADSuyiSDKDrawvodAd *)drawvodAd view:(ADSuyiAdapterDrawvodAdView *)drawvodAdView error:(nullable NSError *)error {
  258. //
  259. //}
  260. //
  261. //- (void)adsy_drawvodAdPlayerDidPlayFinish:(ADSuyiAdapterDrawvodAdView *)drawvodAdView {
  262. //
  263. //}
  264. //
  265. //- (void)adsy_drawvodAdDidExposure:(ADSuyiAdapterDrawvodAdView *)drawvodAdView {
  266. //
  267. //}
  268. //
  269. //- (void)adsy_drawvodAdDidClick:(ADSuyiAdapterDrawvodAdView *)drawvodAdView {
  270. //
  271. //}
  272. #pragma mark - LazyLoad
  273. - (void)setPlayer:(MPPlayerController *)player {
  274. _player = player;
  275. self.preControlView = (ZFPlayerControlView *)player.controlView;
  276. [self.preControlView removeFromSuperview];
  277. }
  278. - (RQDspVideoDetailControlView *)controlView {
  279. if (!_controlView) {
  280. _controlView = [RQDspVideoDetailControlView new];
  281. }
  282. return _controlView;
  283. }
  284. - (UIButton *)backBtn {
  285. if (!_backBtn) {
  286. _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  287. [_backBtn setImage:[UIImage imageNamed:@"icon_titlebar_whiteback"] forState:UIControlStateNormal];
  288. [_backBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
  289. }
  290. return _backBtn;
  291. }
  292. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
  293. if (self.startView == nil)
  294. return nil;
  295. if (self.playingIndexPath &&
  296. self.playingIndexPath.section != self.index)
  297. return nil;
  298. return [MPTransition animationWithDuration:0.3 startView:self.startView startImage:self.startImage player:self.player operation:operation completion:^{
  299. }];
  300. }
  301. //- (ADSuyiSDKDrawvodAd *)drawvodAd {
  302. // if(!_drawvodAd) {
  303. // // 1、初始化沉浸式视频广告,流程和信息流广告类似
  304. // _drawvodAd = [[ADSuyiSDKDrawvodAd alloc] initWithSize:self.tableView.frame.size];
  305. // _drawvodAd.posId = @"0121212a7b45ec720e";
  306. // _drawvodAd.controller = self;
  307. // _drawvodAd.delegate = self;
  308. // }
  309. // return _drawvodAd;
  310. //}
  311. //
  312. //- (NSMutableArray<ADSuyiAdapterDrawvodAdView *> *)data {
  313. // if(!_data) {
  314. // _data = [NSMutableArray new];
  315. // }
  316. // return _data;
  317. //}
  318. @end