audioPlayerViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 mas_makeConstraints:^(MASConstraintMaker *make) {
  110. make.top.equalTo(curSlider.mas_bottom).offset(20);
  111. make.height.mas_equalTo(30);
  112. make.width.mas_equalTo(30);
  113. make.left.equalTo(playPauseBtn.mas_right).offset(50);
  114. }];
  115. //上一首按钮
  116. UIButton * lastBtn = [mgr df_lastBtnWithFrame:CGRectZero image:lastImage superView:self.view block:nil];
  117. [lastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  118. make.top.equalTo(curSlider.mas_bottom).offset(20);
  119. make.height.mas_equalTo(30);
  120. make.width.mas_equalTo(30);
  121. make.right.equalTo(playPauseBtn.mas_left).offset(-50);
  122. }];
  123. }
  124. #pragma mark - DFPLayer dataSource
  125. - (NSArray<DFPlayerModel *> *)df_audioDataForPlayer:(DFPlayer *)player{
  126. _dataArray = [NSMutableArray array];
  127. for (int i = 0; i < _audioOutSidedataArray.count; i++) {
  128. if(i!=_index){
  129. continue;
  130. }
  131. NASFileAudioDataModel *yourModel = _audioOutSidedataArray[i];
  132. DFPlayerModel *model = [[DFPlayerModel alloc] init];
  133. model.audioId = i;//****重要。AudioId从0开始,仅标识当前音频在数组中的位置。
  134. NSString *filePath = yourModel.path;
  135. NSString *urlStr = ksharedAppDelegate.NASFileByBoxService;
  136. //filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  137. NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFile?path=%@",urlStr,filePath];
  138. HLog(@"%@",fileUrl);
  139. //fileUrl = @"http://transfertest.armclouding.com:10006/getFile?path=/storage/emulated/0/Download/录音大师-2024.05.22-16:57.mp3";
  140. //fileUrl = @"http://transfertest.armclouding.com:10010/getFile?path=/sdcard/Music/abc.mp3";
  141. //fileUrl = @"http://m10.music.126.net/20240527160012/d3f165dc686ac01afd4497400b2c2c58/ymusic/5353/0f0f/0358/d99739615f8e5153d77042092f07fd77.mp3";
  142. //fileUrl = @"https://www.cambridgeenglish.org/images/153149-movers-sample-listening-test-vol2.mp3";
  143. //fileUrl = @"https://www.cambridgeenglish.org/images/506891-a2-key-for-schools-listening-sample-test.mp3";
  144. //fileUrl = @"http://downsc.chinaz.net/Files/DownLoad/sound1/201906/11582.mp3";
  145. //fileUrl = @"http://downsc.chinaz.net/files/download/sound1/201206/1638.mp3";
  146. //fileUrl =@"http://192.168.11.248:9888/getFile?path=/storage/C47D-46D2/audioTest/jiajiaeeyinyue.mp3";
  147. //fileUrl =@"http://transfertest.armclouding.com:21025/getFiles/L3N0b3JhZ2UvZW11bGF0ZWQvMC9NdXNpYy9hYmMubXAz";
  148. fileUrl =@"http://transfertest.armclouding.com:21025/test/abc/abc.mp3";
  149. //fileUrl =@"http://transfertest.armclouding.com:21025/getFiles/L3N0b3JhZ2UvZW11bGF0ZWQvMC9NdXNpYy9hYmMubXAz";
  150. // 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";
  151. // fileUrl = [fileUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  152. NSURL * curURL = [NSURL fileURLWithPath:fileUrl];
  153. HLog(@"%@",curURL.absoluteString);
  154. model.audioUrl = curURL;
  155. // if ([yourModel.yourUrl hasPrefix:@"http"]) {//网络音频
  156. // model.audioUrl = [self getAvailableURL:yourModel.yourUrl];
  157. // }else{//本地音频
  158. // NSString *path = [[NSBundle mainBundle] pathForResource:yourModel.yourUrl ofType:@""];
  159. // if (path) {
  160. // model.audioUrl = [NSURL fileURLWithPath:path];
  161. // }
  162. // }
  163. [_dataArray addObject:model];
  164. }
  165. HLog(@"%@ --- %ld",_dataArray,_dataArray.count);
  166. return [_dataArray copy];
  167. }
  168. - (DFPlayerInfoModel *)df_audioInfoForPlayer:(DFPlayer *)player{
  169. DFPlayerInfoModel *infoModel = [[DFPlayerInfoModel alloc] init];
  170. // infoModel.audioName = @"";//yourModel.yourName;
  171. // infoModel.audioSinger = @"";//yourModel.yourSinger;
  172. // infoModel.audioAlbum = @"";//yourModel.yourAlbum;
  173. // infoModel.audioLyrics = @"";//[NSString stringWithContentsOfFile:lyricPath encoding:NSUTF8StringEncoding error:nil];
  174. //infoModel.audioImage = [UIImage imageWithData:imageData];
  175. return infoModel;
  176. }
  177. #pragma mark - DFPlayer delegate
  178. //加入播放队列
  179. - (void)df_playerAudioAddToPlayQueue:(DFPlayer *)player{
  180. // [self tableViewReloadData];
  181. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  182. // dispatch_async(dispatch_get_main_queue(), ^{
  183. // self.navigationItem.title = player.currentAudioInfoModel.audioName;
  184. // self->_bgView.image = [self getBackgroundImage:player.currentAudioInfoModel.audioImage];
  185. // self->_noticeLabel.text = player.currentAudioInfoModel.audioLyrics ? @"" : @"无可用歌词";
  186. // });
  187. // });
  188. }
  189. //缓冲进度代理
  190. - (void)df_player:(DFPlayer *)player bufferProgress:(CGFloat)bufferProgress{
  191. //[self congfigCell:player.currentAudioModel.audioId
  192. //detailTextLabel:[NSString stringWithFormat:@"正在缓冲%lf",bufferProgress]];
  193. }
  194. //播放进度代理
  195. - (void)df_player:(DFPlayer *)player progress:(CGFloat)progress currentTime:(CGFloat)currentTime{
  196. // [self congfigCell:player.currentAudioModel.audioId
  197. // detailTextLabel:[NSString stringWithFormat:@"当前进度%lf--当前时间%.0f",progress,currentTime]];
  198. }
  199. //状态信息代理
  200. - (void)df_player:(DFPlayer *)player didGetStatusCode:(DFPlayerStatusCode)statusCode{
  201. if (statusCode == DFPlayerStatusNoNetwork) {
  202. //[self showAlert:@"没有网络连接"];
  203. }else if(statusCode == DFPlayerStatusViaWWAN){
  204. // [self showAlert:@"继续播放将产生流量费用" okBlock:^{
  205. // [DFPlayer sharedPlayer].isObserveWWAN = NO;
  206. // [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  207. // }];
  208. [DFPlayer sharedPlayer].isObserveWWAN = NO;
  209. [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  210. }else if(statusCode == DFPlayerStatusTimeOut){
  211. //[self showAlert:@"请求超时"];
  212. }else if(statusCode == DFPlayerStatusCacheSucc){
  213. // [self tableViewReloadData];
  214. // return;
  215. [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  216. }else{
  217. NSLog(@"状态码:%lu",(unsigned long)statusCode);
  218. }
  219. }
  220. - (void)viewDidAppear:(BOOL)animated
  221. {
  222. [super viewDidAppear:animated];
  223. [self setTitleFunByIndex];
  224. [[DFPlayer sharedPlayer] df_reloadData];//需在传入数据源后调用
  225. KWeakSelf
  226. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  227. [weakSelf playAudioFun];
  228. });
  229. }
  230. -(void)setTitleFunByIndex
  231. {
  232. if(_index < _audioOutSidedataArray.count){
  233. NASFileAudioDataModel* dataModel = _audioOutSidedataArray[_index];
  234. self.titleLabel.text = dataModel.name;
  235. }
  236. }
  237. - (void)playAudioFun
  238. {
  239. if(_index < self.dataArray.count){
  240. DFPlayerModel *model = self.dataArray[_index];
  241. [[DFPlayer sharedPlayer] df_playWithAudioId:model.audioId];
  242. }
  243. }
  244. @end