// // audioPlayerViewController.m // 双子星云手机 // // Created by xd h on 2024/5/26. // #import "audioPlayerViewController.h" #import "DFPlayer.h" #import "DFPlayerUIManager.h" #import "NASFileAudioModel.h" @interface audioPlayerViewController () @property (nonatomic, assign) BOOL isOlnyOneType; @property (nonatomic, strong) UIImageView*bgImageView; @property (nonatomic, strong) NSMutableArray *dataArray; @end @implementation audioPlayerViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _isOlnyOneType = YES; [self.toolBar setHidden:YES]; [self.navigationBar setHidden:YES]; [self.navBarBGView setHidden:NO]; self.navBarBGView.backgroundColor = [UIColor whiteColor]; [self.view setBackgroundColor:[UIColor whiteColor]]; [self drawAnyView]; } - (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(105); make.width.mas_equalTo(256); make.height.mas_equalTo(256); make.centerX.mas_equalTo(0); }]; [self initDFPlayer]; } #pragma mark - 以下代码与DFPlayer库有关 #pragma mark - 初始化DFPlayer - (void)initDFPlayer{ [[DFPlayer sharedPlayer] df_initPlayerWithUserId:nil]; [DFPlayer sharedPlayer].dataSource = self; [DFPlayer sharedPlayer].delegate = self; [DFPlayer sharedPlayer].playMode = DFPlayerModeSingleCycle; [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 *singleImage = [UIImage imageNamed:@"dfplayer_single"]; // UIImage *circleImage = [UIImage imageNamed:@"dfplayer_circle"]; 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]; //进度条 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.top.equalTo(_bgImageView.mas_bottom).offset(100); 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 df_typeBtnWithFrame:typeRect singleImage:singleImage circleImage:circleImage shuffleImage:shuffleImage superView:_bgView block:nil]; //播放暂停按钮 UIButton * playPauseBtn = [mgr df_playPauseBtnWithFrame:CGRectZero playImage:playImage pauseImage:pauseImage superView:self.view block:nil]; [playPauseBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(curSlider.mas_bottom).offset(20); make.height.mas_equalTo(30); make.width.mas_equalTo(30); make.centerX.mas_equalTo(0); }]; //下一首按钮 UIButton * nextBtn = [mgr df_nextBtnWithFrame:CGRectZero image:nextImage superView:self.view block:nil]; if(_isOlnyOneType){ nextBtn.hidden = YES; } [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(curSlider.mas_bottom).offset(20); make.height.mas_equalTo(30); make.width.mas_equalTo(30); make.left.equalTo(playPauseBtn.mas_right).offset(50); }]; //上一首按钮 UIButton * lastBtn = [mgr df_lastBtnWithFrame:CGRectZero image:lastImage superView:self.view block:nil]; if(_isOlnyOneType){ lastBtn.hidden = YES; } [lastBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(curSlider.mas_bottom).offset(20); make.height.mas_equalTo(30); make.width.mas_equalTo(30); make.right.equalTo(playPauseBtn.mas_left).offset(-50); }]; } #pragma mark - DFPLayer dataSource - (NSArray *)df_audioDataForPlayer:(DFPlayer *)player{ _dataArray = [NSMutableArray array]; for (int i = 0; i < _audioOutSidedataArray.count; i++) { if(_isOlnyOneType){ if(i!=_index){ continue; } } NASFileAudioDataModel *yourModel = _audioOutSidedataArray[i]; DFPlayerModel *model = [[DFPlayerModel alloc] init]; model.audioId = i;//****重要。AudioId从0开始,仅标识当前音频在数组中的位置。 if(_isOlnyOneType){ model.audioId = 0; } NSString *filePath = yourModel.path; NSString *urlStr = ksharedAppDelegate.NASFileByBoxService; NSString *string = filePath; NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; NSString *filePathBase64 = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]; NSData *data2 = [[NSData alloc] initWithBase64EncodedString:filePathBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]; NSString *string32 =[[NSString alloc] initWithData:data2 encoding:NSUTF8StringEncoding]; HLog(@"hxd1111: %@",string32); //filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFile?path=%@",urlStr,filePath]; //NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles?path=%@",urlStr,filePath]; NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles/%@",urlStr,filePathBase64]; HLog(@"hxd2222:%@",fileUrl); //fileUrl = @"http://transfertest.armclouding.com:10006/getFile?path=/storage/emulated/0/Download/录音大师-2024.05.22-16:57.mp3"; //fileUrl = @"http://m10.music.126.net/20240527160012/d3f165dc686ac01afd4497400b2c2c58/ymusic/5353/0f0f/0358/d99739615f8e5153d77042092f07fd77.mp3"; //fileUrl = @"https://www.cambridgeenglish.org/images/153149-movers-sample-listening-test-vol2.mp3"; //fileUrl = @"https://www.cambridgeenglish.org/images/506891-a2-key-for-schools-listening-sample-test.mp3"; //fileUrl = @"http://downsc.chinaz.net/Files/DownLoad/sound1/201906/11582.mp3"; //fileUrl = @"http://downsc.chinaz.net/files/download/sound1/201206/1638.mp3"; //fileUrl =@"http://192.168.11.248:9888/getFile?path=/storage/C47D-46D2/audioTest/jiajiaeeyinyue.mp3"; //fileUrl =@"http://transfertest.armclouding.com:21025/getFiles/L3N0b3JhZ2UvZW11bGF0ZWQvMC9NdXNpYy9hYmMubXAz"; //fileUrl = @"http://downsc.chinaz.net/files/download/sound1/201206/1638.mp3"; //fileUrl = @"http://transfertest.armclouding.com:21025/test/abc/abc.mp3"; //fileUrl = @"http://transfertest.armclouding.com:21025/test/abc/obj.mp3"; //0603 16:40验证可以播放 //fileUrl = @"http://file.phone.androidscloud.com:8210/document/newFile/download/1/ikIm5C0KjKNvusTF6tIH/LowLevelMultipartUpload_44933618366140107699/1.mp3"; // fileUrl = @"http://transfertest.armclouding.com:10001/getFile?path=/storage/6C07-E638/miniType/%E9%9F%B3%E9%A2%91%E6%A0%BC%E5%BC%8Fing/jiajiaeeyinyue.mp3"; //fileUrl = @"http://transfertest.armclouding.com:21025/getFiles/L3N0b3JhZ2UvZW11bGF0ZWQvMC9NdXNpYy9vYmoubXAz"; //fileUrl = @"http://transfertest.armclouding.com:21025/getFiles/L3NkY2FyZC9NdXNpYy9vYmoubXAz"; // fileUrl = [fileUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL * curURL = [NSURL fileURLWithPath:fileUrl]; HLog(@"%@",curURL.absoluteString); model.audioUrl = curURL; // if ([yourModel.yourUrl hasPrefix:@"http"]) {//网络音频 // model.audioUrl = [self getAvailableURL:yourModel.yourUrl]; // }else{//本地音频 // NSString *path = [[NSBundle mainBundle] pathForResource:yourModel.yourUrl ofType:@""]; // if (path) { // model.audioUrl = [NSURL fileURLWithPath:path]; // } // } [_dataArray addObject:model]; } HLog(@"%@ --- %ld",_dataArray,_dataArray.count); return [_dataArray copy]; } - (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{ // [self tableViewReloadData]; // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // dispatch_async(dispatch_get_main_queue(), ^{ // self.navigationItem.title = player.currentAudioInfoModel.audioName; // self->_bgView.image = [self getBackgroundImage:player.currentAudioInfoModel.audioImage]; // self->_noticeLabel.text = player.currentAudioInfoModel.audioLyrics ? @"" : @"无可用歌词"; // }); // }); } //缓冲进度代理 - (void)df_player:(DFPlayer *)player bufferProgress:(CGFloat)bufferProgress{ HLog(@"缓冲进度代理:%f",bufferProgress); } //播放进度代理 - (void)df_player:(DFPlayer *)player progress:(CGFloat)progress currentTime:(CGFloat)currentTime{ //HLog(@"音频播放进度代理"); } //状态信息代理 - (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){ // [self tableViewReloadData]; // return; [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId]; }else{ NSLog(@"状态码:%lu",(unsigned long)statusCode); } } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self setTitleFunByIndex]; [[DFPlayer sharedPlayer] df_reloadData];//需在传入数据源后调用 KWeakSelf dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf playAudioFun]; }); } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [[DFPlayer sharedPlayer] df_deallocPlayer]; } -(void)setTitleFunByIndex { if(_index < _audioOutSidedataArray.count){ NASFileAudioDataModel* dataModel = _audioOutSidedataArray[_index]; self.titleLabel.text = dataModel.name; } } - (void)playAudioFun { if(_isOlnyOneType){ if( self.dataArray.count == 1){ DFPlayerModel *model = self.dataArray.firstObject; [[DFPlayer sharedPlayer] df_playWithAudioId:model.audioId]; } } else{ if(_index < self.dataArray.count){ DFPlayerModel *model = self.dataArray[_index]; [[DFPlayer sharedPlayer] df_playWithAudioId:model.audioId]; } } } @end