audioPlayerViewController.m 32 KB

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