audioPlayerViewController.m 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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. [_audioPlayListV reGetDataFun];
  515. }
  516. #pragma mark 跳转添加音乐到播放列表
  517. - (void)gotoAddAudioToPlayListVCFun
  518. {
  519. addAudioToPlayListViewController *vc = [addAudioToPlayListViewController new];
  520. [self.navigationController pushViewController:vc animated:YES];
  521. KWeakSelf
  522. vc.didNeedToRegetAudioPlayListFun = ^{
  523. [weakSelf.audioPlayListV reGetDataFun];
  524. [weakSelf AddNewAudioToListFun];
  525. };
  526. }
  527. #pragma mark 处理本地是否需要添加新加的的音乐
  528. - (void)AddNewAudioToListFun
  529. {
  530. [[DFPlayer sharedPlayer] df_reloadData];
  531. }
  532. #pragma mark 处理一些UI问题
  533. - (void)handleSomeUIFun
  534. {
  535. if(_allAudioDataArray.count <= 1){
  536. _playLastBtn.enabled = NO;
  537. _playNextBtn.enabled = NO;
  538. }
  539. else{
  540. _playLastBtn.enabled = YES;
  541. _playNextBtn.enabled = YES;
  542. }
  543. }
  544. #pragma mark - DFPLayer dataSource
  545. - (NSArray<DFPlayerModel *> *)df_audioDataForPlayer:(DFPlayer *)player{
  546. _dataArray = [NSMutableArray array];
  547. _allAudioDataArray = [NSMutableArray array];
  548. //把播放列表的数据同步进来
  549. NSMutableArray *localList = [audioPlayListManager shareManager].audioPlayListArr;
  550. if(localList.count > 0){
  551. for (NSInteger i = 0; i < localList.count; i++)
  552. {
  553. lastFileModel *yourModel = localList[i];
  554. DFPlayerModel *model = [[DFPlayerModel alloc] init];
  555. model.audioId = i;//****重要。AudioId从0开始,仅标识当前音频在数组中的位置。
  556. NSString *filePath = yourModel.path;
  557. NSString *urlStr = ksharedAppDelegate.NASFileByBoxService;
  558. NSString *filePathBase64 = [iTools base64UrlEncoder:filePath];
  559. NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles/%@",urlStr,filePathBase64];
  560. NSURL * curURL = [NSURL fileURLWithPath:fileUrl];
  561. model.audioUrl = curURL;
  562. HLog(@"%@",curURL.absoluteString);
  563. [_dataArray addObject:model];
  564. [_allAudioDataArray addObject:yourModel];
  565. }
  566. }
  567. //首次进来 //需要判断原来的播放列表中是否存在音频
  568. if(_isfirstEnterType){
  569. //把外面传入的音频作为第一次传入
  570. lastFileModel *lastFileMod = [lastFileModel new];
  571. lastFileMod.path = _outSideDataModel.path;
  572. lastFileMod.name = _outSideDataModel.name;
  573. lastFileMod.time = _outSideDataModel.time;
  574. lastFileMod.size = _outSideDataModel.size;
  575. lastFileMod.duration = _outSideDataModel.duration;
  576. lastFileMod.type = @"audio";
  577. lastFileMod.lastPreTime = [iTools getNowTimeStamp];
  578. //本地缓存
  579. lastFileMod.localPath = [[audioPlayListManager shareManager] getAudioCacheFullPathBy:_outSideDataModel.name];
  580. DFPlayerModel *model = [[DFPlayerModel alloc] init];
  581. model.audioId = 0;//****重要。AudioId从0开始,仅标识当前音频在数组中的位置。
  582. NSString *filePath = lastFileMod.path;
  583. BOOL isInLocalListType = NO;
  584. for (NSInteger i = 0; i < localList.count; i++){
  585. NASFileAudioDataModel * model = _allAudioDataArray[i];
  586. if([model.path isEqualToString:filePath]){
  587. isInLocalListType = YES;
  588. _index = i;
  589. break;
  590. }
  591. }
  592. if(!isInLocalListType){
  593. NSString *urlStr = ksharedAppDelegate.NASFileByBoxService;
  594. NSString *filePathBase64 = [iTools base64UrlEncoder:filePath];
  595. NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles/%@",urlStr,filePathBase64];
  596. NSURL * curURL = [NSURL fileURLWithPath:fileUrl];
  597. model.audioUrl = curURL;
  598. HLog(@"%@",curURL.absoluteString);
  599. [_dataArray insertObject:model atIndex:0];
  600. [_allAudioDataArray insertObject:lastFileMod atIndex:0];
  601. [[audioPlayListManager shareManager] saveFileInfoWith:lastFileMod with:lastFileMod.path];
  602. _index = 0;
  603. //有数据插入 重新排序audioId
  604. for (NSInteger i = 0; i < _dataArray.count; i++) {
  605. DFPlayerModel *model = _dataArray[i];
  606. model.audioId = i;
  607. }
  608. }
  609. _isfirstEnterType = NO;
  610. }
  611. HLog(@"hxd :%@ --- %ld---index:%ld",_dataArray,_dataArray.count,_index);
  612. mainBlock(^{
  613. [self handleSomeUIFun];
  614. });
  615. return [_dataArray copy];
  616. //本地音频
  617. // NSString *path = [[NSBundle mainBundle] pathForResource:yourModel.yourUrl ofType:@""];
  618. // if (path) {
  619. // model.audioUrl = [NSURL fileURLWithPath:path];
  620. // }
  621. }
  622. - (DFPlayerInfoModel *)df_audioInfoForPlayer:(DFPlayer *)player{
  623. DFPlayerInfoModel *infoModel = [[DFPlayerInfoModel alloc] init];
  624. // infoModel.audioName = @"";//yourModel.yourName;
  625. // infoModel.audioSinger = @"";//yourModel.yourSinger;
  626. // infoModel.audioAlbum = @"";//yourModel.yourAlbum;
  627. // infoModel.audioLyrics = @"";//[NSString stringWithContentsOfFile:lyricPath encoding:NSUTF8StringEncoding error:nil];
  628. //infoModel.audioImage = [UIImage imageWithData:imageData];
  629. return infoModel;
  630. }
  631. #pragma mark - DFPlayer delegate
  632. //加入播放队列
  633. - (void)df_playerAudioAddToPlayQueue:(DFPlayer *)player{
  634. }
  635. //缓冲进度代理
  636. - (void)df_player:(DFPlayer *)player bufferProgress:(CGFloat)bufferProgress{
  637. NSInteger index = player.currentAudioModel.audioId;
  638. if(index >0 && index<_dataArray.count){
  639. lastFileModel *lastFileMod = _allAudioDataArray[index];
  640. HLog(@"缓冲进度代理:%f --- %@",bufferProgress,lastFileMod.name);
  641. }
  642. else{
  643. HLog(@"缓冲进度代理:%f",bufferProgress);
  644. }
  645. }
  646. //播放进度代理
  647. - (void)df_player:(DFPlayer *)player progress:(CGFloat)progress currentTime:(CGFloat)currentTime{
  648. NSInteger index = player.currentAudioModel.audioId;
  649. if(index >0 && index<_dataArray.count){
  650. lastFileModel *lastFileMod = _allAudioDataArray[index];
  651. //HLog(@"音频播放进度:%f --- %f --- %@ ---inddex:%ld ---- %ld",progress,currentTime,lastFileMod.name,_lastAudioIndex,index);
  652. }
  653. else{
  654. //HLog(@"音频播放进度:%f --- %f",progress,currentTime);
  655. }
  656. playDelayed ++;
  657. if(!_playPauseBtn.selected && playDelayed >= 3){
  658. _playPauseBtn.selected = YES;
  659. }
  660. //HLog(@"df_player progress hxd _ind:%ld ---- %ld",_lastAudioIndex,player.currentAudioModel.audioId);
  661. if(_lastAudioIndex != player.currentAudioModel.audioId)
  662. {
  663. // _lastAudioIndex = player.currentAudioModel.audioId;
  664. // _index = _lastAudioIndex;
  665. // HLog(@"df_player progress hxd _ind:setTitleFunByIndex");
  666. // [self setTitleFunByIndex];
  667. [self audioPlayerByIndex:_lastAudioIndex];
  668. [self startRotatingImage];
  669. [self startPlayingRotateRightImage];
  670. }
  671. _lastAudioIndex = player.currentAudioModel.audioId;
  672. _index = _lastAudioIndex;
  673. HLog(@"df_player progress hxd _ind:setTitleFunByIndex");
  674. [self setTitleFunByIndex];
  675. if(progress >= 1.0){
  676. _lastAudioIndex = -1;
  677. [self stopRotatingImage];
  678. }
  679. }
  680. //状态信息代理
  681. - (void)df_player:(DFPlayer *)player didGetStatusCode:(DFPlayerStatusCode)statusCode{
  682. if (statusCode == DFPlayerStatusNoNetwork) {
  683. //[self showAlert:@"没有网络连接"];
  684. }else if(statusCode == DFPlayerStatusViaWWAN){
  685. // [self showAlert:@"继续播放将产生流量费用" okBlock:^{
  686. // [DFPlayer sharedPlayer].isObserveWWAN = NO;
  687. // [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  688. // }];
  689. [DFPlayer sharedPlayer].isObserveWWAN = NO;
  690. [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  691. }else if(statusCode == DFPlayerStatusTimeOut){
  692. //[self showAlert:@"请求超时"];
  693. }else if(statusCode == DFPlayerStatusCacheSucc){
  694. [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  695. }else{
  696. HLog(@"状态码:%lu",(unsigned long)statusCode);
  697. [[iToast makeText:NSLocalizedString(@"play_video_fail_tip",nil)] show];
  698. _lastAudioIndex = -1;
  699. if(_dataArray.count == 1){//只有一首的情况
  700. [self stopRotatingImage];
  701. [self stopPlayingRotateRightImage];
  702. _playPauseBtn.selected = NO;
  703. }
  704. else{
  705. [[DFPlayer sharedPlayer] df_next];
  706. }
  707. }
  708. }
  709. -(void)setTitleFunByIndex
  710. {
  711. HLog(@"setTitleFunByIndex")
  712. if(!_allAudioDataArray){
  713. KWeakSelf
  714. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  715. [weakSelf setTitleFunByIndex];
  716. });
  717. return;
  718. }
  719. if(_index < _allAudioDataArray.count){
  720. lastFileModel* dataModel = _allAudioDataArray[_index];
  721. //self.titleLabel.text = dataModel.name;
  722. _AudioTitleLab.text = dataModel.name;
  723. if(_audioPlayListV){
  724. _audioPlayListV.playingIndex = _index;
  725. }
  726. HLog(@"hxd _index:%ld name:%@",_index,dataModel.name)
  727. }
  728. }
  729. - (void)playAudioFun
  730. {
  731. HLog(@"playAudioFun-index:%ld",_index)
  732. if(_index < self.dataArray.count){
  733. DFPlayerModel *model = self.dataArray[_index];
  734. [[DFPlayer sharedPlayer] df_playWithAudioId:model.audioId];
  735. }
  736. }
  737. #pragma mark 音频可以播放
  738. - (void)audioPlayerByIndex:(NSInteger)index{
  739. if(index >=0 && index < _allAudioDataArray.count){
  740. NASFileAudioDataModel *dataModel = _allAudioDataArray[index];
  741. lastFileModel *lastFileMod = [lastFileModel new];
  742. lastFileMod.path = dataModel.path;
  743. lastFileMod.name = dataModel.name;
  744. lastFileMod.time = dataModel.time;
  745. lastFileMod.size = dataModel.size;
  746. lastFileMod.duration = dataModel.duration;
  747. lastFileMod.type = @"audio";
  748. lastFileMod.lastPreTime = [iTools getNowTimeStamp];
  749. //本地缓存
  750. lastFileMod.localPath = [[audioPlayListManager shareManager] getAudioCacheFullPathBy:dataModel.name];
  751. [[lastFileManager shareManager] saveFileInfoWith:lastFileMod with:dataModel.path];
  752. [[audioPlayListManager shareManager] saveFileInfoWith:lastFileMod with:dataModel.path];
  753. }
  754. }
  755. #pragma mark 图片旋转相关
  756. - (void)startRotatingImage {
  757. // 停止当前动画(如果有的话)
  758. [self stopRotatingImage];
  759. // 创建一个CABasicAnimation实例
  760. self.rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  761. // 设置动画的起始值(从0度开始)
  762. self.rotationAnimation.fromValue = [NSNumber numberWithFloat:0];
  763. // 设置动画的结束值(旋转360度,注意这里是弧度制)
  764. self.rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2];
  765. // 设置动画的持续时间
  766. self.rotationAnimation.duration = 5.0; // 可以根据需要调整旋转速度
  767. // 设置动画的重复次数,这里设置为HUGE_VALF表示无限次
  768. self.rotationAnimation.repeatCount = HUGE_VALF;
  769. // 将动画添加到imageView的layer上
  770. [self.bgImageView.layer addAnimation:self.rotationAnimation forKey:@"rotateAnimation"];
  771. }
  772. - (void)stopRotatingImage {
  773. // 移除imageView上的动画
  774. [self.bgImageView.layer removeAnimationForKey:@"rotateAnimation"];
  775. [UIView animateWithDuration:5 animations:^{
  776. self.bgImageView.layer.transform = CATransform3DIdentity;
  777. }];
  778. }
  779. - (void)restartRotatingImage {
  780. // 直接调用startRotatingImage来重新启动动画
  781. [self startRotatingImage];
  782. }
  783. - (void)startPlayingRotateRightImage
  784. {
  785. [UIView animateWithDuration:2 animations:^{
  786. self.playImageView.transform = CGAffineTransformMakeRotation(0);
  787. //self.playImageView.transform = CGAffineTransformRotate(self.playImageView.transform, -M_PI / 6); // 逆时针旋转30度
  788. }];
  789. }
  790. - (void)stopPlayingRotateRightImage
  791. {
  792. [UIView animateWithDuration:2 animations:^{
  793. self.playImageView.transform = CGAffineTransformMakeRotation(-M_PI*0.15);
  794. }];
  795. }
  796. //改变旋转中心???
  797. - (void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
  798. {
  799. CGPoint oldOrigin = view.frame.origin;
  800. view.layer.anchorPoint = anchorPoint;
  801. CGPoint newOrigin = view.frame.origin;
  802. CGPoint transition;
  803. transition.x = newOrigin.x - oldOrigin.x;
  804. transition.y = newOrigin.y - oldOrigin.y;
  805. view.center = CGPointMake (view.center.x - transition.x, view.center.y - transition.y);
  806. }
  807. @end