audioPlayerViewController.m 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. //
  2. // audioPlayerViewController.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/5/26.
  6. //
  7. #import "audioPlayerViewController.h"
  8. #import "DFPlayer.h"
  9. #import "DFPlayerUIManager.h"
  10. #import "NASFileAudioModel.h"
  11. #import "lastFileManager.h"
  12. #import "audioPlayListManager.h"
  13. #import <QuartzCore/QuartzCore.h>
  14. #import "previewAudioPortraitTopMoreView.h"
  15. #import "editShareView.h"
  16. #import "uploadFileRecordViewController.h"
  17. #import "previewAudioPortraitDetailsView.h"
  18. #import "audioPlayListView.h"
  19. #import "addAudioToPlayListViewController.h"
  20. @interface audioPlayerViewController ()<DFPlayerDelegate,DFPlayerDataSource>
  21. @property (nonatomic, strong) UIImageView*bgImageView;//中间图片
  22. @property (nonatomic, strong) UIImageView*playImageView;//
  23. @property (nonatomic, strong) CABasicAnimation *rotationAnimation;
  24. @property (nonatomic, strong) UILabel *AudioTitleLab;//音乐标题
  25. @property (nonatomic, strong)UIButton * playPauseBtn;
  26. @property (nonatomic, strong)UIButton * playNextBtn;
  27. @property (nonatomic, strong)UIButton * playLastBtn;
  28. @property (nonatomic, strong)UIButton * playListBtn;//歌单
  29. @property (nonatomic, strong)UIButton * playModelBtn;//播放模式(单机 随机 循环)
  30. @property (nonatomic, strong) NSMutableArray<DFPlayerModel *> *dataArray;
  31. @property (nonatomic, assign) NSInteger lastAudioIndex;//-1未记录
  32. @property (nonatomic, strong) audioPlayListView *audioPlayListV;
  33. @end
  34. @implementation audioPlayerViewController
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. // Do any additional setup after loading the view.
  38. [self.toolBar setHidden:YES];
  39. [self.navigationBar setHidden:YES];
  40. [self.navBarBGView setHidden:NO];
  41. self.navBarBGView.backgroundColor = [UIColor whiteColor];
  42. [self.view setBackgroundColor:[UIColor whiteColor]];
  43. [self drawAnyView];
  44. _lastAudioIndex = -1;
  45. }
  46. - (void)drawAnyView{
  47. _bgImageView = [UIImageView new];
  48. _bgImageView.image = [UIImage imageNamed:@"audioBgImg"];
  49. [self.view addSubview:_bgImageView];
  50. [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.top.equalTo(self.navBarBGView.mas_bottom).offset(90);
  52. make.width.mas_equalTo(200);
  53. make.height.mas_equalTo(200);
  54. make.centerX.mas_equalTo(0);
  55. }];
  56. _playImageView = [UIImageView new];
  57. _playImageView.image = [UIImage imageNamed:@"audioPlayImg"];
  58. [self.view addSubview:_playImageView];
  59. [_playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  60. //make.top.equalTo(_bgImageView.mas_top).offset(-19);
  61. make.top.equalTo(_bgImageView.mas_top).offset(-19 - 85);
  62. make.width.mas_equalTo(110);
  63. make.height.mas_equalTo(170);
  64. //make.left.equalTo(_bgImageView.mas_right).offset(-60);
  65. make.left.equalTo(_bgImageView.mas_right).offset(-60 +55);
  66. }];
  67. [self setAnchorPoint:CGPointMake(1.0, 0.0) forView:_playImageView];
  68. _AudioTitleLab = [[UILabel alloc] init];
  69. _AudioTitleLab.textAlignment = NSTextAlignmentCenter;
  70. _AudioTitleLab.textColor = [UIColor blackColor];
  71. _AudioTitleLab.font = [UIFont systemFontOfSize:18.0];
  72. _AudioTitleLab.numberOfLines = 2;
  73. [self.view addSubview:_AudioTitleLab];
  74. [_AudioTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
  75. make.top.equalTo(_bgImageView.mas_bottom).offset(15);
  76. make.left.mas_equalTo(30);
  77. make.right.mas_equalTo(-30);
  78. make.height.mas_equalTo(45);
  79. }];
  80. //底部UI背景
  81. UIImageView* bottombgImageView = [UIImageView new];
  82. bottombgImageView.image = [UIImage imageNamed:@"audioBottomBg"];
  83. bottombgImageView.userInteractionEnabled = YES;
  84. [self.view addSubview:bottombgImageView];
  85. CGFloat bottombgH = SCREEN_W*280.0/375.0;
  86. [bottombgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  87. make.bottom.mas_equalTo(0);
  88. make.left.mas_equalTo(0);
  89. make.right.mas_equalTo(0);
  90. make.height.mas_equalTo(bottombgH);
  91. }];
  92. [self initRightUpButtonFun];
  93. [self initDFPlayer];
  94. }
  95. #pragma mark 右上角按钮
  96. - (void)initRightUpButtonFun
  97. {
  98. //更多按钮
  99. UIButton *morenButton = [[UIButton alloc] init];
  100. [morenButton setImage:[UIImage imageNamed:@"nas_preview_more_black"] forState:UIControlStateNormal];
  101. morenButton.tag = 1;
  102. [morenButton addTarget:self action:@selector(didClickHeadButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  103. [self.view addSubview:morenButton];
  104. [morenButton mas_makeConstraints:^(MASConstraintMaker *make) {
  105. make.centerY.equalTo(self.titleLabel.mas_centerY).offset(0);
  106. make.right.mas_equalTo(-16);
  107. make.width.mas_equalTo(30);
  108. make.height.mas_equalTo(30);
  109. }];
  110. UIButton *shareButton = [[UIButton alloc] init];
  111. [shareButton setImage:[UIImage imageNamed:@"nas_preview_share_black"] forState:UIControlStateNormal];
  112. shareButton.tag = 2;
  113. [shareButton addTarget:self action:@selector(didClickHeadButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  114. [self.view addSubview:shareButton];
  115. [shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
  116. make.centerY.equalTo(self.titleLabel.mas_centerY).offset(0);
  117. make.right.equalTo(morenButton.mas_left).offset(-20);
  118. make.width.mas_equalTo(30);
  119. make.height.mas_equalTo(30);
  120. }];
  121. }
  122. #pragma mark - 以下代码与DFPlayer库有关
  123. #pragma mark - 初始化DFPlayer
  124. - (void)initDFPlayer{
  125. [[DFPlayer sharedPlayer] df_initPlayerWithUserId:nil];
  126. [DFPlayer sharedPlayer].dataSource = self;
  127. [DFPlayer sharedPlayer].delegate = self;
  128. [DFPlayer sharedPlayer].playMode = DFPlayerModeOrderCycle;
  129. [DFPlayer sharedPlayer].isObserveWWAN = NO;
  130. // [[DFPlayer sharedPlayer] df_reloadData];//需在传入数据源后调用
  131. UIImage *nextImage = [UIImage imageNamed:@"dfplayer_next"];
  132. UIImage *lastImage = [UIImage imageNamed:@"dfplayer_last"];
  133. UIImage *playImage = [UIImage imageNamed:@"dfplayer_play"];
  134. UIImage *pauseImage = [UIImage imageNamed:@"dfplayer_pause"];
  135. UIImage *ovalImage = [UIImage imageNamed:@"dfplayer_oval"];
  136. DFPlayerUIManager *mgr = [DFPlayerUIManager sharedManager];
  137. //缓冲条
  138. // [mgr df_bufferViewWithFrame:CGRectZero
  139. // trackTintColor:[[UIColor lightGrayColor] colorWithAlphaComponent:0.5]
  140. // progressTintColor:[UIColor colorWithWhite:1 alpha:0.5]
  141. // superView:self.view];
  142. //播放暂停按钮
  143. //_playPauseBtn = [mgr df_playPauseBtnWithFrame:CGRectZero playImage:playImage pauseImage:pauseImage superView:self.view block:nil];
  144. _playPauseBtn = [[UIButton alloc] init];
  145. [_playPauseBtn setImage:playImage forState:UIControlStateSelected];
  146. [_playPauseBtn setImage:pauseImage forState:UIControlStateNormal];
  147. [_playPauseBtn addTarget:self action:@selector(playOrPauseButtonDidClickFun:) forControlEvents:UIControlEventTouchUpInside];
  148. [self.view addSubview:_playPauseBtn];
  149. _playPauseBtn.selected = YES;
  150. [_playPauseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  151. make.bottom.mas_equalTo(-54);
  152. make.height.mas_equalTo(32);
  153. make.width.mas_equalTo(32);
  154. make.centerX.mas_equalTo(0);
  155. }];
  156. //下一首按钮
  157. _playNextBtn = [mgr df_nextBtnWithFrame:CGRectZero image:nextImage superView:self.view block:nil];
  158. [_playNextBtn setImage:[UIImage imageNamed:@"dfplayer_next_none"] forState:UIControlStateDisabled];
  159. [_playNextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  160. make.centerY.equalTo(_playPauseBtn.mas_centerY).offset(0);
  161. make.height.mas_equalTo(32);
  162. make.width.mas_equalTo(32);
  163. make.left.equalTo(_playPauseBtn.mas_right).offset(50);
  164. }];
  165. //上一首按钮
  166. _playLastBtn = [mgr df_lastBtnWithFrame:CGRectZero image:lastImage superView:self.view block:nil];
  167. [_playLastBtn setImage:[UIImage imageNamed:@"dfplayer_last_none"] forState:UIControlStateDisabled];
  168. [_playLastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  169. make.centerY.equalTo(_playPauseBtn.mas_centerY).offset(0);
  170. make.height.mas_equalTo(32);
  171. make.width.mas_equalTo(32);
  172. make.right.equalTo(_playPauseBtn.mas_left).offset(-50);
  173. }];
  174. //歌单
  175. _playListBtn = [[UIButton alloc] init];
  176. [_playListBtn setImage:[UIImage imageNamed:@"dfplayer_playlist"] forState:UIControlStateNormal];
  177. [_playListBtn addTarget:self action:@selector(didClickPlayListFun) forControlEvents:UIControlEventTouchUpInside];
  178. [self.view addSubview:_playListBtn];
  179. [_playListBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  180. make.centerY.equalTo(_playPauseBtn.mas_centerY).offset(0);
  181. make.height.mas_equalTo(25);
  182. make.width.mas_equalTo(25);
  183. //make.left.equalTo(_playNextBtn.mas_right).offset(50);
  184. make.right.mas_equalTo(-20);
  185. }];
  186. // //播放模式按钮
  187. // [mgr df_typeBtnWithFrame:typeRect singleImage:singleImage circleImage:circleImage shuffleImage:shuffleImage superView:_bgView block:nil];
  188. //播放模型
  189. _playModelBtn = [[UIButton alloc] init];
  190. [_playModelBtn setImage:[UIImage imageNamed:@"dfplayer_circle"] forState:UIControlStateNormal];
  191. [_playModelBtn addTarget:self action:@selector(didClickPlayModelButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  192. [self.view addSubview:_playModelBtn];
  193. [_playModelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  194. make.centerY.equalTo(_playPauseBtn.mas_centerY).offset(0);
  195. make.height.mas_equalTo(25);
  196. make.width.mas_equalTo(25);
  197. make.left.mas_equalTo(20);
  198. }];
  199. //进度条
  200. UISlider * curSlider = [mgr df_sliderWithFrame:CGRectZero
  201. minimumTrackTintColor:[UIColor hwColor:@"#0CDEFD"]
  202. maximumTrackTintColor:[UIColor hwColor:@"#E3E3E3"]
  203. trackHeight:4
  204. thumbImage:[ovalImage imageByResizeToSize:(CGSize){15,14}]
  205. superView:self.view];
  206. [curSlider mas_makeConstraints:^(MASConstraintMaker *make) {
  207. make.bottom.equalTo(_playPauseBtn.mas_top).offset(-40);
  208. make.height.mas_equalTo(40);
  209. make.left.mas_equalTo(70);
  210. make.right.mas_equalTo(-70);
  211. }];
  212. //当前时间
  213. UILabel *currentTimeLabel =[mgr df_currentTimeLabelWithFrame:CGRectZero
  214. textColor:[UIColor hwColor:@"#999999"]
  215. textAlignment:(NSTextAlignmentCenter)
  216. font:[UIFont systemFontOfSize:14.0]
  217. superView:self.view];
  218. [currentTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  219. make.top.equalTo(curSlider.mas_top).offset(0);
  220. make.height.mas_equalTo(40);
  221. make.left.mas_equalTo(5);
  222. make.right.equalTo(curSlider.mas_left).offset(-5);
  223. }];
  224. //总时间
  225. UILabel *totalTimeLabel = [mgr df_totalTimeLabelWithFrame:CGRectZero
  226. textColor:[UIColor hwColor:@"#999999"]
  227. textAlignment:(NSTextAlignmentCenter)
  228. font:[UIFont systemFontOfSize:14.0]
  229. superView:self.view];
  230. [totalTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  231. make.top.equalTo(curSlider.mas_top).offset(0);
  232. make.height.mas_equalTo(40);
  233. make.right.mas_equalTo(-5);
  234. make.left.equalTo(curSlider.mas_right).offset(5);
  235. }];
  236. }
  237. #pragma mark 开始和暂停 按钮点击时间
  238. - (void)playOrPauseButtonDidClickFun:(UIButton*)but
  239. {
  240. but.selected = !but.selected;
  241. if(but.selected){
  242. [self startRotatingImage];
  243. [self startPlayingRotateRightImage];
  244. [[DFPlayer sharedPlayer] df_play];
  245. }
  246. else{
  247. [self stopRotatingImage];
  248. [self stopPlayingRotateRightImage];
  249. [[DFPlayer sharedPlayer] df_pause];
  250. }
  251. }
  252. #pragma mark 开始和暂停 按钮点击时间
  253. - (void)didClickPlayModelButtonFun:(UIButton*)but
  254. {
  255. switch ([DFPlayer sharedPlayer].playMode) {
  256. case DFPlayerModeSingleCycle://单曲循环->顺序循环
  257. [DFPlayer sharedPlayer].playMode = DFPlayerModeOrderCycle;
  258. [but setImage:[UIImage imageNamed:@"dfplayer_circle"] forState:UIControlStateNormal];
  259. [[iToast makeText:NSLocalizedString(@"NAS_audio_playModel_circle",nil)] show];
  260. break;
  261. case DFPlayerModeOrderCycle://顺序循环->随机循环
  262. [DFPlayer sharedPlayer].playMode = DFPlayerModeShuffleCycle;
  263. [but setImage:[UIImage imageNamed:@"dfplayer_shuffle"] forState:UIControlStateNormal];
  264. [[iToast makeText:NSLocalizedString(@"NAS_audio_playModel_shuffle",nil)] show];
  265. break;
  266. case DFPlayerModeShuffleCycle://随机循环->单曲循环
  267. [DFPlayer sharedPlayer].playMode = DFPlayerModeSingleCycle;
  268. [but setImage:[UIImage imageNamed:@"dfplayer_single"] forState:UIControlStateNormal];
  269. [[iToast makeText:NSLocalizedString(@"NAS_audio_playModel_single",nil)] show];
  270. break;
  271. default:
  272. break;
  273. }
  274. }
  275. #pragma mark 点击右上角 更多或分享
  276. - (void)didClickHeadButtonFun:(UIButton*)but
  277. {
  278. NSInteger tag = but.tag;
  279. if(tag == 1){
  280. previewAudioPortraitTopMoreView *topMoreV = [[previewAudioPortraitTopMoreView alloc] init];
  281. [self.view addSubview:topMoreV];
  282. [topMoreV mas_makeConstraints:^(MASConstraintMaker *make) {
  283. make.top.mas_equalTo(0);
  284. make.bottom.mas_equalTo(0);
  285. make.right.mas_equalTo(0);
  286. make.left.mas_equalTo(0);
  287. }];
  288. KWeakSelf
  289. topMoreV.didClickButtonFun = ^(NSInteger tag) {
  290. switch (tag) {
  291. case 10:
  292. [weakSelf gotoDownLoadFileFun];
  293. break;
  294. case 11:
  295. [weakSelf showDeleteAlearViewFun];
  296. break;
  297. case 12:
  298. [weakSelf didClickDetailsFun];
  299. break;
  300. default:
  301. break;
  302. }
  303. };
  304. }
  305. else if (tag == 2){
  306. [self gotoShareViewFun];
  307. }
  308. }
  309. #pragma mark 用户点击分享
  310. - (void)gotoShareViewFun
  311. {
  312. editShareView *editShareV = [[editShareView alloc] init];
  313. NASFileAudioDataModel *dataModel = _audioOutSidedataArray[_index];
  314. editShareV.didSelectListArr = [NSMutableArray arrayWithArray:@[dataModel]];
  315. editShareV.shareFileType = @"4";
  316. [self.view addSubview:editShareV];
  317. [editShareV mas_makeConstraints:^(MASConstraintMaker *make) {
  318. make.left.mas_equalTo(0);
  319. make.right.mas_equalTo(0);
  320. make.bottom.mas_equalTo(0);
  321. make.top.mas_equalTo(0);
  322. }];
  323. }
  324. #pragma mark 用户点击下载
  325. - (void)gotoDownLoadFileFun
  326. {
  327. NASFileAudioDataModel *dataModel = _audioOutSidedataArray[_index];
  328. couldPhoneFileModel* fileModel = [couldPhoneFileModel new];
  329. fileModel.fileType = @"audio";
  330. fileModel.path = dataModel.path;
  331. fileModel.name = dataModel.name;
  332. fileModel.length = dataModel.size;
  333. NSMutableArray *arr = [NSMutableArray new];
  334. [arr addObject:fileModel];
  335. uploadFileRecordViewController *vc = [uploadFileRecordViewController new];
  336. [self.navigationController pushViewController:vc animated:YES];
  337. vc.isDownloadingType = YES;
  338. [vc gotoDownloadFile:arr];
  339. }
  340. #pragma mark 用户点击删除
  341. - (void)showDeleteAlearViewFun
  342. {
  343. NSString *titleStr = NSLocalizedString(@"delete_file_title_msg",nil);
  344. NSString *tipStr = NSLocalizedString(@"delete_file_tip_msg",nil);
  345. KWeakSelf
  346. ComontAlretViewController *curAlretVC= [[ComontAlretViewController alloc] initWithTiTle:titleStr
  347. msg:tipStr
  348. imageStr:nil
  349. cancelTitle:NSLocalizedString(@"other_cancel",nil)
  350. okTitle:NSLocalizedString(@"other_confirm",nil) isOkBtnHighlight:YES
  351. didClickOk:^{
  352. [weakSelf delFileListFun];
  353. } didClickCancel:^{
  354. }];
  355. curAlretVC.modalPresentationStyle = UIModalPresentationCustom;
  356. [self presentViewController:curAlretVC animated:YES completion:^{
  357. curAlretVC.view.superview.backgroundColor = [UIColor clearColor];
  358. }];
  359. }
  360. #pragma mark 删除文件数据
  361. - (void)delFileListFun
  362. {
  363. NSMutableDictionary*paraDict = [NSMutableDictionary new];
  364. NSMutableArray *pathArr = [NSMutableArray new];
  365. NASFileAudioDataModel *dataModel = _audioOutSidedataArray[_index];
  366. [pathArr addObject:dataModel.path];
  367. [paraDict setValue:pathArr forKey:@"path"];
  368. [self showNewIndicatorWithCanBack:YES canTouch:NO];
  369. //NSString*code = [[NSString alloc] initWithFormat:@"delFile?path=%@",paraDict[@"path"]]; //delFile?path=[/storage/emulated/0/Download/IMG_6464.HEIC]
  370. KWeakSelf //@"delFile"
  371. [[netWorkManager shareInstance] cloudPhonePostCallBackCode:@"delFile" Parameters:paraDict success:^(id _Nonnull responseObject) {
  372. [weakSelf removeNewIndicator];
  373. SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil];
  374. if(model && model.status == 0){
  375. [[iToast makeText:NSLocalizedString(@"delete_file_suc_msg",nil)] show];
  376. [weakSelf didDeleteSucFun:dataModel.path];
  377. }
  378. else{
  379. }
  380. } failure:^(NSError * _Nonnull error) {
  381. [weakSelf removeNewIndicator];
  382. }];
  383. }
  384. #pragma mark 删除成功
  385. - (void)didDeleteSucFun:(NSString*)path
  386. {
  387. //1.删除最近文件数据
  388. [[lastFileManager shareManager] deleteFileInfoWithUrl:path];
  389. [[audioPlayListManager shareManager] deleteFileInfoWithUrl:path];
  390. if(_didNeedToRegetDataFun){
  391. _didNeedToRegetDataFun();
  392. }
  393. [self.navigationController popViewControllerAnimated:YES];
  394. }
  395. #pragma mark 用户点击详情
  396. - (void)didClickDetailsFun
  397. {
  398. NSMutableArray *pathArr = [NSMutableArray new];
  399. NASFileAudioDataModel *dataModel = _audioOutSidedataArray[_index];
  400. previewAudioPortraitDetailsView *topDetailsV = [[previewAudioPortraitDetailsView alloc] initWithFrame:CGRectZero withData:dataModel];
  401. [self.view addSubview:topDetailsV];
  402. [topDetailsV mas_makeConstraints:^(MASConstraintMaker *make) {
  403. make.top.mas_equalTo(0);
  404. make.bottom.mas_equalTo(0);
  405. make.right.mas_equalTo(0);
  406. make.left.mas_equalTo(0);
  407. }];
  408. }
  409. #pragma mark 用户点击播放列表
  410. - (void)didClickPlayListFun
  411. {
  412. _audioPlayListV = [[audioPlayListView alloc] initWithFrame:CGRectZero withIndex:_index];
  413. [self.view addSubview:_audioPlayListV];
  414. [_audioPlayListV mas_makeConstraints:^(MASConstraintMaker *make) {
  415. make.top.mas_equalTo(0);
  416. make.bottom.mas_equalTo(0);
  417. make.right.mas_equalTo(0);
  418. make.left.mas_equalTo(0);
  419. }];
  420. KWeakSelf
  421. _audioPlayListV.didClickButtonFun = ^{
  422. [weakSelf gotoAddAudioToPlayListVCFun];
  423. };
  424. }
  425. #pragma mark 跳转添加音乐到播放列表
  426. - (void)gotoAddAudioToPlayListVCFun
  427. {
  428. addAudioToPlayListViewController *vc = [addAudioToPlayListViewController new];
  429. [self.navigationController pushViewController:vc animated:YES];
  430. }
  431. #pragma mark - DFPLayer dataSource
  432. - (NSArray<DFPlayerModel *> *)df_audioDataForPlayer:(DFPlayer *)player{
  433. _dataArray = [NSMutableArray array];
  434. for (int i = 0; i < _audioOutSidedataArray.count; i++) {
  435. NASFileAudioDataModel *yourModel = _audioOutSidedataArray[i];
  436. DFPlayerModel *model = [[DFPlayerModel alloc] init];
  437. model.audioId = i;//****重要。AudioId从0开始,仅标识当前音频在数组中的位置。
  438. NSString *filePath = yourModel.path;
  439. NSString *urlStr = ksharedAppDelegate.NASFileByBoxService;
  440. NSString *string = filePath;
  441. NSString *filePathBase64 = [iTools base64UrlEncoder:string];
  442. // NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
  443. // NSString *filePathBase64 = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
  444. // NSData *data2 = [[NSData alloc] initWithBase64EncodedString:filePathBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
  445. //
  446. // NSString *string32 =[[NSString alloc] initWithData:data2 encoding:NSUTF8StringEncoding];
  447. // HLog(@"hxd1111: %@",string32);
  448. //filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  449. //NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFile?path=%@",urlStr,filePath];
  450. //NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles?path=%@",urlStr,filePath];
  451. NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles/%@",urlStr,filePathBase64];
  452. HLog(@"hxd2222:%@",fileUrl);
  453. //fileUrl = @"http://transfertest.armclouding.com:10006/getFile?path=/storage/emulated/0/Download/录音大师-2024.05.22-16:57.mp3";
  454. //fileUrl = @"http://m10.music.126.net/20240527160012/d3f165dc686ac01afd4497400b2c2c58/ymusic/5353/0f0f/0358/d99739615f8e5153d77042092f07fd77.mp3";
  455. //fileUrl = @"https://www.cambridgeenglish.org/images/153149-movers-sample-listening-test-vol2.mp3";
  456. //fileUrl = @"https://www.cambridgeenglish.org/images/506891-a2-key-for-schools-listening-sample-test.mp3";
  457. //fileUrl = @"http://downsc.chinaz.net/Files/DownLoad/sound1/201906/11582.mp3";
  458. //fileUrl = @"http://downsc.chinaz.net/files/download/sound1/201206/1638.mp3";
  459. //fileUrl =@"http://192.168.11.248:9888/getFile?path=/storage/C47D-46D2/audioTest/jiajiaeeyinyue.mp3";
  460. //fileUrl =@"http://transfertest.armclouding.com:21025/getFiles/L3N0b3JhZ2UvZW11bGF0ZWQvMC9NdXNpYy9hYmMubXAz";
  461. //fileUrl = @"http://downsc.chinaz.net/files/download/sound1/201206/1638.mp3";
  462. //fileUrl = @"http://transfertest.armclouding.com:21025/test/abc/abc.mp3";
  463. //fileUrl = @"http://transfertest.armclouding.com:21025/test/abc/obj.mp3"; //0603 16:40验证可以播放
  464. //fileUrl = @"http://file.phone.androidscloud.com:8210/document/newFile/download/1/ikIm5C0KjKNvusTF6tIH/LowLevelMultipartUpload_44933618366140107699/1.mp3";
  465. // fileUrl = @"http://transfertest.armclouding.com:10001/getFile?path=/storage/6C07-E638/miniType/%E9%9F%B3%E9%A2%91%E6%A0%BC%E5%BC%8Fing/jiajiaeeyinyue.mp3";
  466. //fileUrl = @"http://transfertest.armclouding.com:21025/getFiles/L3N0b3JhZ2UvZW11bGF0ZWQvMC9NdXNpYy9vYmoubXAz";
  467. //fileUrl = @"http://transfertest.armclouding.com:21025/getFiles/L3NkY2FyZC9NdXNpYy9vYmoubXAz";
  468. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/25883214_yinyue.wma";//不支持此媒体的格式
  469. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/ffgfge.m4a";//可以播放
  470. //fileUrl = @"http://testprivacy.phone.armclouding.com:1801/h5/j14_yinyue.ogg";//不支持此媒体的格式
  471. // fileUrl = [fileUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  472. NSURL * curURL = [NSURL fileURLWithPath:fileUrl];
  473. HLog(@"%@",curURL.absoluteString);
  474. model.audioUrl = curURL;
  475. // if ([yourModel.yourUrl hasPrefix:@"http"]) {//网络音频
  476. // model.audioUrl = [self getAvailableURL:yourModel.yourUrl];
  477. // }else{//本地音频
  478. // NSString *path = [[NSBundle mainBundle] pathForResource:yourModel.yourUrl ofType:@""];
  479. // if (path) {
  480. // model.audioUrl = [NSURL fileURLWithPath:path];
  481. // }
  482. // }
  483. [_dataArray addObject:model];
  484. }
  485. HLog(@"%@ --- %ld",_dataArray,_dataArray.count);
  486. return [_dataArray copy];
  487. }
  488. - (DFPlayerInfoModel *)df_audioInfoForPlayer:(DFPlayer *)player{
  489. DFPlayerInfoModel *infoModel = [[DFPlayerInfoModel alloc] init];
  490. // infoModel.audioName = @"";//yourModel.yourName;
  491. // infoModel.audioSinger = @"";//yourModel.yourSinger;
  492. // infoModel.audioAlbum = @"";//yourModel.yourAlbum;
  493. // infoModel.audioLyrics = @"";//[NSString stringWithContentsOfFile:lyricPath encoding:NSUTF8StringEncoding error:nil];
  494. //infoModel.audioImage = [UIImage imageWithData:imageData];
  495. return infoModel;
  496. }
  497. #pragma mark - DFPlayer delegate
  498. //加入播放队列
  499. - (void)df_playerAudioAddToPlayQueue:(DFPlayer *)player{
  500. // [self tableViewReloadData];
  501. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  502. // dispatch_async(dispatch_get_main_queue(), ^{
  503. // self.navigationItem.title = player.currentAudioInfoModel.audioName;
  504. // self->_bgView.image = [self getBackgroundImage:player.currentAudioInfoModel.audioImage];
  505. // self->_noticeLabel.text = player.currentAudioInfoModel.audioLyrics ? @"" : @"无可用歌词";
  506. // });
  507. // });
  508. }
  509. //缓冲进度代理
  510. - (void)df_player:(DFPlayer *)player bufferProgress:(CGFloat)bufferProgress{
  511. HLog(@"缓冲进度代理:%f",bufferProgress);
  512. }
  513. //播放进度代理
  514. - (void)df_player:(DFPlayer *)player progress:(CGFloat)progress currentTime:(CGFloat)currentTime{
  515. HLog(@"音频播放进度:%f --- %f",progress,currentTime);
  516. if(_lastAudioIndex != player.currentAudioModel.audioId){
  517. _lastAudioIndex = player.currentAudioModel.audioId;
  518. _index = _lastAudioIndex;
  519. [self setTitleFunByIndex];
  520. [self audioPlayerByIndex:_lastAudioIndex];
  521. [self startRotatingImage];
  522. [self startPlayingRotateRightImage];
  523. }
  524. if(progress >= 1.0){
  525. _lastAudioIndex = -1;
  526. [self stopRotatingImage];
  527. }
  528. }
  529. //状态信息代理
  530. - (void)df_player:(DFPlayer *)player didGetStatusCode:(DFPlayerStatusCode)statusCode{
  531. if (statusCode == DFPlayerStatusNoNetwork) {
  532. //[self showAlert:@"没有网络连接"];
  533. }else if(statusCode == DFPlayerStatusViaWWAN){
  534. // [self showAlert:@"继续播放将产生流量费用" okBlock:^{
  535. // [DFPlayer sharedPlayer].isObserveWWAN = NO;
  536. // [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  537. // }];
  538. [DFPlayer sharedPlayer].isObserveWWAN = NO;
  539. [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  540. }else if(statusCode == DFPlayerStatusTimeOut){
  541. //[self showAlert:@"请求超时"];
  542. }else if(statusCode == DFPlayerStatusCacheSucc){
  543. [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  544. }else{
  545. HLog(@"状态码:%lu",(unsigned long)statusCode);
  546. [[iToast makeText:NSLocalizedString(@"play_video_fail_tip",nil)] show];
  547. [[DFPlayer sharedPlayer] df_next];
  548. }
  549. }
  550. - (void)viewDidAppear:(BOOL)animated
  551. {
  552. [super viewDidAppear:animated];
  553. [self setTitleFunByIndex];
  554. [[DFPlayer sharedPlayer] df_reloadData];//需在传入数据源后调用
  555. [DFPlayer sharedPlayer].playMode = DFPlayerModeOrderCycle;
  556. KWeakSelf
  557. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  558. [weakSelf playAudioFun];
  559. });
  560. }
  561. - (void)viewDidDisappear:(BOOL)animated
  562. {
  563. [super viewDidDisappear:animated];
  564. //[[DFPlayer sharedPlayer] df_deallocPlayer];
  565. }
  566. -(void)setTitleFunByIndex
  567. {
  568. if(_index < _audioOutSidedataArray.count){
  569. NASFileAudioDataModel* dataModel = _audioOutSidedataArray[_index];
  570. //self.titleLabel.text = dataModel.name;
  571. _AudioTitleLab.text = dataModel.name;
  572. if(_audioPlayListV){
  573. _audioPlayListV.playingIndex = _index;
  574. }
  575. }
  576. }
  577. - (void)playAudioFun
  578. {
  579. if(_index < self.dataArray.count){
  580. DFPlayerModel *model = self.dataArray[_index];
  581. [[DFPlayer sharedPlayer] df_playWithAudioId:model.audioId];
  582. }
  583. }
  584. #pragma mark 音频可以播放
  585. - (void)audioPlayerByIndex:(NSInteger)index{
  586. if(index >=0 && index < _audioOutSidedataArray.count){
  587. NASFileAudioDataModel *dataModel = _audioOutSidedataArray[index];
  588. lastFileModel *lastFileMod = [lastFileModel new];
  589. lastFileMod.path = dataModel.path;
  590. lastFileMod.name = dataModel.name;
  591. lastFileMod.time = dataModel.time;
  592. lastFileMod.size = dataModel.size;
  593. lastFileMod.duration = dataModel.duration;
  594. lastFileMod.type = @"audio";
  595. lastFileMod.lastPreTime = [iTools getNowTimeStamp];
  596. [[lastFileManager shareManager] saveFileInfoWith:lastFileMod with:dataModel.path];
  597. [[audioPlayListManager shareManager] saveFileInfoWith:lastFileMod with:dataModel.path];
  598. }
  599. }
  600. #pragma mark 图片旋转相关
  601. - (void)startRotatingImage {
  602. // 停止当前动画(如果有的话)
  603. [self stopRotatingImage];
  604. // 创建一个CABasicAnimation实例
  605. self.rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  606. // 设置动画的起始值(从0度开始)
  607. self.rotationAnimation.fromValue = [NSNumber numberWithFloat:0];
  608. // 设置动画的结束值(旋转360度,注意这里是弧度制)
  609. self.rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2];
  610. // 设置动画的持续时间
  611. self.rotationAnimation.duration = 5.0; // 可以根据需要调整旋转速度
  612. // 设置动画的重复次数,这里设置为HUGE_VALF表示无限次
  613. self.rotationAnimation.repeatCount = HUGE_VALF;
  614. // 将动画添加到imageView的layer上
  615. [self.bgImageView.layer addAnimation:self.rotationAnimation forKey:@"rotateAnimation"];
  616. }
  617. - (void)stopRotatingImage {
  618. // 移除imageView上的动画
  619. [self.bgImageView.layer removeAnimationForKey:@"rotateAnimation"];
  620. [UIView animateWithDuration:5 animations:^{
  621. self.bgImageView.layer.transform = CATransform3DIdentity;
  622. }];
  623. }
  624. - (void)restartRotatingImage {
  625. // 直接调用startRotatingImage来重新启动动画
  626. [self startRotatingImage];
  627. }
  628. - (void)startPlayingRotateRightImage
  629. {
  630. [UIView animateWithDuration:2 animations:^{
  631. self.playImageView.transform = CGAffineTransformMakeRotation(0);
  632. //self.playImageView.transform = CGAffineTransformRotate(self.playImageView.transform, -M_PI / 6); // 逆时针旋转30度
  633. }];
  634. }
  635. - (void)stopPlayingRotateRightImage
  636. {
  637. [UIView animateWithDuration:2 animations:^{
  638. self.playImageView.transform = CGAffineTransformMakeRotation(-M_PI*0.15);
  639. }];
  640. }
  641. //改变旋转中心???
  642. - (void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
  643. {
  644. CGPoint oldOrigin = view.frame.origin;
  645. view.layer.anchorPoint = anchorPoint;
  646. CGPoint newOrigin = view.frame.origin;
  647. CGPoint transition;
  648. transition.x = newOrigin.x - oldOrigin.x;
  649. transition.y = newOrigin.y - oldOrigin.y;
  650. view.center = CGPointMake (view.center.x - transition.x, view.center.y - transition.y);
  651. }
  652. @end