VideoPlayViewController.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // VideoPlayViewController.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2018/10/18.
  6. // Copyright © 2018 JCZ. All rights reserved.
  7. //
  8. #import "VideoPlayViewController.h"
  9. #import "VideoDownLoadListVC.h"
  10. static NSString *kVideoCover = @"https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";
  11. @interface VideoPlayViewController () <UITableViewDataSource, UITableViewDelegate>
  12. @end
  13. @implementation VideoPlayViewController
  14. - (void)viewWillAppear:(BOOL)animated {
  15. [super viewWillAppear:animated];
  16. self.player.viewControllerDisappear = NO;
  17. }
  18. - (void)viewWillDisappear:(BOOL)animated {
  19. [super viewWillDisappear:animated];
  20. self.player.viewControllerDisappear = YES;
  21. }
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. [self initUI];
  25. }
  26. #pragma mark - Private Method
  27. - (void)initUI {
  28. [self initBase];
  29. [self initPlayView];
  30. [self initTableView];
  31. }
  32. - (void)initBase {
  33. self.title = self.currentModel.title;
  34. self.view.backgroundColor = backGroundColor;
  35. [self configNavigationBarFirstVC];
  36. }
  37. - (void)initTableView {
  38. if (@available(iOS 11.0, *)) {
  39. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  40. }else {
  41. self.automaticallyAdjustsScrollViewInsets = NO;
  42. _tableView.sectionHeaderHeight = 30;
  43. }
  44. _tableView.rowHeight = UITableViewAutomaticDimension;
  45. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  46. UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, CGFLOAT_MIN)];
  47. self.tableView.tableHeaderView = testView;
  48. }
  49. - (void)initPlayView {
  50. __weak typeof(self) weakSelf = self;
  51. ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
  52. /// 播放器相关
  53. [self.containerView sd_setImageWithURL:[NSURL URLWithString:self.currentModel.imageUrl] placeholderImage:[UIImage imageNamed:@""]];
  54. self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
  55. self.player.controlView = self.controlView;
  56. /// 设置退到后台继续播放
  57. self.player.pauseWhenAppResignActive = NO;
  58. self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
  59. myDelegate.allowOrentitaionRotation = isFullScreen;
  60. };
  61. /// 播放完成
  62. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  63. [weakSelf.player.currentPlayerManager replay];
  64. };
  65. self.player.playerPrepareToPlay = ^(id<ZFPlayerMediaPlayback> _Nonnull asset, NSURL * _Nonnull assetURL) {
  66. NSLog(@"======开始播放了");
  67. };
  68. // NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"videoFinish%@%@",_dataDic[@"id"],[self getVideoFormat]]];
  69. // self.assetURLs = @[[NSURL URLWithString:@"https://www.apple.com/105/media/us/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-tpl-cc-us-20170912_1280x720h.mp4"]];
  70. NSURL *pathURL;
  71. if (self.currentModel.status == RQDownloadStatus_Completed) {
  72. pathURL = [NSURL fileURLWithPath:self.currentModel.destinationPath];
  73. }else {
  74. RQDownloadModel *model = [kRQDownloadDataBaseManager getModelWithUrl:self.currentModel.urlString];
  75. if (model && model.status == RQDownloadStatus_Completed) {
  76. pathURL = [NSURL fileURLWithPath:model.destinationPath];
  77. }else {
  78. pathURL = [NSURL URLWithString:self.currentModel.urlString];
  79. }
  80. }
  81. self.assetURLs = @[pathURL];
  82. self.player.assetURLs = self.assetURLs;
  83. }
  84. - (NSString *)getVideoFormat {
  85. NSString *format = [NSString stringWithFormat:@".%@",[[self.currentModel.urlString componentsSeparatedByString:@"."] lastObject]];
  86. return format;
  87. }
  88. - (IBAction)playClick:(id)sender {
  89. [self.player playTheIndex:0];
  90. self.playBtn.hidden = YES;
  91. [self.controlView showTitle:self.currentModel.title coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeLandscape];
  92. }
  93. #pragma mark - System Method
  94. - (void)viewWillLayoutSubviews {
  95. [super viewWillLayoutSubviews];
  96. }
  97. - (UIStatusBarStyle)preferredStatusBarStyle {
  98. if (self.player.isFullScreen) {
  99. return UIStatusBarStyleLightContent;
  100. }
  101. return UIStatusBarStyleDefault;
  102. }
  103. - (BOOL)prefersStatusBarHidden {
  104. return NO;
  105. }
  106. - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
  107. return UIStatusBarAnimationSlide;
  108. }
  109. - (BOOL)shouldAutorotate {
  110. return NO;
  111. }
  112. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  113. if (self.player.isFullScreen) {
  114. return UIInterfaceOrientationMaskLandscape;
  115. }
  116. return UIInterfaceOrientationMaskPortrait;
  117. }
  118. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  119. [self.view endEditing:YES];
  120. }
  121. #pragma mark - UITableViewDataSource And UITableViewDelegate
  122. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  123. return 1;
  124. }
  125. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  126. UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
  127. if (!headerView) {
  128. headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"header"];
  129. headerView.contentView.backgroundColor = [UIColor whiteColor];
  130. headerView.textLabel.font = [UIFont scaleSize:FontTitle];
  131. headerView.textLabel.textColor = kTitleColor;
  132. }
  133. headerView.textLabel.text = @"内容详情";
  134. return headerView;
  135. }
  136. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  137. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  138. if (!cell) {
  139. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  140. cell.textLabel.numberOfLines = 0;
  141. cell.textLabel.font = [UIFont scaleSize:Font17];
  142. cell.textLabel.textColor = contentTextColor;
  143. }
  144. cell.textLabel.text = self.currentModel.content? self.currentModel.content : @"";
  145. return cell;
  146. }
  147. #pragma mark - Lazy Loading
  148. - (ZFPlayerControlView *)controlView {
  149. if (!_controlView) {
  150. _controlView = [ZFPlayerControlView new];
  151. _controlView.fastViewAnimated = YES;
  152. }
  153. return _controlView;
  154. }
  155. - (RQDownloadModel *)currentModel {
  156. if (!_currentModel) {
  157. _currentModel = [[RQDownloadModel alloc] init];
  158. _currentModel.title = @"";
  159. _currentModel.urlString = @"";
  160. _currentModel.imageUrl = @"";
  161. _currentModel.content = @"";
  162. }
  163. return _currentModel;
  164. }
  165. @end