audioPlayerViewController.m 36 KB

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