audioPlayerViewController.m 38 KB

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