audioPlayerViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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:@"#058DFB"]
  63. maximumTrackTintColor:[UIColor hwColor:@"#E3E3E3"]
  64. trackHeight:4
  65. thumbImage:[ovalImage imageByResizeToSize:(CGSize){34,34}]
  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. NASFileAudioDataModel *yourModel = _audioOutSidedataArray[i];
  129. DFPlayerModel *model = [[DFPlayerModel alloc] init];
  130. model.audioId = i;//****重要。AudioId从0开始,仅标识当前音频在数组中的位置。
  131. NSString *filePath = yourModel.path;
  132. NSString *urlStr = ksharedAppDelegate.NASFileService;
  133. NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFile?path=%@",urlStr,filePath];
  134. //fileUrl = @"http://transfertest.armclouding.com:10006/getFile?path=/storage/emulated/0/Download/录音大师-2024.05.22-16:57.mp3";
  135. //fileUrl = @"http://transfertest.armclouding.com:10010/getFile?path=/sdcard/Music/abc.mp3";
  136. //fileUrl = @"http://m10.music.126.net/20240527160012/d3f165dc686ac01afd4497400b2c2c58/ymusic/5353/0f0f/0358/d99739615f8e5153d77042092f07fd77.mp3";
  137. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  138. NSURL * curURL = [NSURL fileURLWithPath:fileUrl];
  139. HLog(@"%@",curURL.absoluteString);
  140. model.audioUrl = curURL;
  141. // if ([yourModel.yourUrl hasPrefix:@"http"]) {//网络音频
  142. // model.audioUrl = [self getAvailableURL:yourModel.yourUrl];
  143. // }else{//本地音频
  144. // NSString *path = [[NSBundle mainBundle] pathForResource:yourModel.yourUrl ofType:@""];
  145. // if (path) {
  146. // model.audioUrl = [NSURL fileURLWithPath:path];
  147. // }
  148. // }
  149. [_dataArray addObject:model];
  150. }
  151. HLog(@"%@ --- %ld",_dataArray,_dataArray.count);
  152. return [_dataArray copy];
  153. }
  154. - (DFPlayerInfoModel *)df_audioInfoForPlayer:(DFPlayer *)player{
  155. DFPlayerInfoModel *infoModel = [[DFPlayerInfoModel alloc] init];
  156. // infoModel.audioName = @"";//yourModel.yourName;
  157. // infoModel.audioSinger = @"";//yourModel.yourSinger;
  158. // infoModel.audioAlbum = @"";//yourModel.yourAlbum;
  159. // infoModel.audioLyrics = @"";//[NSString stringWithContentsOfFile:lyricPath encoding:NSUTF8StringEncoding error:nil];
  160. //infoModel.audioImage = [UIImage imageWithData:imageData];
  161. return infoModel;
  162. }
  163. #pragma mark - DFPlayer delegate
  164. //加入播放队列
  165. - (void)df_playerAudioAddToPlayQueue:(DFPlayer *)player{
  166. // [self tableViewReloadData];
  167. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  168. // dispatch_async(dispatch_get_main_queue(), ^{
  169. // self.navigationItem.title = player.currentAudioInfoModel.audioName;
  170. // self->_bgView.image = [self getBackgroundImage:player.currentAudioInfoModel.audioImage];
  171. // self->_noticeLabel.text = player.currentAudioInfoModel.audioLyrics ? @"" : @"无可用歌词";
  172. // });
  173. // });
  174. }
  175. //缓冲进度代理
  176. - (void)df_player:(DFPlayer *)player bufferProgress:(CGFloat)bufferProgress{
  177. //[self congfigCell:player.currentAudioModel.audioId
  178. //detailTextLabel:[NSString stringWithFormat:@"正在缓冲%lf",bufferProgress]];
  179. }
  180. //播放进度代理
  181. - (void)df_player:(DFPlayer *)player progress:(CGFloat)progress currentTime:(CGFloat)currentTime{
  182. // [self congfigCell:player.currentAudioModel.audioId
  183. // detailTextLabel:[NSString stringWithFormat:@"当前进度%lf--当前时间%.0f",progress,currentTime]];
  184. }
  185. //状态信息代理
  186. - (void)df_player:(DFPlayer *)player didGetStatusCode:(DFPlayerStatusCode)statusCode{
  187. if (statusCode == DFPlayerStatusNoNetwork) {
  188. //[self showAlert:@"没有网络连接"];
  189. }else if(statusCode == DFPlayerStatusViaWWAN){
  190. // [self showAlert:@"继续播放将产生流量费用" okBlock:^{
  191. // [DFPlayer sharedPlayer].isObserveWWAN = NO;
  192. // [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  193. // }];
  194. [DFPlayer sharedPlayer].isObserveWWAN = NO;
  195. [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  196. }else if(statusCode == DFPlayerStatusTimeOut){
  197. //[self showAlert:@"请求超时"];
  198. }else if(statusCode == DFPlayerStatusCacheSucc){
  199. // [self tableViewReloadData];
  200. // return;
  201. [[DFPlayer sharedPlayer] df_playWithAudioId:player.currentAudioModel.audioId];
  202. }else{
  203. NSLog(@"状态码:%lu",(unsigned long)statusCode);
  204. }
  205. }
  206. - (void)viewDidAppear:(BOOL)animated
  207. {
  208. [super viewDidAppear:animated];
  209. [self setTitleFunByIndex];
  210. [[DFPlayer sharedPlayer] df_reloadData];//需在传入数据源后调用
  211. KWeakSelf
  212. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  213. [weakSelf playAudioFun];
  214. });
  215. }
  216. -(void)setTitleFunByIndex
  217. {
  218. if(_index < _audioOutSidedataArray.count){
  219. NASFileAudioDataModel* dataModel = _audioOutSidedataArray[_index];
  220. self.titleLabel.text = dataModel.name;
  221. }
  222. }
  223. - (void)playAudioFun
  224. {
  225. if(_index < self.dataArray.count){
  226. DFPlayerModel *model = self.dataArray[_index];
  227. [[DFPlayer sharedPlayer] df_playWithAudioId:model.audioId];
  228. }
  229. }
  230. @end