audioPlayerViewController.m 33 KB

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