audioPlayerViewController~.m 38 KB

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