audioPlayerViewController.m 33 KB

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