audioPlayerViewController.m 37 KB

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