HomeSchollVideoVC.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // HomeSchollVideoVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/5/18.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "HomeSchollVideoVC.h"
  9. #import <ZFPlayer/ZFAVPlayerManager.h>
  10. #import <ZFPlayer/ZFPlayerControlView.h>
  11. @interface HomeSchollVideoVC ()
  12. @property (nonatomic, strong) ZFPlayerController *player;
  13. @property (nonatomic, strong) UIImageView *containerView;
  14. @property (nonatomic, strong) ZFPlayerControlView *controlView;
  15. @property (nonatomic, strong) UIButton *playBtn;
  16. @property (nonatomic, assign) BOOL isPlay;
  17. @property (nonatomic, strong) NSMutableArray <NSURL *>*assetURLs;
  18. @end
  19. @implementation HomeSchollVideoVC
  20. +(HomeSchollVideoVC *)initHomeSchollVideoVC{
  21. HomeSchollVideoVC *controller = [StoryboardManager.shared.home instantiateViewControllerWithIdentifier:@"HomeSchollVideoVC"];
  22. return controller;
  23. }
  24. - (void)viewWillAppear:(BOOL)animated {
  25. [super viewWillAppear:animated];
  26. [self.assetURLs removeAllObjects];
  27. self.player.viewControllerDisappear = NO;
  28. }
  29. - (void)viewWillDisappear:(BOOL)animated {
  30. [super viewWillDisappear:animated];
  31. [self.assetURLs removeAllObjects];
  32. [self.player stop];
  33. self.player.viewControllerDisappear = YES;
  34. }
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. self.fd_prefersNavigationBarHidden = YES;
  38. [self setupPlayer];
  39. }
  40. - (void)setupPlayer
  41. {
  42. [self.view addSubview:self.containerView];
  43. [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.center.mas_equalTo(self.view);
  45. make.size.mas_offset(CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 16 * 9));
  46. }];
  47. ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
  48. playerManager.shouldAutoPlay = YES;
  49. [self.containerView sd_setImageWithURL:[NSURL URLWithString:self.VideoImage]];
  50. /// 播放器相关
  51. self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
  52. self.player.controlView = self.controlView;
  53. [self.containerView addSubview:self.playBtn];
  54. [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  55. make.center.mas_equalTo(self.containerView);
  56. }];
  57. /// 设置退到后台继续播放
  58. self.player.pauseWhenAppResignActive = NO;
  59. // self.player.resumePlayRecord = YES;
  60. // self.player.lockedScreen = YES;
  61. // self.player.allowOrentitaionRotation = NO;
  62. // [self.player enterFullScreen:YES animated:NO];
  63. WS(weakSelf);
  64. self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
  65. ((AppDelegate *)[[UIApplication sharedApplication] delegate]).allowOrentitaionRotation = isFullScreen;
  66. };
  67. /// 播放完成
  68. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  69. if (!weakSelf.player.isLastAssetURL) {
  70. [weakSelf.player playTheNext];
  71. NSString *title = [NSString stringWithFormat:@"视频标题%zd",weakSelf.player.currentPlayIndex];
  72. [weakSelf.controlView showTitle:title coverURLString:weakSelf.file fullScreenMode:ZFFullScreenModeLandscape];
  73. } else {
  74. [weakSelf.player stop];
  75. }
  76. };
  77. [self.assetURLs addObject:[NSURL URLWithString:weakSelf.file]];
  78. self.player.assetURLs = self.assetURLs;
  79. [self.player playTheIndex:0];
  80. [self.controlView showTitle:@"" coverURLString:weakSelf.file fullScreenMode:ZFFullScreenModeAutomatic];
  81. [self.playBtn setAction:^{
  82. weakSelf.isPlay = !weakSelf.isPlay;
  83. if (weakSelf.isPlay) {
  84. weakSelf.player.currentPlayIndex = 0;
  85. [weakSelf.player playTheNext];
  86. [weakSelf.playBtn setImage:IMG(@"new_allPause_44x44_") forState:UIControlStateNormal];
  87. }else{
  88. [weakSelf.player stop];
  89. [weakSelf.playBtn setImage:IMG(@"new_allPlay_44x44_") forState:UIControlStateNormal];
  90. }
  91. }];
  92. // self.controlView.backBtnClickCallback = ^{
  93. // [weakSelf.navigationController popViewControllerAnimated:YES];
  94. // };
  95. }
  96. #pragma mark - ZFPlayer
  97. - (UIStatusBarStyle)preferredStatusBarStyle {
  98. return UIStatusBarStyleDefault;
  99. }
  100. - (BOOL)prefersStatusBarHidden {
  101. return NO;
  102. }
  103. - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
  104. return UIStatusBarAnimationNone;
  105. }
  106. - (BOOL)shouldAutorotate {
  107. return NO;
  108. }
  109. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  110. return UIInterfaceOrientationMaskLandscapeLeft;
  111. }
  112. - (ZFPlayerControlView *)controlView {
  113. if (!_controlView) {
  114. _controlView = [ZFPlayerControlView new];
  115. _controlView.fastViewAnimated = YES;
  116. _controlView.autoHiddenTimeInterval = 5;
  117. _controlView.autoFadeTimeInterval = 0.5;
  118. _controlView.prepareShowLoading = YES;
  119. _controlView.prepareShowControlView = NO;
  120. _controlView.fullScreenMode = ZFFullScreenModeLandscape;
  121. }
  122. return _controlView;
  123. }
  124. - (UIImageView *)containerView {
  125. if (!_containerView) {
  126. CGFloat h = SCREEN_WIDTH*9/16;
  127. _containerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, h)];
  128. }
  129. return _containerView;
  130. }
  131. - (UIButton *)playBtn {
  132. if (!_playBtn) {
  133. _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  134. [_playBtn setImage:IMG(@"new_allPause_44x44_") forState:UIControlStateNormal];
  135. }
  136. return _playBtn;
  137. }
  138. - (NSMutableArray<NSURL *> *)assetURLs {
  139. if (!_assetURLs) {
  140. _assetURLs = [NSMutableArray array];
  141. }
  142. return _assetURLs;
  143. }
  144. @end