audioPlayerViewController.m 36 KB

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