audioPlayerViewController.m 36 KB

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