audioPlayerViewController.m 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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 "lastFileManager.h"
  11. #import "audioPlayListManager.h"
  12. #import <QuartzCore/QuartzCore.h>
  13. #import "previewAudioPortraitTopMoreView.h"
  14. #import "editShareView.h"
  15. #import "uploadFileRecordViewController.h"
  16. #import "previewAudioPortraitDetailsView.h"
  17. #import "audioPlayListView.h"
  18. #import "addAudioToPlayListViewController.h"
  19. @interface audioPlayerViewController ()<DFPlayerDelegate,DFPlayerDataSource>
  20. {
  21. NSInteger playDelayed;
  22. }
  23. @property (nonatomic, strong) UIImageView*bgImageView;//中间图片
  24. @property (nonatomic, strong) UIImageView*playImageView;//
  25. @property (nonatomic, strong) CABasicAnimation *rotationAnimation;
  26. @property (nonatomic, strong) UILabel *AudioTitleLab;//音乐标题
  27. @property (nonatomic, strong)UIButton * playPauseBtn;
  28. @property (nonatomic, strong)UIButton * playNextBtn;
  29. @property (nonatomic, strong)UIButton * playLastBtn;
  30. @property (nonatomic, strong)UIButton * playListBtn;//歌单
  31. @property (nonatomic, strong)UIButton * playModelBtn;//播放模式(单机 随机 循环)
  32. @property (nonatomic, assign) NSInteger index;//当前位置
  33. @property (nonatomic, strong) NSMutableArray * allAudioDataArray;//所有的 播放数据(lastFileModel)
  34. @property (nonatomic, strong) NSMutableArray<DFPlayerModel *> *dataArray;
  35. @property (nonatomic, assign) NSInteger lastAudioIndex;//-1未记录
  36. @property (nonatomic, strong) audioPlayListView *audioPlayListV;
  37. @end
  38. @implementation audioPlayerViewController
  39. - (void)viewDidLoad {
  40. [super viewDidLoad];
  41. // Do any additional setup after loading the view.
  42. [self.toolBar setHidden:YES];
  43. [self.navigationBar setHidden:YES];
  44. [self.navBarBGView setHidden:NO];
  45. self.navBarBGView.backgroundColor = [UIColor whiteColor];
  46. [self.view setBackgroundColor:[UIColor whiteColor]];
  47. [self drawAnyView];
  48. _lastAudioIndex = -1;
  49. //数据埋点
  50. [[netWorkManager shareInstance] DataEmbeddingPointBy:4 withEventValue:@"Music_preview"];
  51. }
  52. - (void)viewDidAppear:(BOOL)animated
  53. {
  54. [super viewDidAppear:animated];
  55. if(_outSideDataModel && _isfirstEnterType){
  56. [[DFPlayer sharedPlayer] df_pause];
  57. [[DFPlayer sharedPlayer] df_reloadData];//需在传入数据源后调用
  58. [self setTitleFunByIndex];
  59. //begin 20240729 修复音频播放时,进入播放列表点击当前播放歌曲后,会重头播放
  60. if([DFPlayer sharedPlayer].currentAudioModel){
  61. NSString *filePath = _outSideDataModel.path;
  62. if([[DFPlayer sharedPlayer].currentAudioModel.filePath isEqualToString:filePath]){
  63. [[DFPlayer sharedPlayer] df_play];
  64. return;//不重新播放
  65. }
  66. }//end 20240729 修复音频播放时,进入播放列表点击当前播放歌曲后,会重头播放
  67. KWeakSelf
  68. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  69. [weakSelf playAudioFun];
  70. });
  71. }
  72. else{//点击playingview 进来的
  73. _allAudioDataArray = [audioPlayListManager shareManager].audioPlayListArr;
  74. _lastAudioIndex = -1;
  75. _index = [DFPlayer sharedPlayer].currentAudioModel.audioId;
  76. [self setTitleFunByIndex];
  77. }
  78. }
  79. - (void)viewDidDisappear:(BOOL)animated
  80. {
  81. [super viewDidDisappear:animated];
  82. //[[DFPlayer sharedPlayer] df_deallocPlayer];
  83. }
  84. - (void)drawAnyView{
  85. _bgImageView = [UIImageView new];
  86. _bgImageView.image = [UIImage imageNamed:@"audioBgImg"];
  87. [self.view addSubview:_bgImageView];
  88. [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  89. make.top.equalTo(self.navBarBGView.mas_bottom).offset(90);
  90. make.width.mas_equalTo(200);
  91. make.height.mas_equalTo(200);
  92. make.centerX.mas_equalTo(0);
  93. }];
  94. _playImageView = [UIImageView new];
  95. _playImageView.image = [UIImage imageNamed:@"audioPlayImg"];
  96. [self.view addSubview:_playImageView];
  97. [_playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  98. //make.top.equalTo(_bgImageView.mas_top).offset(-19);
  99. make.top.equalTo(_bgImageView.mas_top).offset(-19 - 85);
  100. make.width.mas_equalTo(110);
  101. make.height.mas_equalTo(170);
  102. //make.left.equalTo(_bgImageView.mas_right).offset(-60);
  103. make.left.equalTo(_bgImageView.mas_right).offset(-60 +55);
  104. }];
  105. [self setAnchorPoint:CGPointMake(1.0, 0.0) forView:_playImageView];
  106. _AudioTitleLab = [[UILabel alloc] init];
  107. _AudioTitleLab.textAlignment = NSTextAlignmentCenter;
  108. _AudioTitleLab.textColor = [UIColor blackColor];
  109. _AudioTitleLab.font = [UIFont systemFontOfSize:18.0];
  110. _AudioTitleLab.numberOfLines = 2;
  111. [self.view addSubview:_AudioTitleLab];
  112. [_AudioTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
  113. make.top.equalTo(_bgImageView.mas_bottom).offset(15);
  114. make.left.mas_equalTo(30);
  115. make.right.mas_equalTo(-30);
  116. make.height.mas_equalTo(45);
  117. }];
  118. //底部UI背景
  119. UIImageView* bottombgImageView = [UIImageView new];
  120. bottombgImageView.image = [UIImage imageNamed:@"audioBottomBg"];
  121. bottombgImageView.userInteractionEnabled = YES;
  122. [self.view addSubview:bottombgImageView];
  123. CGFloat bottombgH = SCREEN_W*280.0/375.0;
  124. [bottombgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  125. make.bottom.mas_equalTo(0);
  126. make.left.mas_equalTo(0);
  127. make.right.mas_equalTo(0);
  128. make.height.mas_equalTo(bottombgH);
  129. }];
  130. [self initRightUpButtonFun];
  131. [self initDFPlayer];
  132. }
  133. #pragma mark 右上角按钮
  134. - (void)initRightUpButtonFun
  135. {
  136. //更多按钮
  137. UIButton *morenButton = [[UIButton alloc] init];
  138. [morenButton setImage:[UIImage imageNamed:@"nas_preview_more_black"] forState:UIControlStateNormal];
  139. morenButton.tag = 1;
  140. [morenButton addTarget:self action:@selector(didClickHeadButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  141. [self.view addSubview:morenButton];
  142. [morenButton mas_makeConstraints:^(MASConstraintMaker *make) {
  143. make.centerY.equalTo(self.titleLabel.mas_centerY).offset(0);
  144. make.right.mas_equalTo(-16);
  145. make.width.mas_equalTo(30);
  146. make.height.mas_equalTo(30);
  147. }];
  148. UIButton *shareButton = [[UIButton alloc] init];
  149. [shareButton setImage:[UIImage imageNamed:@"nas_preview_share_black"] forState:UIControlStateNormal];
  150. shareButton.tag = 2;
  151. [shareButton addTarget:self action:@selector(didClickHeadButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  152. [self.view addSubview:shareButton];
  153. [shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
  154. make.centerY.equalTo(self.titleLabel.mas_centerY).offset(0);
  155. make.right.equalTo(morenButton.mas_left).offset(-20);
  156. make.width.mas_equalTo(30);
  157. make.height.mas_equalTo(30);
  158. }];
  159. }
  160. #pragma mark - 以下代码与DFPlayer库有关
  161. #pragma mark - 初始化DFPlayer
  162. - (void)initDFPlayer{
  163. NSInteger playMode = [HWDataManager getIntegerWithKey:@"Const_audio_playMode_record"];
  164. [[DFPlayer sharedPlayer] df_initPlayerWithUserId:nil];
  165. [DFPlayer sharedPlayer].dataSource = self;
  166. [DFPlayer sharedPlayer].delegate = self;
  167. if(playMode <= DFPlayerModeOnlyOnce || playMode > DFPlayerModeShuffleCycle){
  168. [DFPlayer sharedPlayer].playMode = DFPlayerModeOrderCycle;
  169. }
  170. else{
  171. [DFPlayer sharedPlayer].playMode = playMode;
  172. }
  173. [DFPlayer sharedPlayer].isObserveWWAN = NO;
  174. // [[DFPlayer sharedPlayer] df_reloadData];//需在传入数据源后调用
  175. UIImage *nextImage = [UIImage imageNamed:@"dfplayer_next"];
  176. UIImage *lastImage = [UIImage imageNamed:@"dfplayer_last"];
  177. UIImage *playImage = [UIImage imageNamed:@"dfplayer_play"];
  178. UIImage *pauseImage = [UIImage imageNamed:@"dfplayer_pause"];
  179. UIImage *ovalImage = [UIImage imageNamed:@"dfplayer_oval"];
  180. DFPlayerUIManager *mgr = [DFPlayerUIManager sharedManager];
  181. //缓冲条
  182. // [mgr df_bufferViewWithFrame:CGRectZero
  183. // trackTintColor:[[UIColor lightGrayColor] colorWithAlphaComponent:0.5]
  184. // progressTintColor:[UIColor colorWithWhite:1 alpha:0.5]
  185. // superView:self.view];
  186. //播放暂停按钮
  187. //_playPauseBtn = [mgr df_playPauseBtnWithFrame:CGRectZero playImage:playImage pauseImage:pauseImage superView:self.view block:nil];
  188. _playPauseBtn = [[UIButton alloc] init];
  189. [_playPauseBtn setImage:playImage forState:UIControlStateSelected];
  190. [_playPauseBtn setImage:pauseImage forState:UIControlStateNormal];
  191. [_playPauseBtn addTarget:self action:@selector(playOrPauseButtonDidClickFun:) forControlEvents:UIControlEventTouchUpInside];
  192. [self.view addSubview:_playPauseBtn];
  193. _playPauseBtn.selected = YES;
  194. [_playPauseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  195. make.bottom.mas_equalTo(-54);
  196. make.height.mas_equalTo(32);
  197. make.width.mas_equalTo(32);
  198. make.centerX.mas_equalTo(0);
  199. }];
  200. //下一首按钮
  201. //_playNextBtn = [mgr df_nextBtnWithFrame:CGRectZero image:nextImage superView:self.view block:nil];
  202. _playNextBtn = [[UIButton alloc] init];
  203. [_playNextBtn setImage:[UIImage imageNamed:@"dfplayer_next_none"] forState:UIControlStateDisabled];
  204. [_playNextBtn setImage:nextImage forState:UIControlStateNormal];
  205. [_playNextBtn addTarget:self action:@selector(playNextButtonFun) forControlEvents:UIControlEventTouchUpInside];
  206. [self.view addSubview:_playNextBtn];
  207. [_playNextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  208. make.centerY.equalTo(_playPauseBtn.mas_centerY).offset(0);
  209. make.height.mas_equalTo(32);
  210. make.width.mas_equalTo(32);
  211. make.left.equalTo(_playPauseBtn.mas_right).offset(50);
  212. }];
  213. //上一首按钮
  214. //_playLastBtn = [mgr df_lastBtnWithFrame:CGRectZero image:lastImage superView:self.view block:nil];
  215. _playLastBtn = [[UIButton alloc] init];
  216. [_playLastBtn setImage:[UIImage imageNamed:@"dfplayer_last_none"] forState:UIControlStateDisabled];
  217. [_playLastBtn setImage:lastImage forState:UIControlStateNormal];
  218. [_playLastBtn addTarget:self action:@selector(playLastButtonFun) forControlEvents:UIControlEventTouchUpInside];
  219. [self.view addSubview:_playLastBtn];
  220. [_playLastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  221. make.centerY.equalTo(_playPauseBtn.mas_centerY).offset(0);
  222. make.height.mas_equalTo(32);
  223. make.width.mas_equalTo(32);
  224. make.right.equalTo(_playPauseBtn.mas_left).offset(-50);
  225. }];
  226. //歌单
  227. _playListBtn = [[UIButton alloc] init];
  228. [_playListBtn setImage:[UIImage imageNamed:@"dfplayer_playlist"] forState:UIControlStateNormal];
  229. [_playListBtn addTarget:self action:@selector(didClickPlayListFun) forControlEvents:UIControlEventTouchUpInside];
  230. [self.view addSubview:_playListBtn];
  231. [_playListBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  232. make.centerY.equalTo(_playPauseBtn.mas_centerY).offset(0);
  233. make.height.mas_equalTo(25);
  234. make.width.mas_equalTo(25);
  235. //make.left.equalTo(_playNextBtn.mas_right).offset(50);
  236. make.right.mas_equalTo(-20);
  237. }];
  238. // //播放模式按钮
  239. // [mgr df_typeBtnWithFrame:typeRect singleImage:singleImage circleImage:circleImage shuffleImage:shuffleImage superView:_bgView block:nil];
  240. UIImage *playModelImage = [UIImage imageNamed:@"dfplayer_circle"];
  241. if ([DFPlayer sharedPlayer].playMode == DFPlayerModeShuffleCycle){
  242. playModelImage = [UIImage imageNamed:@"dfplayer_shuffle"];
  243. }
  244. else if ([DFPlayer sharedPlayer].playMode == DFPlayerModeSingleCycle){
  245. playModelImage = [UIImage imageNamed:@"dfplayer_single"];
  246. }
  247. //播放模型
  248. _playModelBtn = [[UIButton alloc] init];
  249. [_playModelBtn setImage:playModelImage forState:UIControlStateNormal];
  250. [_playModelBtn addTarget:self action:@selector(didClickPlayModelButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  251. [self.view addSubview:_playModelBtn];
  252. [_playModelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  253. make.centerY.equalTo(_playPauseBtn.mas_centerY).offset(0);
  254. make.height.mas_equalTo(25);
  255. make.width.mas_equalTo(25);
  256. make.left.mas_equalTo(20);
  257. }];
  258. //进度条
  259. UISlider * curSlider = [mgr df_sliderWithFrame:CGRectZero
  260. minimumTrackTintColor:[UIColor hwColor:@"#0CDEFD"]
  261. maximumTrackTintColor:[UIColor hwColor:@"#E3E3E3"]
  262. trackHeight:4
  263. thumbImage:[ovalImage imageByResizeToSize:(CGSize){15,14}]
  264. superView:self.view];
  265. [curSlider mas_makeConstraints:^(MASConstraintMaker *make) {
  266. make.bottom.equalTo(_playPauseBtn.mas_top).offset(-40);
  267. make.height.mas_equalTo(40);
  268. make.left.mas_equalTo(70);
  269. make.right.mas_equalTo(-70);
  270. }];
  271. //当前时间
  272. UILabel *currentTimeLabel =[mgr df_currentTimeLabelWithFrame:CGRectZero
  273. textColor:[UIColor hwColor:@"#999999"]
  274. textAlignment:(NSTextAlignmentCenter)
  275. font:[UIFont systemFontOfSize:14.0]
  276. superView:self.view];
  277. [currentTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  278. make.top.equalTo(curSlider.mas_top).offset(0);
  279. make.height.mas_equalTo(40);
  280. make.left.mas_equalTo(5);
  281. make.right.equalTo(curSlider.mas_left).offset(-5);
  282. }];
  283. //总时间
  284. UILabel *totalTimeLabel = [mgr df_totalTimeLabelWithFrame:CGRectZero
  285. textColor:[UIColor hwColor:@"#999999"]
  286. textAlignment:(NSTextAlignmentCenter)
  287. font:[UIFont systemFontOfSize:14.0]
  288. superView:self.view];
  289. [totalTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  290. make.top.equalTo(curSlider.mas_top).offset(0);
  291. make.height.mas_equalTo(40);
  292. make.right.mas_equalTo(-5);
  293. make.left.equalTo(curSlider.mas_right).offset(5);
  294. }];
  295. }
  296. #pragma mark 开始和暂停 按钮点击时间
  297. - (void)playOrPauseButtonDidClickFun:(UIButton*)but
  298. {
  299. but.selected = !but.selected;
  300. if(but.selected){
  301. [self startRotatingImage];
  302. [self startPlayingRotateRightImage];
  303. [[DFPlayer sharedPlayer] df_play];
  304. }
  305. else{
  306. [self stopRotatingImage];
  307. [self stopPlayingRotateRightImage];
  308. playDelayed = 0;
  309. [[DFPlayer sharedPlayer] df_pause];
  310. }
  311. }
  312. #pragma mark 按钮点击 播放模型
  313. - (void)didClickPlayModelButtonFun:(UIButton*)but
  314. {
  315. switch ([DFPlayer sharedPlayer].playMode) {
  316. case DFPlayerModeSingleCycle://单曲循环->顺序循环
  317. [DFPlayer sharedPlayer].playMode = DFPlayerModeOrderCycle;
  318. [but setImage:[UIImage imageNamed:@"dfplayer_circle"] forState:UIControlStateNormal];
  319. [[iToast makeText:NSLocalizedString(@"NAS_audio_playModel_circle",nil)] show];
  320. break;
  321. case DFPlayerModeOrderCycle://顺序循环->随机循环
  322. [DFPlayer sharedPlayer].playMode = DFPlayerModeShuffleCycle;
  323. [but setImage:[UIImage imageNamed:@"dfplayer_shuffle"] forState:UIControlStateNormal];
  324. [[iToast makeText:NSLocalizedString(@"NAS_audio_playModel_shuffle",nil)] show];
  325. break;
  326. case DFPlayerModeShuffleCycle://随机循环->单曲循环
  327. [DFPlayer sharedPlayer].playMode = DFPlayerModeSingleCycle;
  328. [but setImage:[UIImage imageNamed:@"dfplayer_single"] forState:UIControlStateNormal];
  329. [[iToast makeText:NSLocalizedString(@"NAS_audio_playModel_single",nil)] show];
  330. break;
  331. default:
  332. break;
  333. }
  334. [HWDataManager setIntegerWithKey:@"Const_audio_playMode_record" value:[DFPlayer sharedPlayer].playMode];
  335. }
  336. #pragma mark 点击下一首
  337. - (void)playNextButtonFun
  338. {
  339. [[DFPlayer sharedPlayer] df_next];
  340. }
  341. #pragma mark 点击上一首
  342. - (void)playLastButtonFun
  343. {
  344. [[DFPlayer sharedPlayer] df_last];
  345. }
  346. #pragma mark 点击右上角 更多或分享
  347. - (void)didClickHeadButtonFun:(UIButton*)but
  348. {
  349. NSInteger tag = but.tag;
  350. if(tag == 1){
  351. previewAudioPortraitTopMoreView *topMoreV = [[previewAudioPortraitTopMoreView alloc] init];
  352. [self.view addSubview:topMoreV];
  353. [topMoreV mas_makeConstraints:^(MASConstraintMaker *make) {
  354. make.top.mas_equalTo(0);
  355. make.bottom.mas_equalTo(0);
  356. make.right.mas_equalTo(0);
  357. make.left.mas_equalTo(0);
  358. }];
  359. KWeakSelf
  360. topMoreV.didClickButtonFun = ^(NSInteger tag) {
  361. switch (tag) {
  362. case 10:
  363. [weakSelf gotoDownLoadFileFun];
  364. break;
  365. case 11:
  366. [weakSelf showDeleteAlearViewFun];
  367. break;
  368. case 12:
  369. [weakSelf didClickDetailsFun];
  370. break;
  371. default:
  372. break;
  373. }
  374. };
  375. }
  376. else if (tag == 2){
  377. [self gotoShareViewFun];
  378. }
  379. }
  380. #pragma mark 用户点击分享
  381. - (void)gotoShareViewFun
  382. {
  383. editShareView *editShareV = [[editShareView alloc] init];
  384. NASFileAudioDataModel *dataModel = _allAudioDataArray[_index];
  385. editShareV.didSelectListArr = [NSMutableArray arrayWithArray:@[dataModel]];
  386. editShareV.shareFileType = @"4";
  387. [self.view addSubview:editShareV];
  388. [editShareV mas_makeConstraints:^(MASConstraintMaker *make) {
  389. make.left.mas_equalTo(0);
  390. make.right.mas_equalTo(0);
  391. make.bottom.mas_equalTo(0);
  392. make.top.mas_equalTo(0);
  393. }];
  394. }
  395. #pragma mark 用户点击下载
  396. - (void)gotoDownLoadFileFun
  397. {
  398. NASFileAudioDataModel *dataModel = _allAudioDataArray[_index];
  399. couldPhoneFileModel* fileModel = [couldPhoneFileModel new];
  400. fileModel.fileType = dataModel.type;
  401. fileModel.path = dataModel.path;
  402. fileModel.name = dataModel.name;
  403. fileModel.length = dataModel.size;
  404. NSMutableArray *arr = [NSMutableArray new];
  405. [arr addObject:fileModel];
  406. uploadFileRecordViewController *vc = [uploadFileRecordViewController new];
  407. [self.navigationController pushViewController:vc animated:YES];
  408. vc.isDownloadingType = YES;
  409. [vc gotoDownloadFile:arr];
  410. }
  411. #pragma mark 用户点击删除
  412. - (void)showDeleteAlearViewFun
  413. {
  414. NSString *titleStr = NSLocalizedString(@"delete_file_title_msg",nil);
  415. NSString *tipStr = NSLocalizedString(@"delete_file_tip_msg",nil);
  416. KWeakSelf
  417. ComontAlretViewController *curAlretVC= [[ComontAlretViewController alloc] initWithTiTle:titleStr
  418. msg:tipStr
  419. imageStr:@""
  420. cancelTitle:NSLocalizedString(@"other_cancel",nil)
  421. okTitle:NSLocalizedString(@"other_confirm",nil) isOkBtnHighlight:YES
  422. didClickOk:^{
  423. [weakSelf delFileListFun];
  424. } didClickCancel:^{
  425. }];
  426. curAlretVC.modalPresentationStyle = UIModalPresentationCustom;
  427. [self presentViewController:curAlretVC animated:YES completion:^{
  428. curAlretVC.view.superview.backgroundColor = [UIColor clearColor];
  429. }];
  430. }
  431. #pragma mark 删除文件数据
  432. - (void)delFileListFun
  433. {
  434. NSMutableDictionary*paraDict = [NSMutableDictionary new];
  435. NSMutableArray *pathArr = [NSMutableArray new];
  436. NASFileAudioDataModel *dataModel = _allAudioDataArray[_index];
  437. [pathArr addObject:dataModel.path];
  438. [paraDict setValue:pathArr forKey:@"path"];
  439. [self showNewIndicatorWithCanBack:YES canTouch:NO];
  440. //NSString*code = [[NSString alloc] initWithFormat:@"delFile?path=%@",paraDict[@"path"]]; //delFile?path=[/storage/emulated/0/Download/IMG_6464.HEIC]
  441. KWeakSelf //@"delFile"
  442. [[netWorkManager shareInstance] cloudPhonePostCallBackCode:@"delFile" Parameters:paraDict success:^(id _Nonnull responseObject) {
  443. [weakSelf removeNewIndicator];
  444. SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil];
  445. if(model && model.status == 0){
  446. [[iToast makeText:NSLocalizedString(@"delete_file_suc_msg",nil)] show];
  447. [weakSelf didDeleteSucFun:dataModel.path];
  448. }
  449. else{
  450. }
  451. } failure:^(NSError * _Nonnull error) {
  452. [weakSelf removeNewIndicator];
  453. }];
  454. //数据埋点
  455. [[netWorkManager shareInstance] DataEmbeddingPointBy:3 withEventValue:@"Music_delete"];
  456. }
  457. #pragma mark 删除成功
  458. - (void)didDeleteSucFun:(NSString*)path
  459. {
  460. //1.删除最近文件数据
  461. [[lastFileManager shareManager] deleteFileInfoWithUrl:path];
  462. [[audioPlayListManager shareManager] deleteFileInfoWithUrl:path];
  463. if(_didNeedToRegetDataFun){
  464. _didNeedToRegetDataFun();
  465. }
  466. if([audioPlayListManager shareManager].audioPlayListArr.count >= 1){
  467. _isfirstEnterType = NO;
  468. [[DFPlayer sharedPlayer] df_reloadData];
  469. if([DFPlayer sharedPlayer].playerModelArray.count > _index){
  470. [[DFPlayer sharedPlayer] df_playWithAudioId:_index];
  471. }
  472. else{
  473. [[DFPlayer sharedPlayer] df_playWithAudioId:0];
  474. }
  475. }
  476. else{
  477. [self.navigationController popViewControllerAnimated:YES];
  478. }
  479. }
  480. #pragma mark 用户点击详情
  481. - (void)didClickDetailsFun
  482. {
  483. NASFileAudioDataModel *dataModel = _allAudioDataArray[_index];
  484. previewAudioPortraitDetailsView *topDetailsV = [[previewAudioPortraitDetailsView alloc] initWithFrame:CGRectZero withData:dataModel];
  485. [self.view addSubview:topDetailsV];
  486. [topDetailsV mas_makeConstraints:^(MASConstraintMaker *make) {
  487. make.top.mas_equalTo(0);
  488. make.bottom.mas_equalTo(0);
  489. make.right.mas_equalTo(0);
  490. make.left.mas_equalTo(0);
  491. }];
  492. }
  493. #pragma mark 用户点击播放列表
  494. - (void)didClickPlayListFun
  495. {
  496. _audioPlayListV = [[audioPlayListView alloc] initWithFrame:CGRectZero withIndex:_index];
  497. [self.view addSubview:_audioPlayListV];
  498. [_audioPlayListV mas_makeConstraints:^(MASConstraintMaker *make) {
  499. make.top.mas_equalTo(0);
  500. make.bottom.mas_equalTo(0);
  501. make.right.mas_equalTo(0);
  502. make.left.mas_equalTo(0);
  503. }];
  504. KWeakSelf
  505. _audioPlayListV.didClickButtonFun = ^{
  506. [weakSelf gotoAddAudioToPlayListVCFun];
  507. };
  508. _audioPlayListV.didClickDeleteFun = ^(lastFileModel * _Nonnull dataModel) {
  509. [weakSelf deleteAudioToListBy:dataModel];
  510. };
  511. _audioPlayListV.didClickAudioFun = ^(NSInteger row) {
  512. [[DFPlayer sharedPlayer] df_playWithAudioId:row];
  513. };
  514. }
  515. #pragma mark 处理本地是否需要添加新加的的音乐
  516. - (void)deleteAudioToListBy:(lastFileModel*)dataModel
  517. {
  518. [[lastFileManager shareManager] deleteFileInfoWithUrl:dataModel.path];
  519. [[audioPlayListManager shareManager] deleteFileInfoWithUrl:dataModel.path];
  520. [[DFPlayer sharedPlayer] df_reloadData];
  521. KWeakSelf
  522. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  523. [weakSelf.audioPlayListV reGetDataFun];
  524. });
  525. }
  526. #pragma mark 跳转添加音乐到播放列表
  527. - (void)gotoAddAudioToPlayListVCFun
  528. {
  529. addAudioToPlayListViewController *vc = [addAudioToPlayListViewController new];
  530. [self.navigationController pushViewController:vc animated:YES];
  531. KWeakSelf
  532. vc.didNeedToRegetAudioPlayListFun = ^{
  533. [weakSelf AddNewAudioToListFun];
  534. };
  535. }
  536. #pragma mark 处理本地是否需要添加新加的的音乐
  537. - (void)AddNewAudioToListFun
  538. {
  539. [[DFPlayer sharedPlayer] df_reloadData];
  540. KWeakSelf
  541. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  542. [weakSelf.audioPlayListV reGetDataFun];
  543. });
  544. }
  545. #pragma mark 处理一些UI问题
  546. - (void)handleSomeUIFun
  547. {
  548. if(_allAudioDataArray.count <= 1){
  549. _playLastBtn.enabled = NO;
  550. _playNextBtn.enabled = NO;
  551. }
  552. else{
  553. _playLastBtn.enabled = YES;
  554. _playNextBtn.enabled = YES;
  555. }
  556. }
  557. #pragma mark - DFPLayer dataSource
  558. - (NSArray<DFPlayerModel *> *)df_audioDataForPlayer:(DFPlayer *)player{
  559. _dataArray = [NSMutableArray array];
  560. _allAudioDataArray = [NSMutableArray array];
  561. //把播放列表的数据同步进来
  562. NSMutableArray *localList = [audioPlayListManager shareManager].audioPlayListArr;
  563. if(localList.count > 0){
  564. for (NSInteger i = 0; i < localList.count; i++)
  565. {
  566. lastFileModel *yourModel = localList[i];
  567. DFPlayerModel *model = [[DFPlayerModel alloc] init];
  568. model.audioId = i;//****重要。AudioId从0开始,仅标识当前音频在数组中的位置。
  569. model.filePath = yourModel.path;
  570. NSString *filePath = yourModel.path;
  571. NSString *urlStr = ksharedAppDelegate.NASFileByBoxService;
  572. NSString *filePathBase64 = [iTools base64UrlEncoder:filePath];
  573. NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles/%@",urlStr,filePathBase64];
  574. NSURL * curURL = [NSURL fileURLWithPath:fileUrl];
  575. model.audioUrl = curURL;
  576. HLog(@"%@",curURL.absoluteString);
  577. [_dataArray addObject:model];
  578. [_allAudioDataArray addObject:yourModel];
  579. //20240729 修复添加音乐时显示名称不对
  580. if([DFPlayer sharedPlayer].currentAudioModel){
  581. if([[DFPlayer sharedPlayer].currentAudioModel.filePath isEqualToString:filePath]){
  582. _index = i;
  583. }
  584. }
  585. }
  586. }
  587. //首次进来 //需要判断原来的播放列表中是否存在音频
  588. if(_isfirstEnterType){
  589. //把外面传入的音频作为第一次传入
  590. lastFileModel *lastFileMod = [lastFileModel new];
  591. lastFileMod.path = _outSideDataModel.path;
  592. lastFileMod.name = _outSideDataModel.name;
  593. lastFileMod.time = _outSideDataModel.time;
  594. lastFileMod.size = _outSideDataModel.size;
  595. lastFileMod.duration = _outSideDataModel.duration;
  596. lastFileMod.type = @"audio";
  597. lastFileMod.lastPreTime = [iTools getNowTimeStamp];
  598. //本地缓存
  599. lastFileMod.localPath = [[audioPlayListManager shareManager] getAudioCacheFullPathBy:_outSideDataModel.name];
  600. DFPlayerModel *model = [[DFPlayerModel alloc] init];
  601. model.audioId = 0;//****重要。AudioId从0开始,仅标识当前音频在数组中的位置。
  602. NSString *filePath = lastFileMod.path;
  603. model.filePath = filePath;
  604. BOOL isInLocalListType = NO;
  605. for (NSInteger i = 0; i < localList.count; i++){
  606. NASFileAudioDataModel * model = _allAudioDataArray[i];
  607. if([model.path isEqualToString:filePath]){
  608. isInLocalListType = YES;
  609. _index = i;
  610. break;
  611. }
  612. }
  613. if(!isInLocalListType){
  614. NSString *urlStr = ksharedAppDelegate.NASFileByBoxService;
  615. NSString *filePathBase64 = [iTools base64UrlEncoder:filePath];
  616. NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles/%@",urlStr,filePathBase64];
  617. NSURL * curURL = [NSURL fileURLWithPath:fileUrl];
  618. model.audioUrl = curURL;
  619. HLog(@"%@",curURL.absoluteString);
  620. [_dataArray insertObject:model atIndex:0];
  621. [_allAudioDataArray insertObject:lastFileMod atIndex:0];
  622. [[audioPlayListManager shareManager] saveFileInfoWith:lastFileMod with:lastFileMod.path];
  623. _index = 0;
  624. //有数据插入 重新排序audioId
  625. for (NSInteger i = 0; i < _dataArray.count; i++) {
  626. DFPlayerModel *model = _dataArray[i];
  627. model.audioId = i;
  628. }
  629. }
  630. _isfirstEnterType = NO;
  631. }
  632. HLog(@"hxd :%@ --- %ld---index:%ld",_dataArray,_dataArray.count,_index);
  633. mainBlock(^{
  634. [self handleSomeUIFun];
  635. });
  636. return [_dataArray copy];
  637. //本地音频
  638. // NSString *path = [[NSBundle mainBundle] pathForResource:yourModel.yourUrl ofType:@""];
  639. // if (path) {
  640. // model.audioUrl = [NSURL fileURLWithPath:path];
  641. // }
  642. }
  643. - (DFPlayerInfoModel *)df_audioInfoForPlayer:(DFPlayer *)player{
  644. DFPlayerInfoModel *infoModel = [[DFPlayerInfoModel alloc] init];
  645. // infoModel.audioName = @"";//yourModel.yourName;
  646. // infoModel.audioSinger = @"";//yourModel.yourSinger;
  647. // infoModel.audioAlbum = @"";//yourModel.yourAlbum;
  648. // infoModel.audioLyrics = @"";//[NSString stringWithContentsOfFile:lyricPath encoding:NSUTF8StringEncoding error:nil];
  649. //infoModel.audioImage = [UIImage imageWithData:imageData];
  650. return infoModel;
  651. }
  652. #pragma mark - DFPlayer delegate
  653. //加入播放队列
  654. - (void)df_playerAudioAddToPlayQueue:(DFPlayer *)player{
  655. }
  656. //缓冲进度代理
  657. - (void)df_player:(DFPlayer *)player bufferProgress:(CGFloat)bufferProgress{
  658. NSInteger index = player.currentAudioModel.audioId;
  659. if(index >0 && index<_dataArray.count){
  660. lastFileModel *lastFileMod = _allAudioDataArray[index];
  661. HLog(@"缓冲进度代理:%f --- %@",bufferProgress,lastFileMod.name);
  662. }
  663. else{
  664. HLog(@"缓冲进度代理:%f",bufferProgress);
  665. }
  666. }
  667. //播放进度代理
  668. - (void)df_player:(DFPlayer *)player progress:(CGFloat)progress currentTime:(CGFloat)currentTime{
  669. NSInteger index = player.currentAudioModel.audioId;
  670. if(index >0 && index<_dataArray.count){
  671. lastFileModel *lastFileMod = _allAudioDataArray[index];
  672. //HLog(@"音频播放进度:%f --- %f --- %@ ---inddex:%ld ---- %ld",progress,currentTime,lastFileMod.name,_lastAudioIndex,index);
  673. }
  674. else{
  675. //HLog(@"音频播放进度:%f --- %f",progress,currentTime);
  676. }
  677. playDelayed ++;
  678. if(!_playPauseBtn.selected && playDelayed >= 3){
  679. _playPauseBtn.selected = YES;
  680. }
  681. //HLog(@"df_player progress hxd _ind:%ld ---- %ld",_lastAudioIndex,player.currentAudioModel.audioId);
  682. if(_lastAudioIndex != player.currentAudioModel.audioId)
  683. {
  684. _lastAudioIndex = player.currentAudioModel.audioId;
  685. _index = _lastAudioIndex;
  686. // HLog(@"df_player progress hxd _ind:setTitleFunByIndex");
  687. // [self setTitleFunByIndex];
  688. [self audioPlayerByIndex:_lastAudioIndex];
  689. [self startRotatingImage];
  690. [self startPlayingRotateRightImage];
  691. }
  692. //尝试强行修改 歌曲名 后续有时间优化
  693. _lastAudioIndex = player.currentAudioModel.audioId;
  694. _index = _lastAudioIndex;
  695. //HLog(@"df_player progress hxd _ind:setTitleFunByIndex");
  696. [self setTitleFunByIndex];
  697. if(progress >= 1.0){
  698. _lastAudioIndex = -1;
  699. [self stopRotatingImage];
  700. }
  701. }
  702. //状态信息代理
  703. - (void)df_player:(DFPlayer *)player didGetStatusCode:(DFPlayerStatusCode)statusCode{
  704. if (statusCode == DFPlayerStatusNoNetwork) {
  705. //[self showAlert:@"没有网络连接"];
  706. }else if(statusCode == DFPlayerStatusViaWWAN){
  707. // [self showAlert:@"继续播放将产生流量费用" okBlock:^{
  708. // [DFPlayer sharedPlayer].isObserveWWAN = NO;
  709. // [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  710. // }];
  711. [DFPlayer sharedPlayer].isObserveWWAN = NO;
  712. [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  713. }else if(statusCode == DFPlayerStatusTimeOut){
  714. //[self showAlert:@"请求超时"];
  715. }else if(statusCode == DFPlayerStatusCacheSucc){
  716. [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  717. }else{
  718. HLog(@"状态码:%lu",(unsigned long)statusCode);
  719. [[iToast makeText:NSLocalizedString(@"play_video_fail_tip",nil)] show];
  720. _lastAudioIndex = -1;
  721. if(_dataArray.count == 1){//只有一首的情况
  722. [self stopRotatingImage];
  723. [self stopPlayingRotateRightImage];
  724. _playPauseBtn.selected = NO;
  725. }
  726. else{
  727. [[DFPlayer sharedPlayer] df_next];
  728. }
  729. }
  730. }
  731. -(void)setTitleFunByIndex
  732. {
  733. //HLog(@"setTitleFunByIndex")
  734. if(!_allAudioDataArray){
  735. KWeakSelf
  736. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  737. [weakSelf setTitleFunByIndex];
  738. });
  739. return;
  740. }
  741. if(_index < _allAudioDataArray.count){
  742. lastFileModel* dataModel = _allAudioDataArray[_index];
  743. //self.titleLabel.text = dataModel.name;
  744. _AudioTitleLab.text = dataModel.name;
  745. if(_audioPlayListV){
  746. _audioPlayListV.playingIndex = _index;
  747. }
  748. //HLog(@"hxd _index:%ld name:%@",_index,dataModel.name)
  749. }
  750. }
  751. - (void)playAudioFun
  752. {
  753. HLog(@"playAudioFun-index:%ld",_index)
  754. if(_index < self.dataArray.count){
  755. DFPlayerModel *model = self.dataArray[_index];
  756. [[DFPlayer sharedPlayer] df_playWithAudioId:model.audioId];
  757. }
  758. }
  759. #pragma mark 音频可以播放
  760. - (void)audioPlayerByIndex:(NSInteger)index{
  761. if(index >=0 && index < _allAudioDataArray.count){
  762. NASFileAudioDataModel *dataModel = _allAudioDataArray[index];
  763. lastFileModel *lastFileMod = [lastFileModel new];
  764. lastFileMod.path = dataModel.path;
  765. lastFileMod.name = dataModel.name;
  766. lastFileMod.time = dataModel.time;
  767. lastFileMod.size = dataModel.size;
  768. lastFileMod.duration = dataModel.duration;
  769. lastFileMod.type = @"audio";
  770. lastFileMod.lastPreTime = [iTools getNowTimeStamp];
  771. //本地缓存
  772. lastFileMod.localPath = [[audioPlayListManager shareManager] getAudioCacheFullPathBy:dataModel.name];
  773. [[lastFileManager shareManager] saveFileInfoWith:lastFileMod with:dataModel.path];
  774. [[audioPlayListManager shareManager] saveFileInfoWith:lastFileMod with:dataModel.path];
  775. }
  776. }
  777. #pragma mark 图片旋转相关
  778. - (void)startRotatingImage {
  779. // 停止当前动画(如果有的话)
  780. [self stopRotatingImage];
  781. // 创建一个CABasicAnimation实例
  782. self.rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  783. // 设置动画的起始值(从0度开始)
  784. self.rotationAnimation.fromValue = [NSNumber numberWithFloat:0];
  785. // 设置动画的结束值(旋转360度,注意这里是弧度制)
  786. self.rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2];
  787. // 设置动画的持续时间
  788. self.rotationAnimation.duration = 5.0; // 可以根据需要调整旋转速度
  789. // 设置动画的重复次数,这里设置为HUGE_VALF表示无限次
  790. self.rotationAnimation.repeatCount = HUGE_VALF;
  791. // 将动画添加到imageView的layer上
  792. [self.bgImageView.layer addAnimation:self.rotationAnimation forKey:@"rotateAnimation"];
  793. }
  794. - (void)stopRotatingImage {
  795. // 移除imageView上的动画
  796. [self.bgImageView.layer removeAnimationForKey:@"rotateAnimation"];
  797. [UIView animateWithDuration:5 animations:^{
  798. self.bgImageView.layer.transform = CATransform3DIdentity;
  799. }];
  800. }
  801. - (void)restartRotatingImage {
  802. // 直接调用startRotatingImage来重新启动动画
  803. [self startRotatingImage];
  804. }
  805. - (void)startPlayingRotateRightImage
  806. {
  807. [UIView animateWithDuration:2 animations:^{
  808. self.playImageView.transform = CGAffineTransformMakeRotation(0);
  809. //self.playImageView.transform = CGAffineTransformRotate(self.playImageView.transform, -M_PI / 6); // 逆时针旋转30度
  810. }];
  811. }
  812. - (void)stopPlayingRotateRightImage
  813. {
  814. [UIView animateWithDuration:2 animations:^{
  815. self.playImageView.transform = CGAffineTransformMakeRotation(-M_PI*0.15);
  816. }];
  817. }
  818. //改变旋转中心???
  819. - (void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
  820. {
  821. CGPoint oldOrigin = view.frame.origin;
  822. view.layer.anchorPoint = anchorPoint;
  823. CGPoint newOrigin = view.frame.origin;
  824. CGPoint transition;
  825. transition.x = newOrigin.x - oldOrigin.x;
  826. transition.y = newOrigin.y - oldOrigin.y;
  827. view.center = CGPointMake (view.center.x - transition.x, view.center.y - transition.y);
  828. }
  829. @end