audioPlayerViewController.m 37 KB

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