audioPlayerViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 "NASFileAudioModel.h"
  11. @interface audioPlayerViewController ()<DFPlayerDelegate,DFPlayerDataSource>
  12. @property (nonatomic, strong) UIImageView*bgImageView;
  13. @property (nonatomic, strong) NSMutableArray<DFPlayerModel *> *dataArray;
  14. @end
  15. @implementation audioPlayerViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. // Do any additional setup after loading the view.
  19. [self.toolBar setHidden:YES];
  20. [self.navigationBar setHidden:YES];
  21. [self.navBarBGView setHidden:NO];
  22. self.navBarBGView.backgroundColor = [UIColor whiteColor];
  23. [self.view setBackgroundColor:[UIColor whiteColor]];
  24. [self drawAnyView];
  25. }
  26. - (void)drawAnyView{
  27. _bgImageView = [UIImageView new];
  28. _bgImageView.image = [UIImage imageNamed:@"audioBgImg"];
  29. [self.view addSubview:_bgImageView];
  30. [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  31. make.top.equalTo(self.navBarBGView.mas_bottom).offset(105);
  32. make.width.mas_equalTo(256);
  33. make.height.mas_equalTo(256);
  34. make.centerX.mas_equalTo(0);
  35. }];
  36. [self initDFPlayer];
  37. }
  38. #pragma mark - 以下代码与DFPlayer库有关
  39. #pragma mark - 初始化DFPlayer
  40. - (void)initDFPlayer{
  41. [[DFPlayer sharedPlayer] df_initPlayerWithUserId:nil];
  42. [DFPlayer sharedPlayer].dataSource = self;
  43. [DFPlayer sharedPlayer].delegate = self;
  44. [DFPlayer sharedPlayer].playMode = DFPlayerModeSingleCycle;
  45. [DFPlayer sharedPlayer].isObserveWWAN = NO;
  46. // [[DFPlayer sharedPlayer] df_reloadData];//需在传入数据源后调用
  47. UIImage *nextImage = [UIImage imageNamed:@"dfplayer_next"];
  48. UIImage *lastImage = [UIImage imageNamed:@"dfplayer_last"];
  49. UIImage *playImage = [UIImage imageNamed:@"dfplayer_play"];
  50. UIImage *pauseImage = [UIImage imageNamed:@"dfplayer_pause"];
  51. // UIImage *singleImage = [UIImage imageNamed:@"dfplayer_single"];
  52. // UIImage *circleImage = [UIImage imageNamed:@"dfplayer_circle"];
  53. UIImage *ovalImage = [UIImage imageNamed:@"dfplayer_oval"];
  54. DFPlayerUIManager *mgr = [DFPlayerUIManager sharedManager];
  55. //缓冲条
  56. // [mgr df_bufferViewWithFrame:CGRectZero
  57. // trackTintColor:[[UIColor lightGrayColor] colorWithAlphaComponent:0.5]
  58. // progressTintColor:[UIColor colorWithWhite:1 alpha:0.5]
  59. // superView:self.view];
  60. //进度条
  61. UISlider * curSlider = [mgr df_sliderWithFrame:CGRectZero
  62. minimumTrackTintColor:[UIColor hwColor:@"#0CDEFD"]
  63. maximumTrackTintColor:[UIColor hwColor:@"#E3E3E3"]
  64. trackHeight:4
  65. thumbImage:[ovalImage imageByResizeToSize:(CGSize){15,14}]
  66. superView:self.view];
  67. [curSlider mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.top.equalTo(_bgImageView.mas_bottom).offset(100);
  69. make.height.mas_equalTo(40);
  70. make.left.mas_equalTo(70);
  71. make.right.mas_equalTo(-70);
  72. }];
  73. //当前时间
  74. UILabel *currentTimeLabel =[mgr df_currentTimeLabelWithFrame:CGRectZero
  75. textColor:[UIColor hwColor:@"#999999"]
  76. textAlignment:(NSTextAlignmentCenter)
  77. font:[UIFont systemFontOfSize:14.0]
  78. superView:self.view];
  79. [currentTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.top.equalTo(curSlider.mas_top).offset(0);
  81. make.height.mas_equalTo(40);
  82. make.left.mas_equalTo(5);
  83. make.right.equalTo(curSlider.mas_left).offset(-5);
  84. }];
  85. //总时间
  86. UILabel *totalTimeLabel = [mgr df_totalTimeLabelWithFrame:CGRectZero
  87. textColor:[UIColor hwColor:@"#999999"]
  88. textAlignment:(NSTextAlignmentCenter)
  89. font:[UIFont systemFontOfSize:14.0]
  90. superView:self.view];
  91. [totalTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  92. make.top.equalTo(curSlider.mas_top).offset(0);
  93. make.height.mas_equalTo(40);
  94. make.right.mas_equalTo(-5);
  95. make.left.equalTo(curSlider.mas_right).offset(5);
  96. }];
  97. // //播放模式按钮
  98. // [mgr df_typeBtnWithFrame:typeRect singleImage:singleImage circleImage:circleImage shuffleImage:shuffleImage superView:_bgView block:nil];
  99. //播放暂停按钮
  100. UIButton * playPauseBtn = [mgr df_playPauseBtnWithFrame:CGRectZero playImage:playImage pauseImage:pauseImage superView:self.view block:nil];
  101. [playPauseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  102. make.top.equalTo(curSlider.mas_bottom).offset(20);
  103. make.height.mas_equalTo(30);
  104. make.width.mas_equalTo(30);
  105. make.centerX.mas_equalTo(0);
  106. }];
  107. //下一首按钮
  108. UIButton * nextBtn = [mgr df_nextBtnWithFrame:CGRectZero image:nextImage superView:self.view block:nil];
  109. nextBtn.hidden = YES;
  110. [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  111. make.top.equalTo(curSlider.mas_bottom).offset(20);
  112. make.height.mas_equalTo(30);
  113. make.width.mas_equalTo(30);
  114. make.left.equalTo(playPauseBtn.mas_right).offset(50);
  115. }];
  116. //上一首按钮
  117. UIButton * lastBtn = [mgr df_lastBtnWithFrame:CGRectZero image:lastImage superView:self.view block:nil];
  118. lastBtn.hidden = YES;
  119. [lastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  120. make.top.equalTo(curSlider.mas_bottom).offset(20);
  121. make.height.mas_equalTo(30);
  122. make.width.mas_equalTo(30);
  123. make.right.equalTo(playPauseBtn.mas_left).offset(-50);
  124. }];
  125. }
  126. #pragma mark - DFPLayer dataSource
  127. - (NSArray<DFPlayerModel *> *)df_audioDataForPlayer:(DFPlayer *)player{
  128. _dataArray = [NSMutableArray array];
  129. for (int i = 0; i < _audioOutSidedataArray.count; i++) {
  130. if(i!=_index){
  131. continue;
  132. }
  133. NASFileAudioDataModel *yourModel = _audioOutSidedataArray[i];
  134. DFPlayerModel *model = [[DFPlayerModel alloc] init];
  135. model.audioId = i;//****重要。AudioId从0开始,仅标识当前音频在数组中的位置。
  136. NSString *filePath = yourModel.path;
  137. NSString *urlStr = ksharedAppDelegate.NASFileByBoxService;
  138. NSString *string = filePath;
  139. NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
  140. NSString *filePathBase64 = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
  141. NSData *data2 = [[NSData alloc] initWithBase64EncodedString:filePathBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
  142. NSString *string32 =[[NSString alloc] initWithData:data2 encoding:NSUTF8StringEncoding];
  143. HLog(@"hxd1111: %@",string32);
  144. //filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  145. //NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFile?path=%@",urlStr,filePath];
  146. //NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles?path=%@",urlStr,filePath];
  147. NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles/%@",urlStr,filePathBase64];
  148. HLog(@"hxd2222:%@",fileUrl);
  149. //fileUrl = @"http://transfertest.armclouding.com:10006/getFile?path=/storage/emulated/0/Download/录音大师-2024.05.22-16:57.mp3";
  150. //fileUrl = @"http://m10.music.126.net/20240527160012/d3f165dc686ac01afd4497400b2c2c58/ymusic/5353/0f0f/0358/d99739615f8e5153d77042092f07fd77.mp3";
  151. //fileUrl = @"https://www.cambridgeenglish.org/images/153149-movers-sample-listening-test-vol2.mp3";
  152. //fileUrl = @"https://www.cambridgeenglish.org/images/506891-a2-key-for-schools-listening-sample-test.mp3";
  153. //fileUrl = @"http://downsc.chinaz.net/Files/DownLoad/sound1/201906/11582.mp3";
  154. //fileUrl = @"http://downsc.chinaz.net/files/download/sound1/201206/1638.mp3";
  155. //fileUrl =@"http://192.168.11.248:9888/getFile?path=/storage/C47D-46D2/audioTest/jiajiaeeyinyue.mp3";
  156. //fileUrl =@"http://transfertest.armclouding.com:21025/getFiles/L3N0b3JhZ2UvZW11bGF0ZWQvMC9NdXNpYy9hYmMubXAz";
  157. //fileUrl = @"http://downsc.chinaz.net/files/download/sound1/201206/1638.mp3";
  158. //fileUrl = @"http://transfertest.armclouding.com:21025/test/abc/abc.mp3";
  159. //fileUrl = @"http://transfertest.armclouding.com:21025/test/abc/obj.mp3"; //0603 16:40验证可以播放
  160. //fileUrl = @"http://file.phone.androidscloud.com:8210/document/newFile/download/1/ikIm5C0KjKNvusTF6tIH/LowLevelMultipartUpload_44933618366140107699/1.mp3";
  161. // 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";
  162. //fileUrl = @"http://transfertest.armclouding.com:21025/getFiles/L3N0b3JhZ2UvZW11bGF0ZWQvMC9NdXNpYy9vYmoubXAz";
  163. //fileUrl = @"http://transfertest.armclouding.com:21025/getFiles/L3NkY2FyZC9NdXNpYy9vYmoubXAz";
  164. // fileUrl = [fileUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  165. NSURL * curURL = [NSURL fileURLWithPath:fileUrl];
  166. HLog(@"%@",curURL.absoluteString);
  167. model.audioUrl = curURL;
  168. // if ([yourModel.yourUrl hasPrefix:@"http"]) {//网络音频
  169. // model.audioUrl = [self getAvailableURL:yourModel.yourUrl];
  170. // }else{//本地音频
  171. // NSString *path = [[NSBundle mainBundle] pathForResource:yourModel.yourUrl ofType:@""];
  172. // if (path) {
  173. // model.audioUrl = [NSURL fileURLWithPath:path];
  174. // }
  175. // }
  176. [_dataArray addObject:model];
  177. }
  178. HLog(@"%@ --- %ld",_dataArray,_dataArray.count);
  179. return [_dataArray copy];
  180. }
  181. - (DFPlayerInfoModel *)df_audioInfoForPlayer:(DFPlayer *)player{
  182. DFPlayerInfoModel *infoModel = [[DFPlayerInfoModel alloc] init];
  183. // infoModel.audioName = @"";//yourModel.yourName;
  184. // infoModel.audioSinger = @"";//yourModel.yourSinger;
  185. // infoModel.audioAlbum = @"";//yourModel.yourAlbum;
  186. // infoModel.audioLyrics = @"";//[NSString stringWithContentsOfFile:lyricPath encoding:NSUTF8StringEncoding error:nil];
  187. //infoModel.audioImage = [UIImage imageWithData:imageData];
  188. return infoModel;
  189. }
  190. #pragma mark - DFPlayer delegate
  191. //加入播放队列
  192. - (void)df_playerAudioAddToPlayQueue:(DFPlayer *)player{
  193. // [self tableViewReloadData];
  194. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  195. // dispatch_async(dispatch_get_main_queue(), ^{
  196. // self.navigationItem.title = player.currentAudioInfoModel.audioName;
  197. // self->_bgView.image = [self getBackgroundImage:player.currentAudioInfoModel.audioImage];
  198. // self->_noticeLabel.text = player.currentAudioInfoModel.audioLyrics ? @"" : @"无可用歌词";
  199. // });
  200. // });
  201. }
  202. //缓冲进度代理
  203. - (void)df_player:(DFPlayer *)player bufferProgress:(CGFloat)bufferProgress{
  204. HLog(@"缓冲进度代理");
  205. }
  206. //播放进度代理
  207. - (void)df_player:(DFPlayer *)player progress:(CGFloat)progress currentTime:(CGFloat)currentTime{
  208. HLog(@"音频播放进度代理");
  209. }
  210. //状态信息代理
  211. - (void)df_player:(DFPlayer *)player didGetStatusCode:(DFPlayerStatusCode)statusCode{
  212. if (statusCode == DFPlayerStatusNoNetwork) {
  213. //[self showAlert:@"没有网络连接"];
  214. }else if(statusCode == DFPlayerStatusViaWWAN){
  215. // [self showAlert:@"继续播放将产生流量费用" okBlock:^{
  216. // [DFPlayer sharedPlayer].isObserveWWAN = NO;
  217. // [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  218. // }];
  219. [DFPlayer sharedPlayer].isObserveWWAN = NO;
  220. [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  221. }else if(statusCode == DFPlayerStatusTimeOut){
  222. //[self showAlert:@"请求超时"];
  223. }else if(statusCode == DFPlayerStatusCacheSucc){
  224. // [self tableViewReloadData];
  225. // return;
  226. [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  227. }else{
  228. NSLog(@"状态码:%lu",(unsigned long)statusCode);
  229. }
  230. }
  231. - (void)viewDidAppear:(BOOL)animated
  232. {
  233. [super viewDidAppear:animated];
  234. [self setTitleFunByIndex];
  235. [[DFPlayer sharedPlayer] df_reloadData];//需在传入数据源后调用
  236. KWeakSelf
  237. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  238. [weakSelf playAudioFun];
  239. });
  240. }
  241. - (void)viewDidDisappear:(BOOL)animated
  242. {
  243. [super viewDidDisappear:animated];
  244. [[DFPlayer sharedPlayer] df_deallocPlayer];
  245. }
  246. -(void)setTitleFunByIndex
  247. {
  248. if(_index < _audioOutSidedataArray.count){
  249. NASFileAudioDataModel* dataModel = _audioOutSidedataArray[_index];
  250. self.titleLabel.text = dataModel.name;
  251. }
  252. }
  253. - (void)playAudioFun
  254. {
  255. if(_index < self.dataArray.count){
  256. DFPlayerModel *model = self.dataArray[_index];
  257. [[DFPlayer sharedPlayer] df_playWithAudioId:model.audioId];
  258. }
  259. }
  260. @end