// // RQDspVideoDetailViewController.m // JSJP // // Created by RONGQING on 2022/1/19. // #import "RQDspVideoDetailViewController.h" #import #import #import #import #import "MPTransition.h" /// 播放器view的tag,列表中UI控件唯一tag值 #define kPlayerViewTag 100 static NSString *kIdentifier = @"kIdentifier"; @interface RQDspVideoDetailViewController () /// viewModel @property (nonatomic, readonly, strong) RQDspVideoDetailViewModel *viewModel; @property (nonatomic, readwrite, strong) UIButton *backBtn; @property (nonatomic, readwrite, strong) RQDspVideoDetailControlView *controlView; @property (nonatomic, readwrite, strong) NSIndexPath *playingIndexPath; @property (nonatomic, readwrite, strong) ZFPlayerControlView *preControlView; @property (nonatomic, readwrite, assign) BOOL isInited; @property (nonatomic, readwrite, assign) BOOL isPassPlayer; @property (nonatomic, readwrite, strong) MPPlayerController *player; @property (nonatomic, readwrite, assign) NSInteger index; @property (nonatomic, strong) UIView *startView; @property (nonatomic, strong) UIImage *startImage; @property (nonatomic, copy) void(^popbackBlock)(void); //@property (nonatomic, readwrite, strong) ADSuyiSDKDrawvodAd *drawvodAd; //@property (nonatomic, strong) NSMutableArray *data; //@property (nonatomic, readwrite, strong) VideosItem *lastVideoItem; @end @implementation RQDspVideoDetailViewController @dynamic viewModel; #pragma mark - SystemMethod - (void)viewDidLoad { [super viewDidLoad]; /// 初始化 [self rq_setup]; // [self loadDrawvodAd]; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; self.backBtn.frame = CGRectMake(15, CGRectGetMaxY([UIApplication sharedApplication].statusBarFrame), 36, 36); } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self check]; } - (void)check { if (!self.isInited) { self.isInited = YES; if (self.isPassPlayer) { UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.playingIndexPath]; UIView *playerView = [cell viewWithTag:10086]; self.player.controlView = self.controlView; if (playerView) { [self.player updateNoramlPlayerWithContainerView:playerView]; [self.controlView resetControlView]; RQCommonGroupViewModel *group = self.viewModel.dataSource[self.playingIndexPath.section]; RQDspVideoDetailItemViewModel *itemViewModel = group.itemViewModels.firstObject; VideosItem *videosItem = itemViewModel.videosItem; UIViewContentMode imageMode; if (videosItem.videoWidth >= videosItem.videoHeight) { imageMode = UIViewContentModeScaleAspectFit; } else { imageMode = UIViewContentModeScaleAspectFill; } [self.controlView showCoverViewWithUrl:videosItem.coverFileUrl withImageMode:imageMode]; } } else { self.player.controlView = self.controlView; [self playTheVideoAtIndexPath:self.playingIndexPath scrollToTop:NO]; } } self.navigationController.delegate = self; } //- (void)dealloc { // for (ADSuyiAdapterDrawvodAdView *adview in self.data) { // // 5、广告内存回收 // [adview unRegist]; // } //} #pragma mark - PrivateMethods /// 初始化 - (void)rq_setup { @weakify(self) self.tableView.pagingEnabled = YES; self.tableView.showsVerticalScrollIndicator = NO; self.tableView.scrollsToTop = NO; self.tableView.contentInset = UIEdgeInsetsZero; if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } self.index = self.viewModel.indexPath.section; [self.view addSubview:self.backBtn]; if (!self.player) { self.player = [MPPlayerController playrWithContainerView:[UIView new]]; self.player.playableArray = [self.viewModel.dataSource.rac_sequence map:^id _Nullable(RQCommonGroupViewModel *group) { RQDspVideoDetailItemViewModel *itemViewModel = group.itemViewModels.firstObject; return itemViewModel.videosItem; }].array; }else { for(UIView *subView in self.player.currentPlayerManager.view.subviews) { [subView removeFromSuperview]; } self.isPassPlayer = YES; } self.player.presentationSizeChanged = ^(id _Nonnull asset, CGSize size) { @strongify(self) if (size.width >= size.height) { self.player.currentPlayerManager.scalingMode = ZFPlayerScalingModeAspectFit; } else { self.player.currentPlayerManager.scalingMode = ZFPlayerScalingModeAspectFill; } }; // [self.tableView reloadData]; self.playingIndexPath = [NSIndexPath indexPathForRow:0 inSection:self.index]; [self.tableView scrollToRowAtIndexPath:self.playingIndexPath atScrollPosition:UITableViewScrollPositionNone animated:NO]; } //- (void)loadDrawvodAd { // // 2、加载沉浸式视频广告 // [self.drawvodAd load:1]; //} - (void)backClick:(UIButton *)sender { [self.navigationController popViewControllerAnimated:YES]; } /// play the video - (void)playTheVideoAtIndexPath:(NSIndexPath *)indexPath scrollToTop:(BOOL)scrollToTop { UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; RQCommonGroupViewModel *group = self.viewModel.dataSource[indexPath.section]; RQDspVideoDetailItemViewModel *itemViewModel = group.itemViewModels.firstObject; VideosItem *videosItem = itemViewModel.videosItem; UIView *contanier = [cell viewWithTag:10086]; [self.player updateNoramlPlayerWithContainerView:contanier]; if (RQObjectIsNil(videosItem.adView)) { [self.player playWithPlayable:videosItem]; } [self.controlView resetControlView]; UIViewContentMode imageMode; if (videosItem.videoWidth >= videosItem.videoHeight) { imageMode = UIViewContentModeScaleAspectFit; } else { imageMode = UIViewContentModeScaleAspectFill; } [self.controlView showCoverViewWithUrl:videosItem.coverFileUrl withImageMode:imageMode]; self.playingIndexPath = indexPath; } - (void)didMoveToParentViewController:(UIViewController *)parent { if (!parent) { if (self.isPassPlayer) { [self.controlView removeFromSuperview]; for (UIView *subView in self.player.currentPlayerManager.view.subviews) { [subView removeFromSuperview]; } self.player.controlView = self.preControlView; if (self.popbackBlock) { self.popbackBlock(); } } } } #pragma mark - UIScrollViewDelegate 列表播放必须实现 - (void)cellPlayVideo { if (self.tableView.visibleCells.count && self.tableView.visibleCells.count == 1) { UITableViewCell *cell = self.tableView.visibleCells.firstObject; NSIndexPath *ip = [self.tableView indexPathForCell:cell]; NSComparisonResult result = [ip compare:self.playingIndexPath]; // 判断indexPath是否发生变化 if (ip && result != NSOrderedSame) { [self.player stop]; [self playTheVideoAtIndexPath:ip scrollToTop:NO]; } if (ip) { self.playingIndexPath = ip; } } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [self cellPlayVideo]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { [self handlePlayerOutofScreen:scrollView]; } - (void)handlePlayerOutofScreen: (UIScrollView *)scrollView { // 为了处理快速滑动时,player复用的bug self.tableView.contentInset = UIEdgeInsetsZero; UIView *cell = [self.tableView cellForRowAtIndexPath:self.playingIndexPath]; if (!cell && self.playingIndexPath) { [self.player stop]; return; } } #pragma mark - OverrideMethods - (UIEdgeInsets)contentInset { return UIEdgeInsetsZero; } - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath { RQDspVideoDetailCell *cell = [RQDspVideoDetailCell cellWithTableView:tableView]; // ADSuyiAdapterDrawvodAdView *adView = self.data[indexPath.section]; // cell.adView = adView; return cell; } - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object { RQDspVideoDetailCell *dspVideoDetailCell = (RQDspVideoDetailCell *)cell; [dspVideoDetailCell bindViewModel:object]; } #pragma mark - ADSuyiSDKDrawvodAdDelegate //- (void)adsy_drawvodAdSuccessToLoad:(ADSuyiSDKDrawvodAd *)drawvodAd drawvodAdArray:(NSArray *)drawvodAdViewArray { // for (ADSuyiAdapterDrawvodAdView *adView in drawvodAdViewArray) { // // 3、渲染沉浸式广告 // [adView render]; // } //} // //- (void)adsy_drawvodAdFailToLoad:(ADSuyiSDKDrawvodAd *)drawvodAd errorModel:(ADSuyiAdapterErrorDefine *)errorModel { // //} // //- (void)adsy_drawvodAdSuccessToRender:(ADSuyiSDKDrawvodAd *)drawvodAd view:(ADSuyiAdapterDrawvodAdView *)drawvodAdView { // // 4、渲染成功,此时高度正常,资源加载中,可'展示 // NSMutableArray *dataSourceArr = [NSMutableArray arrayWithArray:self.viewModel.dataSource]; // [self.data addObject:drawvodAdView]; // NSInteger i = 8; // if (self.lastVideoItem) { // if (self.lastVideoItem.index < dataSourceArr.count) { // i = self.lastVideoItem.index + arc4random() % (dataSourceArr.count - self.lastVideoItem.index) + 4; // } else { // return; // } // } else { // i = (dataSourceArr.count > 6)? 6 + arc4random() % (dataSourceArr.count - 6) : 0; // } // // if (i > dataSourceArr.count) { // return; // } //// VideosItem *videoItem = dataSourceArr[i]; //// videoItem.index = i; //// videoItem.totalNum = dataSourceArr.count + 1; // RQCommonGroupViewModel *group = dataSourceArr[i]; // RQDspVideoDetailItemViewModel *dspVideoDetailItemViewModel = group.itemViewModels.firstObject; // dspVideoDetailItemViewModel.videosItem.adView = drawvodAdView; // dspVideoDetailItemViewModel.rowHeight = RQ_SCREEN_HEIGHT; // dspVideoDetailItemViewModel.operation = ^{ // }; // group.itemViewModels = @[dspVideoDetailItemViewModel]; // // [dataSourceArr replaceObjectAtIndex:i withObject:group]; // self.viewModel.dataSource = dataSourceArr.copy; // self.lastVideoItem = dspVideoDetailItemViewModel.videosItem; //} // //- (void)adsy_drawvodAdFailToRender:(ADSuyiSDKDrawvodAd *)drawvodAd view:(ADSuyiAdapterDrawvodAdView *)drawvodAdView error:(nullable NSError *)error { // //} // //- (void)adsy_drawvodAdPlayerDidPlayFinish:(ADSuyiAdapterDrawvodAdView *)drawvodAdView { // //} // //- (void)adsy_drawvodAdDidExposure:(ADSuyiAdapterDrawvodAdView *)drawvodAdView { // //} // //- (void)adsy_drawvodAdDidClick:(ADSuyiAdapterDrawvodAdView *)drawvodAdView { // //} #pragma mark - LazyLoad - (void)setPlayer:(MPPlayerController *)player { _player = player; self.preControlView = (ZFPlayerControlView *)player.controlView; [self.preControlView removeFromSuperview]; } - (RQDspVideoDetailControlView *)controlView { if (!_controlView) { _controlView = [RQDspVideoDetailControlView new]; } return _controlView; } - (UIButton *)backBtn { if (!_backBtn) { _backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_backBtn setImage:[UIImage imageNamed:@"icon_titlebar_whiteback"] forState:UIControlStateNormal]; [_backBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside]; } return _backBtn; } - (id)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{ if (self.startView == nil) return nil; if (self.playingIndexPath && self.playingIndexPath.section != self.index) return nil; return [MPTransition animationWithDuration:0.3 startView:self.startView startImage:self.startImage player:self.player operation:operation completion:^{ }]; } //- (ADSuyiSDKDrawvodAd *)drawvodAd { // if(!_drawvodAd) { // // 1、初始化沉浸式视频广告,流程和信息流广告类似 // _drawvodAd = [[ADSuyiSDKDrawvodAd alloc] initWithSize:self.tableView.frame.size]; // _drawvodAd.posId = @"0121212a7b45ec720e"; // _drawvodAd.controller = self; // _drawvodAd.delegate = self; // } // return _drawvodAd; //} // //- (NSMutableArray *)data { // if(!_data) { // _data = [NSMutableArray new]; // } // return _data; //} @end