videoPlayByAVPlayerViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // videoPlayByAVPlayerViewController.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/5/22.
  6. //
  7. #import "videoPlayByAVPlayerViewController.h"
  8. //#import <ZFPlayer/ZFAVPlayerManager.h>
  9. //#import <ZFPlayer/ZFIJKPlayerManager.h>
  10. //#import <ZFPlayer/ZFPlayerControlView.h>
  11. //#import <ZFPlayer/ZFPlayerConst.h>
  12. //#import "ZFUtilities.h"
  13. #import "ZFAVPlayerManager.h"
  14. #import "ZFIJKPlayerManager.h"
  15. #import "ZFPlayerControlView.h"
  16. #import "ZFPlayerConst.h"
  17. #import "ZFUtilities.h"
  18. @interface videoPlayByAVPlayerViewController ()
  19. @property (nonatomic, strong) ZFPlayerController *player;
  20. @property (nonatomic, strong) ZFPlayerControlView *controlView;
  21. @property (nonatomic, strong)ZFAVPlayerManager *playerManager;
  22. @property (nonatomic, strong)UIView *bgView;
  23. @property (nonatomic, strong)UIButton *portraitBackBtn;
  24. @property (nonatomic, strong)UIButton *portraitToLandScapeBtn;
  25. @property (nonatomic, strong)UIButton *landScapeToPortraitBtn;
  26. @end
  27. @implementation videoPlayByAVPlayerViewController
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. [self.toolBar setHidden:YES];
  31. [self.navigationBar setHidden:YES];
  32. [self.navBarBGView setHidden:YES];
  33. //self.navBarBGView.backgroundColor = [UIColor blackColor];
  34. self.view.backgroundColor = [UIColor blackColor];
  35. _bgView = [[UIView alloc] init];
  36. [self.view addSubview:_bgView];
  37. [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  38. make.left.mas_equalTo(0);
  39. make.right.mas_equalTo(0);
  40. make.bottom.mas_equalTo(-safeArea);
  41. make.top.mas_equalTo(H_STATE_BAR);
  42. }];
  43. @zf_weakify(self)
  44. self.controlView.backBtnClickCallback = ^{
  45. @zf_strongify(self)
  46. [self didClickBackBtnFun];
  47. // [self.player rotateToOrientation:UIInterfaceOrientationPortrait animated:NO completion:nil];
  48. // [self.player stop];
  49. //[self dismissViewControllerAnimated:NO completion:nil];
  50. };
  51. _playerManager = [[ZFAVPlayerManager alloc] init];
  52. /// 播放器相关
  53. self.player = [[ZFPlayerController alloc] initWithPlayerManager:_playerManager containerView:_bgView];
  54. self.player.controlView = self.controlView;
  55. // self.player.orientationObserver.supportInterfaceOrientation = ZFInterfaceOrientationMaskLandscape;
  56. // [self.player rotateToOrientation:UIInterfaceOrientationLandscapeRight animated:NO completion:nil];
  57. self.playerManager.playerPlayFailed = ^(id<ZFPlayerMediaPlayback> _Nonnull asset, id _Nonnull error) {
  58. mainBlock(^{
  59. @zf_strongify(self)
  60. self.controlView.failBtn.hidden = YES;
  61. [[iToast makeText:NSLocalizedString(@"play_video_fail_tip",nil)] show];
  62. });
  63. };
  64. }
  65. - (void)viewDidAppear:(BOOL)animated
  66. {
  67. [super viewDidAppear:animated];
  68. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  69. if(!self.playerManager.assetURL){
  70. NSString *filePath = _VideoDataMode.path;
  71. NSString *urlStr = ksharedAppDelegate.NASFileByBoxService;
  72. NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFile?path=%@",urlStr,filePath];
  73. // NSString *string = filePath;
  74. // NSString *filePathBase64 = [iTools base64UrlEncoder:string];
  75. // NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles/%@",urlStr,filePathBase64];
  76. NSString *showUrl = [[NSString alloc] initWithFormat:@"%@getThumbnail?path=%@",urlStr,filePath];
  77. HLog(@"video url:%@",fileUrl);
  78. //fileUrl = @"http://transfertest.armclouding.com:10010/getFile?path=/sdcard/bb.mp4";
  79. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/123.mp4";//可以播放
  80. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/movTest.mov";//可以播放
  81. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/yinwei4.mp4";//可以播放
  82. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/fuchouzhe2.mp4";//可以播放
  83. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/fuchouzhe.m4v";//可以播放
  84. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/qisheng.ts";//加载失败
  85. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/wmvTest.wmv";//加载失败
  86. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/复仇者联盟.3gp";//加载失败
  87. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/fuchouzhe3.mpeg";//加载失败
  88. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/fuchouzhe4.mpg";//加载失败
  89. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/fuchouzhe5.rm";//加载失败
  90. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/fuchouzhe5.vob";//加载失败
  91. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/gamemkv.mkv";//加载失败
  92. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/j14_yinyue.ogg";//
  93. //fileUrl = @"http://file.phone.androidscloud.com:8210/document/newFile/download/1/ikIm5C0KjKNvusTF6tIH/LowLevelMultipartUpload_45050149837826456388";//可以播放
  94. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/我的视频.mp4";
  95. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  96. showUrl = [showUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  97. //HLog(@"video url:%@",fileUrl);
  98. NSURL * sourceMovieURL= [NSURL URLWithString:fileUrl];
  99. self.playerManager.assetURL = sourceMovieURL;
  100. [self.controlView showTitle:_VideoDataMode.name coverURLString:showUrl fullScreenMode:ZFFullScreenModeAutomatic];
  101. [self customControlViewUIFun];
  102. }
  103. }
  104. - (void)viewWillDisappear:(BOOL)animated {
  105. [super viewWillDisappear:animated];
  106. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDarkContent];
  107. }
  108. - (BOOL)shouldAutorotate {
  109. return NO;
  110. }
  111. /// 如果不好用则copy VC中 加一下
  112. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  113. if (self.player.isFullScreen) {
  114. return UIInterfaceOrientationMaskLandscape;
  115. }
  116. return UIInterfaceOrientationMaskPortrait;
  117. }
  118. //- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  119. // return UIInterfaceOrientationLandscapeRight;
  120. //}
  121. - (ZFPlayerControlView *)controlView {
  122. if (!_controlView) {
  123. _controlView = [ZFPlayerControlView new];
  124. _controlView.fastViewAnimated = YES;
  125. _controlView.effectViewShow = NO;
  126. _controlView.prepareShowLoading = YES;
  127. //_controlView.showCustomStatusBar = YES;
  128. }
  129. return _controlView;
  130. }
  131. #pragma mark reset controlView UI
  132. - (void)customControlViewUIFun
  133. {
  134. //竖屏
  135. _controlView.portraitControlView.fullScreenBtn.hidden = YES;
  136. //totalTimeLabel slider
  137. [_controlView.portraitControlView.topToolView addSubview:self.portraitBackBtn];
  138. UILabel *titleLab = _controlView.portraitControlView.titleLabel;
  139. titleLab.textAlignment = NSTextAlignmentCenter;
  140. titleLab.lineBreakMode = NSLineBreakByTruncatingMiddle;
  141. CGRect titleLabFrame = titleLab.frame;
  142. HLog(@"%f %f %f %f",titleLabFrame.origin.x,titleLabFrame.origin.y,titleLabFrame.size.width,titleLabFrame.size.height);
  143. HLog(@"");
  144. [titleLab mas_remakeConstraints:^(MASConstraintMaker *make) {
  145. make.left.mas_equalTo(45);
  146. make.right.mas_equalTo(-45);
  147. make.height.mas_equalTo(titleLabFrame.size.height);
  148. make.top.mas_equalTo(titleLabFrame.origin.y);
  149. }];
  150. [_controlView.portraitControlView.topToolView addSubview:self.portraitBackBtn];
  151. [self.portraitBackBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  152. make.left.mas_equalTo(15);
  153. make.width.mas_equalTo(28);
  154. make.height.mas_equalTo(28);
  155. make.top.mas_equalTo(titleLabFrame.origin.y);
  156. }];
  157. [_controlView.portraitControlView.topToolView addSubview:self.portraitToLandScapeBtn];
  158. [self.portraitToLandScapeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  159. make.right.mas_equalTo(-15);
  160. make.width.mas_equalTo(28);
  161. make.height.mas_equalTo(28);
  162. make.top.mas_equalTo(titleLabFrame.origin.y);
  163. }];
  164. //横屏
  165. //_controlView.landScapeControlView.fullScreenBtn.hidden = YES;
  166. UILabel *titleLabInlandScape = _controlView.landScapeControlView.titleLabel;
  167. titleLabInlandScape.textAlignment = NSTextAlignmentCenter;
  168. titleLabInlandScape.lineBreakMode = NSLineBreakByTruncatingMiddle;
  169. CGRect titleLabFrameInlandScape = titleLabInlandScape.frame;
  170. [titleLabInlandScape mas_remakeConstraints:^(MASConstraintMaker *make) {
  171. make.left.mas_equalTo(45);
  172. make.right.mas_equalTo(-45);
  173. make.height.mas_equalTo(titleLabFrameInlandScape.size.height);
  174. make.top.mas_equalTo(titleLabFrameInlandScape.origin.y);
  175. }];
  176. [_controlView.landScapeControlView.topToolView addSubview:self.landScapeToPortraitBtn];
  177. [self.landScapeToPortraitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  178. make.right.mas_equalTo(-15);
  179. make.width.mas_equalTo(28);
  180. make.height.mas_equalTo(28);
  181. make.top.mas_equalTo(titleLabFrame.origin.y);
  182. }];
  183. }
  184. - (UIButton *)portraitBackBtn {
  185. if (!_portraitBackBtn) {
  186. _portraitBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  187. [_portraitBackBtn setImage:ZFPlayer_Image(@"ZFPlayer_back_full") forState:UIControlStateNormal];
  188. [_portraitBackBtn addTarget:self action:@selector(didClickBackBtnFun) forControlEvents:UIControlEventTouchUpInside];
  189. }
  190. return _portraitBackBtn;
  191. }
  192. - (UIButton *)portraitToLandScapeBtn {
  193. if (!_portraitToLandScapeBtn) {
  194. _portraitToLandScapeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  195. [_portraitToLandScapeBtn setImage:[UIImage imageNamed:@"portraitFullScreen_icon"] forState:UIControlStateNormal];
  196. [_portraitToLandScapeBtn addTarget:self action:@selector(portraitOrLandScapeFun:) forControlEvents:UIControlEventTouchUpInside];
  197. //_portraitToLandScapeBtn.backgroundColor = [UIColor greenColor];
  198. }
  199. return _portraitToLandScapeBtn;
  200. }
  201. - (UIButton *)landScapeToPortraitBtn {
  202. if (!_landScapeToPortraitBtn) {
  203. _landScapeToPortraitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  204. [_landScapeToPortraitBtn setImage:[UIImage imageNamed:@"landScapeToPortrait_icon"] forState:UIControlStateNormal];
  205. [_landScapeToPortraitBtn addTarget:self action:@selector(portraitOrLandScapeFun:) forControlEvents:UIControlEventTouchUpInside];
  206. //_portraitToLandScapeBtn.backgroundColor = [UIColor greenColor];
  207. }
  208. return _landScapeToPortraitBtn;
  209. }
  210. #pragma mark 点击返回键
  211. - (void)didClickBackBtnFun
  212. {
  213. [self.player stop];
  214. [self.navigationController popViewControllerAnimated:YES];
  215. }
  216. #pragma mark 横竖屏切换
  217. - (void)portraitOrLandScapeFun:(UIButton*)but
  218. {
  219. if(but ==_portraitToLandScapeBtn){
  220. //[self.player enterFullScreen:YES animated:YES];;
  221. [self.player rotateToOrientation:UIInterfaceOrientationLandscapeRight animated:YES completion:nil];
  222. }
  223. else{
  224. [self.player rotateToOrientation:UIInterfaceOrientationPortrait animated:YES completion:nil];
  225. }
  226. }
  227. @end