audioPlayerViewController.m 37 KB

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