audioPlayerViewController.m 14 KB

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