audioPlayerViewController.m 16 KB

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