audioPlayerViewController.m 38 KB

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