// // RQDspVideoDetailViewController.m // SDJK // // 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, readwrite, strong) NSMutableArray *dataSource; @property (nonatomic, strong) UIView *startView; @property (nonatomic, strong) UIImage *startImage; @property (nonatomic, copy) void(^popbackBlock)(void); @end @implementation RQDspVideoDetailViewController @dynamic viewModel; #pragma mark - SystemMethod - (void)viewDidLoad { [super viewDidLoad]; /// 初始化 [self rq_setup]; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; self.backBtn.frame = CGRectMake(15, CGRectGetMaxY([UIApplication sharedApplication].statusBarFrame), 36, 36); } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; 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]; VideosItem *videosItem = self.dataSource[self.playingIndexPath.row]; 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; } #pragma mark - PrivateMethods /// 初始化 - (void)rq_setup { @weakify(self) self.tableView.pagingEnabled = YES; self.tableView.showsVerticalScrollIndicator = NO; self.tableView.scrollsToTop = NO; self.index = self.viewModel.indexPath.row; [self.view addSubview:self.backBtn]; if (!self.player) { self.player = [MPPlayerController playrWithContainerView:[UIView new]]; [self requestData]; self.player.playableArray = self.dataSource; }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:self.index inSection:0]; [self.tableView scrollToRowAtIndexPath:self.playingIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; } - (void)requestData { [self.dataSource addObjectsFromArray:RQ_COMMON_MANAGER.dspListArr]; self.player.playableArray = self.dataSource; } - (void)backClick:(UIButton *)sender { [self.navigationController popViewControllerAnimated:YES]; } /// play the video - (void)playTheVideoAtIndexPath:(NSIndexPath *)indexPath scrollToTop:(BOOL)scrollToTop { UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; UIView *contanier = [cell viewWithTag:10086]; [self.player updateNoramlPlayerWithContainerView:contanier]; [self.player playWithPlayable:self.dataSource[indexPath.row]]; [self.controlView resetControlView]; VideosItem *videosItem = self.dataSource[indexPath.row]; 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 UIView *cell = [self.tableView cellForRowAtIndexPath:self.playingIndexPath]; if (!cell && self.playingIndexPath) { [self.player stop]; return; } } #pragma mark - OverrideMethods - (UIEdgeInsets)contentInset { return UIEdgeInsetsMake(0, 0, 0, 0); } - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case 0: { RQDspVideoDetailCell *cell = [RQDspVideoDetailCell cellWithTableView:tableView]; return cell; } default: return [super tableView:tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; } } - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object { switch (indexPath.section) { case 0: { RQDspVideoDetailCell *dspVideoDetailCell = (RQDspVideoDetailCell *)cell; [dspVideoDetailCell bindViewModel:object]; break; } default: { [super configureCell:cell atIndexPath:indexPath withObject:object]; break; } } } #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; } - (NSMutableArray *)dataSource { if (!_dataSource) { _dataSource = @[].mutableCopy; } return _dataSource; } - (id)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{ if (self.startView == nil) return nil; if (self.playingIndexPath && self.playingIndexPath.row != self.index) return nil; return [MPTransition animationWithDuration:0.3 startView:self.startView startImage:self.startImage player:self.player operation:operation completion:^{ }]; } @end