audioPlayerViewController.m 15 KB

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