audioPlayerViewController.m 35 KB

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