// // audioPlayerViewController.m // 双子星云手机 // // Created by xd h on 2024/5/26. // #import "audioPlayerViewController.h" #import "DFPlayer.h" #import "DFPlayerUIManager.h" #import "lastFileManager.h" #import "audioPlayListManager.h" #import #import "previewAudioPortraitTopMoreView.h" #import "editShareView.h" #import "uploadFileRecordViewController.h" #import "previewAudioPortraitDetailsView.h" #import "audioPlayListView.h" #import "addAudioToPlayListViewController.h" @interface audioPlayerViewController () { NSInteger playDelayed; } @property (nonatomic, strong) UIImageView*bgImageView;//中间图片 @property (nonatomic, strong) UIImageView*playImageView;// @property (nonatomic, strong) CABasicAnimation *rotationAnimation; @property (nonatomic, strong) UILabel *AudioTitleLab;//音乐标题 @property (nonatomic, strong)UIButton * playPauseBtn; @property (nonatomic, strong)UIButton * playNextBtn; @property (nonatomic, strong)UIButton * playLastBtn; @property (nonatomic, strong)UIButton * playListBtn;//歌单 @property (nonatomic, strong)UIButton * playModelBtn;//播放模式(单机 随机 循环) @property (nonatomic, assign) NSInteger index;//当前位置 @property (nonatomic, strong) NSMutableArray * allAudioDataArray;//所有的 播放数据(lastFileModel) @property (nonatomic, strong) NSMutableArray *dataArray; @property (nonatomic, assign) NSInteger lastAudioIndex;//-1未记录 @property (nonatomic, strong) audioPlayListView *audioPlayListV; @property (nonatomic, assign) BOOL isAddListType;//添加歌曲 @end @implementation audioPlayerViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.toolBar setHidden:YES]; [self.navigationBar setHidden:YES]; [self.navBarBGView setHidden:NO]; self.navBarBGView.backgroundColor = [UIColor whiteColor]; [self.view setBackgroundColor:[UIColor whiteColor]]; [self drawAnyView]; _lastAudioIndex = -1; //数据埋点 [[netWorkManager shareInstance] DataEmbeddingPointBy:4 withEventValue:@"Music_preview"]; //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LogoutByOtherFun) name:logoutByOtherNotification object:nil]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if(_outSideDataModel && _isfirstEnterType){ [[DFPlayer sharedPlayer] df_pause]; [[DFPlayer sharedPlayer] df_reloadData];//需在传入数据源后调用 [self setTitleFunByIndex]; //begin 20240729 修复音频播放时,进入播放列表点击当前播放歌曲后,会重头播放 if([DFPlayer sharedPlayer].currentAudioModel){ NSString *filePath = _outSideDataModel.path; if([[DFPlayer sharedPlayer].currentAudioModel.filePath isEqualToString:filePath]){ [[DFPlayer sharedPlayer] df_play]; return;//不重新播放 } }//end 20240729 修复音频播放时,进入播放列表点击当前播放歌曲后,会重头播放 KWeakSelf dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf playAudioFun]; }); } else{//点击playingview 进来的 _allAudioDataArray = [audioPlayListManager shareManager].audioPlayListArr; _lastAudioIndex = -1; if(!_isAddListType){//添加歌曲进来 不用改 _index = [DFPlayer sharedPlayer].currentAudioModel.audioId; [self setTitleFunByIndex]; } _isAddListType = YES; [self handleSomeUIFun]; } if([DFPlayer sharedPlayer].state == DFPlayerStatePause){ _playPauseBtn.selected = NO; } } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; //[[DFPlayer sharedPlayer] df_deallocPlayer]; } - (void)drawAnyView{ _bgImageView = [UIImageView new]; _bgImageView.image = [UIImage imageNamed:@"audioBgImg"]; [self.view addSubview:_bgImageView]; [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.navBarBGView.mas_bottom).offset(90); make.width.mas_equalTo(200); make.height.mas_equalTo(200); make.centerX.mas_equalTo(0); }]; _playImageView = [UIImageView new]; _playImageView.image = [UIImage imageNamed:@"audioPlayImg"]; [self.view addSubview:_playImageView]; [_playImageView mas_makeConstraints:^(MASConstraintMaker *make) { //make.top.equalTo(_bgImageView.mas_top).offset(-19); make.top.equalTo(_bgImageView.mas_top).offset(-19 - 85); make.width.mas_equalTo(110); make.height.mas_equalTo(170); //make.left.equalTo(_bgImageView.mas_right).offset(-60); make.left.equalTo(_bgImageView.mas_right).offset(-60 +55); }]; [self setAnchorPoint:CGPointMake(1.0, 0.0) forView:_playImageView]; _AudioTitleLab = [[UILabel alloc] init]; _AudioTitleLab.textAlignment = NSTextAlignmentCenter; _AudioTitleLab.textColor = [UIColor blackColor]; _AudioTitleLab.font = [UIFont systemFontOfSize:18.0]; _AudioTitleLab.numberOfLines = 2; [self.view addSubview:_AudioTitleLab]; [_AudioTitleLab mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(_bgImageView.mas_bottom).offset(15); make.left.mas_equalTo(30); make.right.mas_equalTo(-30); make.height.mas_equalTo(45); }]; //底部UI背景 UIImageView* bottombgImageView = [UIImageView new]; bottombgImageView.image = [UIImage imageNamed:@"audioBottomBg"]; bottombgImageView.userInteractionEnabled = YES; [self.view addSubview:bottombgImageView]; CGFloat bottombgH = SCREEN_W*280.0/375.0; [bottombgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(0); make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.height.mas_equalTo(bottombgH); }]; [self initRightUpButtonFun]; [self initDFPlayer]; } #pragma mark 右上角按钮 - (void)initRightUpButtonFun { //更多按钮 UIButton *morenButton = [[UIButton alloc] init]; [morenButton setImage:[UIImage imageNamed:@"nas_preview_more_black"] forState:UIControlStateNormal]; morenButton.tag = 1; [morenButton addTarget:self action:@selector(didClickHeadButtonFun:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:morenButton]; [morenButton mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.titleLabel.mas_centerY).offset(0); make.right.mas_equalTo(-16); make.width.mas_equalTo(30); make.height.mas_equalTo(30); }]; UIButton *shareButton = [[UIButton alloc] init]; [shareButton setImage:[UIImage imageNamed:@"nas_preview_share_black"] forState:UIControlStateNormal]; shareButton.tag = 2; [shareButton addTarget:self action:@selector(didClickHeadButtonFun:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:shareButton]; [shareButton mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.titleLabel.mas_centerY).offset(0); make.right.equalTo(morenButton.mas_left).offset(-20); make.width.mas_equalTo(30); make.height.mas_equalTo(30); }]; } #pragma mark - 以下代码与DFPlayer库有关 #pragma mark - 初始化DFPlayer - (void)initDFPlayer{ NSInteger playMode = [HWDataManager getIntegerWithKey:@"Const_audio_playMode_record"]; [[DFPlayer sharedPlayer] df_initPlayerWithUserId:nil]; [DFPlayer sharedPlayer].dataSource = self; [DFPlayer sharedPlayer].delegate = self; if(playMode <= DFPlayerModeOnlyOnce || playMode > DFPlayerModeShuffleCycle){ [DFPlayer sharedPlayer].playMode = DFPlayerModeOrderCycle; } else{ [DFPlayer sharedPlayer].playMode = playMode; } [DFPlayer sharedPlayer].isObserveWWAN = NO; // [[DFPlayer sharedPlayer] df_reloadData];//需在传入数据源后调用 UIImage *nextImage = [UIImage imageNamed:@"dfplayer_next"]; UIImage *lastImage = [UIImage imageNamed:@"dfplayer_last"]; UIImage *playImage = [UIImage imageNamed:@"dfplayer_play"]; UIImage *pauseImage = [UIImage imageNamed:@"dfplayer_pause"]; UIImage *ovalImage = [UIImage imageNamed:@"dfplayer_oval"]; DFPlayerUIManager *mgr = [DFPlayerUIManager sharedManager]; //缓冲条 // [mgr df_bufferViewWithFrame:CGRectZero // trackTintColor:[[UIColor lightGrayColor] colorWithAlphaComponent:0.5] // progressTintColor:[UIColor colorWithWhite:1 alpha:0.5] // superView:self.view]; //播放暂停按钮 //_playPauseBtn = [mgr df_playPauseBtnWithFrame:CGRectZero playImage:playImage pauseImage:pauseImage superView:self.view block:nil]; _playPauseBtn = [[UIButton alloc] init]; [_playPauseBtn setImage:playImage forState:UIControlStateSelected]; [_playPauseBtn setImage:pauseImage forState:UIControlStateNormal]; [_playPauseBtn addTarget:self action:@selector(playOrPauseButtonDidClickFun:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_playPauseBtn]; _playPauseBtn.selected = YES; [_playPauseBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(-54); make.height.mas_equalTo(32); make.width.mas_equalTo(32); make.centerX.mas_equalTo(0); }]; //下一首按钮 //_playNextBtn = [mgr df_nextBtnWithFrame:CGRectZero image:nextImage superView:self.view block:nil]; _playNextBtn = [[UIButton alloc] init]; [_playNextBtn setImage:[UIImage imageNamed:@"dfplayer_next_none"] forState:UIControlStateDisabled]; [_playNextBtn setImage:nextImage forState:UIControlStateNormal]; [_playNextBtn addTarget:self action:@selector(playNextButtonFun) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_playNextBtn]; [_playNextBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(_playPauseBtn.mas_centerY).offset(0); make.height.mas_equalTo(32); make.width.mas_equalTo(32); make.left.equalTo(_playPauseBtn.mas_right).offset(50); }]; //上一首按钮 //_playLastBtn = [mgr df_lastBtnWithFrame:CGRectZero image:lastImage superView:self.view block:nil]; _playLastBtn = [[UIButton alloc] init]; [_playLastBtn setImage:[UIImage imageNamed:@"dfplayer_last_none"] forState:UIControlStateDisabled]; [_playLastBtn setImage:lastImage forState:UIControlStateNormal]; [_playLastBtn addTarget:self action:@selector(playLastButtonFun) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_playLastBtn]; [_playLastBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(_playPauseBtn.mas_centerY).offset(0); make.height.mas_equalTo(32); make.width.mas_equalTo(32); make.right.equalTo(_playPauseBtn.mas_left).offset(-50); }]; //歌单 _playListBtn = [[UIButton alloc] init]; [_playListBtn setImage:[UIImage imageNamed:@"dfplayer_playlist"] forState:UIControlStateNormal]; [_playListBtn addTarget:self action:@selector(didClickPlayListFun) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_playListBtn]; [_playListBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(_playPauseBtn.mas_centerY).offset(0); make.height.mas_equalTo(25); make.width.mas_equalTo(25); //make.left.equalTo(_playNextBtn.mas_right).offset(50); make.right.mas_equalTo(-20); }]; // //播放模式按钮 // [mgr df_typeBtnWithFrame:typeRect singleImage:singleImage circleImage:circleImage shuffleImage:shuffleImage superView:_bgView block:nil]; UIImage *playModelImage = [UIImage imageNamed:@"dfplayer_circle"]; if ([DFPlayer sharedPlayer].playMode == DFPlayerModeShuffleCycle){ playModelImage = [UIImage imageNamed:@"dfplayer_shuffle"]; } else if ([DFPlayer sharedPlayer].playMode == DFPlayerModeSingleCycle){ playModelImage = [UIImage imageNamed:@"dfplayer_single"]; } //播放模型 _playModelBtn = [[UIButton alloc] init]; [_playModelBtn setImage:playModelImage forState:UIControlStateNormal]; [_playModelBtn addTarget:self action:@selector(didClickPlayModelButtonFun:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_playModelBtn]; [_playModelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(_playPauseBtn.mas_centerY).offset(0); make.height.mas_equalTo(25); make.width.mas_equalTo(25); make.left.mas_equalTo(20); }]; //进度条 UISlider * curSlider = [mgr df_sliderWithFrame:CGRectZero minimumTrackTintColor:[UIColor hwColor:@"#0CDEFD"] maximumTrackTintColor:[UIColor hwColor:@"#E3E3E3"] trackHeight:4 thumbImage:[ovalImage imageByResizeToSize:(CGSize){15,14}] superView:self.view]; [curSlider mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(_playPauseBtn.mas_top).offset(-40); make.height.mas_equalTo(40); make.left.mas_equalTo(70); make.right.mas_equalTo(-70); }]; //当前时间 UILabel *currentTimeLabel =[mgr df_currentTimeLabelWithFrame:CGRectZero textColor:[UIColor hwColor:@"#999999"] textAlignment:(NSTextAlignmentCenter) font:[UIFont systemFontOfSize:14.0] superView:self.view]; [currentTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(curSlider.mas_top).offset(0); make.height.mas_equalTo(40); make.left.mas_equalTo(5); make.right.equalTo(curSlider.mas_left).offset(-5); }]; //总时间 UILabel *totalTimeLabel = [mgr df_totalTimeLabelWithFrame:CGRectZero textColor:[UIColor hwColor:@"#999999"] textAlignment:(NSTextAlignmentCenter) font:[UIFont systemFontOfSize:14.0] superView:self.view]; [totalTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(curSlider.mas_top).offset(0); make.height.mas_equalTo(40); make.right.mas_equalTo(-5); make.left.equalTo(curSlider.mas_right).offset(5); }]; //音乐播放中 点击进来 总时长没有显示问题 [mgr RefreshTotalTimeLabelFun]; } #pragma mark 开始和暂停 按钮点击时间 - (void)playOrPauseButtonDidClickFun:(UIButton*)but { but.selected = !but.selected; if(but.selected){ [self startRotatingImage]; [self startPlayingRotateRightImage]; [[DFPlayer sharedPlayer] df_play]; } else{ [self didClickPauseFun]; } } - (void)didClickPauseFun { [self stopRotatingImage]; [self stopPlayingRotateRightImage]; playDelayed = 0; [[DFPlayer sharedPlayer] df_pause]; } #pragma mark 按钮点击 播放模型 - (void)didClickPlayModelButtonFun:(UIButton*)but { switch ([DFPlayer sharedPlayer].playMode) { case DFPlayerModeSingleCycle://单曲循环->顺序循环 [DFPlayer sharedPlayer].playMode = DFPlayerModeOrderCycle; [but setImage:[UIImage imageNamed:@"dfplayer_circle"] forState:UIControlStateNormal]; [[iToast makeText:NSLocalizedString(@"NAS_audio_playModel_circle",nil)] show]; break; case DFPlayerModeOrderCycle://顺序循环->随机循环 [DFPlayer sharedPlayer].playMode = DFPlayerModeShuffleCycle; [but setImage:[UIImage imageNamed:@"dfplayer_shuffle"] forState:UIControlStateNormal]; [[iToast makeText:NSLocalizedString(@"NAS_audio_playModel_shuffle",nil)] show]; break; case DFPlayerModeShuffleCycle://随机循环->单曲循环 [DFPlayer sharedPlayer].playMode = DFPlayerModeSingleCycle; [but setImage:[UIImage imageNamed:@"dfplayer_single"] forState:UIControlStateNormal]; [[iToast makeText:NSLocalizedString(@"NAS_audio_playModel_single",nil)] show]; break; default: break; } [HWDataManager setIntegerWithKey:@"Const_audio_playMode_record" value:[DFPlayer sharedPlayer].playMode]; } #pragma mark 点击下一首 - (void)playNextButtonFun { [[DFPlayer sharedPlayer] df_next]; } #pragma mark 点击上一首 - (void)playLastButtonFun { [[DFPlayer sharedPlayer] df_last]; } #pragma mark 点击右上角 更多或分享 - (void)didClickHeadButtonFun:(UIButton*)but { NSInteger tag = but.tag; if(tag == 1){ previewAudioPortraitTopMoreView *topMoreV = [[previewAudioPortraitTopMoreView alloc] init]; [self.view addSubview:topMoreV]; [topMoreV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(0); make.bottom.mas_equalTo(0); make.right.mas_equalTo(0); make.left.mas_equalTo(0); }]; KWeakSelf topMoreV.didClickButtonFun = ^(NSInteger tag) { switch (tag) { case 10: [weakSelf gotoDownLoadFileFun]; break; case 11: [weakSelf showDeleteAlearViewFun]; break; case 12: [weakSelf didClickDetailsFun]; break; default: break; } }; } else if (tag == 2){ [self gotoShareViewFun]; } } #pragma mark 用户点击分享 - (void)gotoShareViewFun { editShareView *editShareV = [[editShareView alloc] init]; NASFileAudioDataModel *dataModel = _allAudioDataArray[_index]; editShareV.didSelectListArr = [NSMutableArray arrayWithArray:@[dataModel]]; editShareV.shareFileType = @"4"; [self.view addSubview:editShareV]; [editShareV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.bottom.mas_equalTo(0); make.top.mas_equalTo(0); }]; } #pragma mark 用户点击下载 - (void)gotoDownLoadFileFun { NASFileAudioDataModel *dataModel = _allAudioDataArray[_index]; couldPhoneFileModel* fileModel = [couldPhoneFileModel new]; fileModel.fileType = dataModel.type; fileModel.path = dataModel.path; fileModel.name = dataModel.name; fileModel.length = dataModel.size; NSMutableArray *arr = [NSMutableArray new]; [arr addObject:fileModel]; uploadFileRecordViewController *vc = [uploadFileRecordViewController new]; [self.navigationController pushViewController:vc animated:YES]; vc.isDownloadingType = YES; [vc gotoDownloadFile:arr]; } #pragma mark 用户点击删除 - (void)showDeleteAlearViewFun { NSString *titleStr = NSLocalizedString(@"delete_file_title_msg",nil); NSString *tipStr = NSLocalizedString(@"delete_file_tip_msg",nil); KWeakSelf ComontAlretViewController *curAlretVC= [[ComontAlretViewController alloc] initWithTiTle:titleStr msg:tipStr imageStr:@"" cancelTitle:NSLocalizedString(@"other_cancel",nil) okTitle:NSLocalizedString(@"other_confirm",nil) isOkBtnHighlight:YES didClickOk:^{ [weakSelf delFileListFun]; } didClickCancel:^{ }]; curAlretVC.modalPresentationStyle = UIModalPresentationCustom; [self presentViewController:curAlretVC animated:YES completion:^{ curAlretVC.view.superview.backgroundColor = [UIColor clearColor]; }]; } #pragma mark 删除文件数据 - (void)delFileListFun { NSMutableDictionary*paraDict = [NSMutableDictionary new]; NSMutableArray *pathArr = [NSMutableArray new]; NASFileAudioDataModel *dataModel = _allAudioDataArray[_index]; [pathArr addObject:dataModel.path]; [paraDict setValue:pathArr forKey:@"path"]; [self showNewIndicatorWithCanBack:YES canTouch:NO]; //NSString*code = [[NSString alloc] initWithFormat:@"delFile?path=%@",paraDict[@"path"]]; //delFile?path=[/storage/emulated/0/Download/IMG_6464.HEIC] KWeakSelf //@"delFile" [[netWorkManager shareInstance] cloudPhonePostCallBackCode:@"delFile" Parameters:paraDict success:^(id _Nonnull responseObject) { [weakSelf removeNewIndicator]; SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil]; if(model && model.status == 0){ [[iToast makeText:NSLocalizedString(@"delete_file_suc_msg",nil)] show]; [weakSelf didDeleteSucFun:dataModel.path]; } else{ } } failure:^(NSError * _Nonnull error) { [weakSelf removeNewIndicator]; }]; //数据埋点 [[netWorkManager shareInstance] DataEmbeddingPointBy:3 withEventValue:@"Music_delete"]; } #pragma mark 删除成功 - (void)didDeleteSucFun:(NSString*)path { //1.删除最近文件数据 [[lastFileManager shareManager] deleteFileInfoWithUrl:path]; [[audioPlayListManager shareManager] deleteFileInfoWithUrl:path]; if(_didNeedDeleteFile){ _didNeedDeleteFile(path); } if([audioPlayListManager shareManager].audioPlayListArr.count >= 1){ _isfirstEnterType = NO; [[DFPlayer sharedPlayer] df_reloadData]; if([DFPlayer sharedPlayer].playerModelArray.count > _index){ [[DFPlayer sharedPlayer] df_playWithAudioId:_index]; } else{ [[DFPlayer sharedPlayer] df_playWithAudioId:0]; } } else{ [self.navigationController popViewControllerAnimated:YES]; } } #pragma mark 用户点击详情 - (void)didClickDetailsFun { NASFileAudioDataModel *dataModel = _allAudioDataArray[_index]; previewAudioPortraitDetailsView *topDetailsV = [[previewAudioPortraitDetailsView alloc] initWithFrame:CGRectZero withData:dataModel]; [self.view addSubview:topDetailsV]; [topDetailsV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(0); make.bottom.mas_equalTo(0); make.right.mas_equalTo(0); make.left.mas_equalTo(0); }]; } #pragma mark 用户点击播放列表 - (void)didClickPlayListFun { _audioPlayListV = [[audioPlayListView alloc] initWithFrame:CGRectZero withIndex:_index]; [self.view addSubview:_audioPlayListV]; [_audioPlayListV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(0); make.bottom.mas_equalTo(0); make.right.mas_equalTo(0); make.left.mas_equalTo(0); }]; KWeakSelf _audioPlayListV.didClickButtonFun = ^{ [weakSelf gotoAddAudioToPlayListVCFun]; }; _audioPlayListV.didClickDeleteFun = ^(lastFileModel * _Nonnull dataModel) { [weakSelf deleteAudioToListBy:dataModel]; }; _audioPlayListV.didClickAudioFun = ^(NSInteger row) { [[DFPlayer sharedPlayer] df_playWithAudioId:row]; }; } #pragma mark 处理本地是否需要添加新加的的音乐 - (void)deleteAudioToListBy:(lastFileModel*)dataModel { [[lastFileManager shareManager] deleteFileInfoWithUrl:dataModel.path]; [[audioPlayListManager shareManager] deleteFileInfoWithUrl:dataModel.path]; [[DFPlayer sharedPlayer] df_reloadData]; KWeakSelf dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf.audioPlayListV reGetDataFun]; }); } #pragma mark 跳转添加音乐到播放列表 - (void)gotoAddAudioToPlayListVCFun { addAudioToPlayListViewController *vc = [addAudioToPlayListViewController new]; [self.navigationController pushViewController:vc animated:YES]; KWeakSelf vc.didNeedToRegetAudioPlayListFun = ^{ [weakSelf AddNewAudioToListFun]; }; } #pragma mark 处理本地是否需要添加新加的的音乐 - (void)AddNewAudioToListFun { _isAddListType = YES; [[DFPlayer sharedPlayer] df_reloadData]; KWeakSelf dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ weakSelf.audioPlayListV.playingIndex = weakSelf.index; [weakSelf.audioPlayListV reGetDataFun]; }); } #pragma mark 处理一些UI问题 - (void)handleSomeUIFun { if(_allAudioDataArray.count <= 1){ _playLastBtn.enabled = NO; _playNextBtn.enabled = NO; } else{ _playLastBtn.enabled = YES; _playNextBtn.enabled = YES; } } #pragma mark - DFPLayer dataSource - (NSArray *)df_audioDataForPlayer:(DFPlayer *)player{ _dataArray = [NSMutableArray array]; _allAudioDataArray = [NSMutableArray array]; //把播放列表的数据同步进来 NSMutableArray *localList = [audioPlayListManager shareManager].audioPlayListArr; if(localList.count > 0){ for (NSInteger i = 0; i < localList.count; i++) { lastFileModel *yourModel = localList[i]; DFPlayerModel *model = [[DFPlayerModel alloc] init]; model.audioId = i;//****重要。AudioId从0开始,仅标识当前音频在数组中的位置。 model.filePath = yourModel.path; NSString *filePath = yourModel.path; NSString *urlStr = ksharedAppDelegate.NASFileByBoxService; NSString *filePathBase64 = [iTools base64UrlEncoder:filePath]; NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles/%@",urlStr,filePathBase64]; NSURL * curURL = [NSURL fileURLWithPath:fileUrl]; model.audioUrl = curURL; HLog(@"%@",curURL.absoluteString); [_dataArray addObject:model]; [_allAudioDataArray addObject:yourModel]; //20240729 修复添加音乐时显示名称不对 if([DFPlayer sharedPlayer].currentAudioModel){ if([[DFPlayer sharedPlayer].currentAudioModel.filePath isEqualToString:filePath]){ _index = i; } } } } //首次进来 //需要判断原来的播放列表中是否存在音频 if(_isfirstEnterType){ //把外面传入的音频作为第一次传入 lastFileModel *lastFileMod = [lastFileModel new]; lastFileMod.path = _outSideDataModel.path; lastFileMod.name = _outSideDataModel.name; lastFileMod.time = _outSideDataModel.time; lastFileMod.size = _outSideDataModel.size; lastFileMod.duration = _outSideDataModel.duration; lastFileMod.type = @"audio"; lastFileMod.lastPreTime = [iTools getNowTimeStamp]; //本地缓存 lastFileMod.localPath = [[audioPlayListManager shareManager] getAudioCacheFullPathBy:_outSideDataModel.name]; DFPlayerModel *model = [[DFPlayerModel alloc] init]; model.audioId = 0;//****重要。AudioId从0开始,仅标识当前音频在数组中的位置。 NSString *filePath = lastFileMod.path; model.filePath = filePath; BOOL isInLocalListType = NO; for (NSInteger i = 0; i < localList.count; i++){ NASFileAudioDataModel * model = _allAudioDataArray[i]; if([model.path isEqualToString:filePath]){ isInLocalListType = YES; _index = i; break; } } if(!isInLocalListType){ NSString *urlStr = ksharedAppDelegate.NASFileByBoxService; NSString *filePathBase64 = [iTools base64UrlEncoder:filePath]; NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles/%@",urlStr,filePathBase64]; NSURL * curURL = [NSURL fileURLWithPath:fileUrl]; model.audioUrl = curURL; HLog(@"%@",curURL.absoluteString); [_dataArray insertObject:model atIndex:0]; [_allAudioDataArray insertObject:lastFileMod atIndex:0]; [[audioPlayListManager shareManager] saveFileInfoWith:lastFileMod with:lastFileMod.path]; _index = 0; //有数据插入 重新排序audioId for (NSInteger i = 0; i < _dataArray.count; i++) { DFPlayerModel *model = _dataArray[i]; model.audioId = i; } } _isfirstEnterType = NO; } HLog(@"hxd :%@ --- %ld---index:%ld",_dataArray,_dataArray.count,_index); mainBlock(^{ [self handleSomeUIFun]; }); [DFPlayer sharedPlayer].allAudioDataArray = [_allAudioDataArray mutableCopy]; return [_dataArray copy]; //本地音频 // NSString *path = [[NSBundle mainBundle] pathForResource:yourModel.yourUrl ofType:@""]; // if (path) { // model.audioUrl = [NSURL fileURLWithPath:path]; // } } - (DFPlayerInfoModel *)df_audioInfoForPlayer:(DFPlayer *)player{ DFPlayerInfoModel *infoModel = [[DFPlayerInfoModel alloc] init]; // infoModel.audioName = @"";//yourModel.yourName; // infoModel.audioSinger = @"";//yourModel.yourSinger; // infoModel.audioAlbum = @"";//yourModel.yourAlbum; // infoModel.audioLyrics = @"";//[NSString stringWithContentsOfFile:lyricPath encoding:NSUTF8StringEncoding error:nil]; //infoModel.audioImage = [UIImage imageWithData:imageData]; return infoModel; } #pragma mark - DFPlayer delegate //加入播放队列 - (void)df_playerAudioAddToPlayQueue:(DFPlayer *)player{ _playPauseBtn.selected = YES; NSInteger curPlayingindex = player.currentAudioModel.audioId; if(curPlayingindex >= 0) { _lastAudioIndex = curPlayingindex; _index = curPlayingindex; [self setTitleFunByIndex]; [self startRotatingImage]; [self startPlayingRotateRightImage]; } } //缓冲进度代理 - (void)df_player:(DFPlayer *)player bufferProgress:(CGFloat)bufferProgress{ NSInteger index = player.currentAudioModel.audioId; if(index >0 && index<_dataArray.count){ lastFileModel *lastFileMod = _allAudioDataArray[index]; HLog(@"缓冲进度代理:%f --- %@",bufferProgress,lastFileMod.name); } else{ HLog(@"缓冲进度代理:%f",bufferProgress); } } //播放进度代理 - (void)df_player:(DFPlayer *)player progress:(CGFloat)progress currentTime:(CGFloat)currentTime{ // NSInteger index = player.currentAudioModel.audioId; // if(index >0 && index<_dataArray.count){ // lastFileModel *lastFileMod = _allAudioDataArray[index]; // //HLog(@"音频播放进度:%f --- %f --- %@ ---inddex:%ld ---- %ld",progress,currentTime,lastFileMod.name,_lastAudioIndex,index); // } // else{ // //HLog(@"音频播放进度:%f --- %f",progress,currentTime); // } // // // playDelayed ++; // if(!_playPauseBtn.selected && playDelayed >= 3){ // _playPauseBtn.selected = YES; // } // // NSInteger curPlayingindex = player.currentAudioModel.audioId; // //HLog(@"df_player progress hxd _ind:%ld ---- %ld",_lastAudioIndex,player.currentAudioModel.audioId); // if(_lastAudioIndex != curPlayingindex // && curPlayingindex >= 0 ) // { // _lastAudioIndex = curPlayingindex; // _index = curPlayingindex; //// HLog(@"df_player progress hxd _ind:setTitleFunByIndex"); //// [self setTitleFunByIndex]; // //[self audioPlayerByIndex:_lastAudioIndex]; // // [self startRotatingImage]; // [self startPlayingRotateRightImage]; // } // // //尝试强行修改 歌曲名 后续有时间优化 // if(curPlayingindex >= 0){ // _lastAudioIndex = curPlayingindex; // _index = curPlayingindex; // //HLog(@"df_player progress hxd _ind:setTitleFunByIndex"); // [self setTitleFunByIndex]; // } // // if(progress >= 1.0){ // _lastAudioIndex = -1; // [self stopRotatingImage]; // } } //状态信息代理 - (void)df_player:(DFPlayer *)player didGetStatusCode:(DFPlayerStatusCode)statusCode{ if (statusCode == DFPlayerStatusNoNetwork) { //[self showAlert:@"没有网络连接"]; }else if(statusCode == DFPlayerStatusViaWWAN){ // [self showAlert:@"继续播放将产生流量费用" okBlock:^{ // [DFPlayer sharedPlayer].isObserveWWAN = NO; // [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId]; // }]; [DFPlayer sharedPlayer].isObserveWWAN = NO; [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId]; }else if(statusCode == DFPlayerStatusTimeOut){ //[self showAlert:@"请求超时"]; }else if(statusCode == DFPlayerStatusCacheSucc){ [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId]; }else{ HLog(@"状态码:%lu",(unsigned long)statusCode); [[iToast makeText:NSLocalizedString(@"play_video_fail_tip",nil)] show]; _lastAudioIndex = -1; if(_dataArray.count == 1){//只有一首的情况 [self stopRotatingImage]; [self stopPlayingRotateRightImage]; _playPauseBtn.selected = NO; } else{ [[DFPlayer sharedPlayer] df_next]; } } } -(void)setTitleFunByIndex { //HLog(@"setTitleFunByIndex") if(!_allAudioDataArray){ KWeakSelf dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf setTitleFunByIndex]; }); return; } if(_index < _allAudioDataArray.count){ lastFileModel* dataModel = _allAudioDataArray[_index]; //self.titleLabel.text = dataModel.name; _AudioTitleLab.text = dataModel.name; if(_audioPlayListV){ _audioPlayListV.playingIndex = _index; } //HLog(@"hxd _index:%ld name:%@",_index,dataModel.name) } } - (void)playAudioFun { HLog(@"playAudioFun-index:%ld",_index) if(_index < self.dataArray.count){ DFPlayerModel *model = self.dataArray[_index]; [[DFPlayer sharedPlayer] df_playWithAudioId:model.audioId]; } } #pragma mark 图片旋转相关 - (void)startRotatingImage { // 停止当前动画(如果有的话) [self stopRotatingImage]; // 创建一个CABasicAnimation实例 self.rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; // 设置动画的起始值(从0度开始) self.rotationAnimation.fromValue = [NSNumber numberWithFloat:0]; // 设置动画的结束值(旋转360度,注意这里是弧度制) self.rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2]; // 设置动画的持续时间 self.rotationAnimation.duration = 5.0; // 可以根据需要调整旋转速度 // 设置动画的重复次数,这里设置为HUGE_VALF表示无限次 self.rotationAnimation.repeatCount = HUGE_VALF; // 将动画添加到imageView的layer上 [self.bgImageView.layer addAnimation:self.rotationAnimation forKey:@"rotateAnimation"]; } - (void)stopRotatingImage { // 移除imageView上的动画 [self.bgImageView.layer removeAnimationForKey:@"rotateAnimation"]; [UIView animateWithDuration:5 animations:^{ self.bgImageView.layer.transform = CATransform3DIdentity; }]; } - (void)restartRotatingImage { // 直接调用startRotatingImage来重新启动动画 [self startRotatingImage]; } - (void)startPlayingRotateRightImage { [UIView animateWithDuration:2 animations:^{ self.playImageView.transform = CGAffineTransformMakeRotation(0); //self.playImageView.transform = CGAffineTransformRotate(self.playImageView.transform, -M_PI / 6); // 逆时针旋转30度 }]; } - (void)stopPlayingRotateRightImage { [UIView animateWithDuration:2 animations:^{ self.playImageView.transform = CGAffineTransformMakeRotation(-M_PI*0.15); }]; } //改变旋转中心??? - (void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view { CGPoint oldOrigin = view.frame.origin; view.layer.anchorPoint = anchorPoint; CGPoint newOrigin = view.frame.origin; CGPoint transition; transition.x = newOrigin.x - oldOrigin.x; transition.y = newOrigin.y - oldOrigin.y; view.center = CGPointMake (view.center.x - transition.x, view.center.y - transition.y); } #pragma mark 被挤下线 - (void)LogoutByOtherFun { if(_playPauseBtn.selected){ [self playOrPauseButtonDidClickFun:_playPauseBtn]; } } @end