audioPlayerViewController.m 30 KB

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