videoPlayByAVPlayerViewController.m 12 KB

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