YunMusicPlayVC.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. //
  2. // YunMusicPlayVC.m
  3. // BuguLive
  4. //
  5. // Created by 岳克奎 on 16/11/14.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "YunMusicPlayVC.h"
  9. @interface YunMusicPlayVC ()<YunMusicSoundEffectVCDelegate>
  10. @property(nonatomic, assign) NSInteger sdktype;
  11. @end
  12. @implementation YunMusicPlayVC
  13. - (void)viewDidLoad
  14. {
  15. [super viewDidLoad];
  16. _musicPlayVC_Pan_Ges = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesClick:)];
  17. [self.view addGestureRecognizer:_musicPlayVC_Pan_Ges];
  18. }
  19. - (void)panGesClick:(UIPanGestureRecognizer *)pan
  20. {
  21. if (_yunMusicPlayVCPanGesBlock)
  22. {
  23. _yunMusicPlayVCPanGesBlock(self,_musicPlayVC_Pan_Ges);
  24. }
  25. }
  26. #pragma mark 音效页面
  27. - (IBAction)soundefectSet:(UIButton *)sender
  28. {
  29. YunMusicSoundEffectVC *soundsEffect_VC = [YunMusicSoundEffectVC showYunMusicSoundEffectVCInVC:_fwTLiveController inView:_fwTLiveController.liveServiceController.liveUIViewController.liveView showFrame:CGRectMake(0,0,kScreenW, kScreenH) oldBGMValue:_recordBgmValue oldMicValue:_recordMicValue];
  30. soundsEffect_VC.delegate = self;
  31. }
  32. #pragma mark- 关闭music
  33. - (IBAction)soundEndBtnClick:(UIButton *)sender
  34. {
  35. // 结束因为播放
  36. if(_sdktype == 0)
  37. {
  38. [self.fwTLiveController.publishController.txLivePublisher stopBGM];
  39. }
  40. else
  41. {
  42. [self.fwTKsyController.agoraLinkMicStreamerController.gPUStreamerKit.bgmPlayer stopPlayBgm];
  43. }
  44. // 移除用代理 因为音效VC如果存在也要退出
  45. // 移除本页面
  46. [self removeMusicPlayVC];
  47. self.musicPlaySuperVC = nil;
  48. }
  49. #pragma mark- 暂停和播放 切换
  50. /**
  51. 暂停和播放 切换
  52. @param sender fw_relive_start:未播放 fw_relive_suspend:正在播放
  53. */
  54. - (IBAction)pauseMusic:(UIButton *)sender
  55. {
  56. _yunMusicPlaying = !_yunMusicPlaying;
  57. if (_yunMusicPlaying)
  58. {
  59. if(_sdktype == 0)
  60. {
  61. [self.fwTLiveController.publishController.txLivePublisher pauseBGM];
  62. }
  63. else
  64. {
  65. [self.fwTKsyController.agoraLinkMicStreamerController.gPUStreamerKit.bgmPlayer pauseBgm];
  66. }
  67. // 暂停状态 转为播放
  68. [_pauseMusic_Btn setImage:[UIImage imageNamed:@"fw_relive_suspend"] forState:UIControlStateNormal];
  69. // [self.fwTLiveController.publishController.txLivePublisher resumeBGM];
  70. if(_sdktype == 0)
  71. {
  72. [self.fwTLiveController.publishController.txLivePublisher resumeBGM];
  73. }
  74. else
  75. {
  76. [self.fwTKsyController.agoraLinkMicStreamerController.gPUStreamerKit.bgmPlayer resumeBgm];
  77. }
  78. if (self.recordMusicCurrentTime+200>= self.recordMusicTotalTime && self.recordMusicModel &&_yunMusicPlaying&&self.recordMusicTotalTime/1000>10)
  79. {
  80. // 需要重新播放
  81. __weak YunMusicPlayVC *weak_Self= self;
  82. [[MusicCenterManager shareManager] showMusicLRCofLRCDataStr:weak_Self.recordMusicModel.title musicNameStr:weak_Self.recordMusicModel.title musicSingerStr:weak_Self.recordMusicModel.title complete:^(BOOL finished, NSMutableArray *lrcModelMArray, NSMutableArray *lrcPointTimeStrArray)
  83. {
  84. // 歌词处理完毕 得到数据 将处理好的数据 传给lrcView,等待歌词开启的时候使用
  85. weak_Self.lrcView.lrcUpLab.text = @" ";
  86. weak_Self.lrcView.lrcDowmLab.text = @" ";
  87. weak_Self.lrcView.lrcModelMArray = lrcModelMArray;
  88. weak_Self.lrcView.lrcTimePointMArray = lrcPointTimeStrArray;
  89. // 开始播放
  90. // weak_Self.yunMusicPlaying = [weak_Self.fwTLiveController.publishController.txLivePublisher playBGM:[weak_Self getFullFilePath:weak_Self.recordMusicModel.title] withBeginNotify:^(NSInteger errCode) {
  91. //
  92. // } withProgressNotify:^(NSInteger progressMS, NSInteger durationMS) {
  93. //
  94. // // 再检修播放
  95. // weak_Self.recordMusicTotalTime = durationMS;
  96. // weak_Self.recordMusicCurrentTime = progressMS;
  97. // if (progressMS+200>=durationMS)
  98. // {
  99. // weak_Self.yunMusicPlaying = NO;
  100. // [weak_Self.pauseMusic_Btn setImage:[UIImage imageNamed:@"fw_relive_start"] forState:UIControlStateNormal];
  101. // }
  102. //
  103. // [ weak_Self.lrcView setCurrentTime:progressMS*0.001 musicTotalTime:durationMS*0.001 present:durationMS ==0 ?0:(CGFloat)progressMS/durationMS];
  104. //
  105. // }andCompleteNotify:^(NSInteger errCode) {
  106. //
  107. // }];
  108. }];
  109. }
  110. }
  111. else
  112. {
  113. if(_sdktype == 0)
  114. {
  115. [self.fwTLiveController.publishController.txLivePublisher pauseBGM];
  116. }
  117. else
  118. {
  119. [self.fwTKsyController.agoraLinkMicStreamerController.gPUStreamerKit.bgmPlayer pauseBgm];
  120. }
  121. [_pauseMusic_Btn setImage:[UIImage imageNamed:@"fw_relive_start"] forState:UIControlStateNormal];
  122. }
  123. }
  124. - (void)showYunMusicPlayInVC:(UIViewController*)vc inview:(UIView*)inview showframe:(CGRect)showframe myPlayType:(NSInteger)playType
  125. {
  126. // 1 选择music vc
  127. choseMuiscVC* chosevc = [[choseMuiscVC alloc]initWithNibName:@"choseMuiscVC" bundle:nil];
  128. // 2 nav
  129. UIViewController* nullvc = UIViewController.new;
  130. UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:chosevc];
  131. nav.navigationBarHidden = YES;
  132. nav.view.backgroundColor = [UIColor clearColor];
  133. nav.modalPresentationStyle = UIModalPresentationOverCurrentContext;
  134. self.musicPlaySuperVC = vc;
  135. // 3
  136. __block UIViewController *tmpVC = self.musicPlaySuperVC;
  137. chosevc.mitblock = ^(musiceModel* chosemusic)
  138. {
  139. [nav dismissViewControllerAnimated:YES completion:^{
  140. if(chosemusic)
  141. {
  142. // 选择了就开始放歌了
  143. [YunMusicPlayVC showYunMusicPlayInVC:tmpVC inview:inview musicFrame:showframe musicmodel:chosemusic PlayType:playType];
  144. tmpVC = nil;
  145. }
  146. }];
  147. };
  148. // 跳转到choseMusicVC
  149. [vc presentViewController:nav animated:NO completion:^{
  150. }];
  151. }
  152. #pragma mark -public methods ------------------------------------------ 公有方法区域 -----------------------------------------
  153. - (NSInteger)fileSizeAtPath:(NSString*) filePath
  154. {
  155. NSFileManager* manager = [NSFileManager defaultManager];
  156. if ([manager fileExistsAtPath:filePath])
  157. {
  158. return (NSInteger)[[manager attributesOfItemAtPath:filePath error:nil] fileSize];
  159. }
  160. return 0;
  161. }
  162. #pragma mark 加载到 直播VC上
  163. + (YunMusicPlayVC *)showYunMusicPlayInVC:(UIViewController*)vc inview:(UIView*)inview musicFrame:(CGRect)musicFrame musicmodel:(musiceModel*)musicmodel PlayType:(NSInteger)playType
  164. {
  165. if(playType == 0)
  166. {
  167. BGTLiveController *live_VC = (BGTLiveController *)vc;
  168. for (UIView *one_View in live_VC.liveServiceController.liveUIViewController.liveView.subviews)
  169. {
  170. if ([one_View.nextResponder isKindOfClass:[YunMusicPlayVC class]])
  171. {
  172. [live_VC removeChild:(YunMusicPlayVC *)one_View.nextResponder];
  173. [one_View removeFromSuperview];
  174. }
  175. }
  176. YunMusicPlayVC *music_VC = [[YunMusicPlayVC alloc]initWithNibName:@"YunMusicPlayVC" bundle:nil];
  177. music_VC.recordBgmValue = 1.0;
  178. music_VC.recordMicValue = 1.0;
  179. music_VC.sdktype = playType;
  180. MUSIC_CENTER_MANAGER.dataDelegate = music_VC;
  181. [vc addChild:music_VC];
  182. [inview addSubview:music_VC.view];
  183. music_VC.view.frame = musicFrame;
  184. music_VC.fwTLiveController =(BGTLiveController *)vc;
  185. if (music_VC.fwTLiveController.publishController.txLivePublisher)
  186. {
  187. }
  188. // 歌词处理
  189. __weak YunMusicPlayVC *weak_playVC = music_VC;
  190. music_VC.recordMusicModel = musicmodel;
  191. [[MusicCenterManager shareManager] showMusicLRCofLRCDataStr:musicmodel.title musicNameStr:musicmodel.title musicSingerStr:musicmodel.title complete:^(BOOL finished, NSMutableArray *lrcModelMArray, NSMutableArray *lrcPointTimeStrArray)
  192. {
  193. // 歌词处理完毕 得到数据 将处理好的数据 传给lrcView,等待歌词开启的时候使用
  194. weak_playVC.lrcView.lrcUpLab.text = @" ";
  195. weak_playVC.lrcView.lrcDowmLab.text = @" ";
  196. weak_playVC.lrcView.lrcModelMArray = lrcModelMArray;
  197. weak_playVC.lrcView.lrcTimePointMArray = lrcPointTimeStrArray;
  198. NSLog(@"==========filePath:%@",[music_VC getFullFilePath:musicmodel.title]);
  199. NSLog(@"eqqew ========== %ld",(long)[music_VC fileSizeAtPath:[music_VC getFullFilePath:musicmodel.title]]);
  200. // 开始播放,先结束之前播放的BGM
  201. [music_VC.fwTLiveController.publishController.txLivePublisher stopBGM];
  202. // weak_playVC.yunMusicPlaying = [music_VC.fwTLiveController.publishController.txLivePublisher playBGM:[music_VC getFullFilePath:musicmodel.title]];
  203. weak_playVC.yunMusicPlaying = [music_VC.fwTLiveController.publishController.txLivePublisher playBGM:[music_VC getFullFilePath:musicmodel.title] withBeginNotify:^(NSInteger errCode) {
  204. } withProgressNotify:^(NSInteger progressMS, NSInteger durationMS) {
  205. // 再检修播放
  206. weak_playVC.recordMusicTotalTime = durationMS;
  207. weak_playVC.recordMusicCurrentTime = progressMS;
  208. if (progressMS+200>=durationMS)
  209. {
  210. weak_playVC.yunMusicPlaying = NO;
  211. [weak_playVC.pauseMusic_Btn setImage:[UIImage imageNamed:@"fw_relive_start"] forState:UIControlStateNormal];
  212. }
  213. [weak_playVC.lrcView setCurrentTime:progressMS*0.001 musicTotalTime:durationMS*0.001 present:durationMS ==0 ?0:(CGFloat)progressMS/durationMS];
  214. }andCompleteNotify:^(NSInteger errCode) {
  215. }];
  216. NSLog(@"%d",weak_playVC.yunMusicPlaying);
  217. }];
  218. music_VC.musicName_Lab.text = [NSString stringWithFormat:@"%@(%@)",musicmodel.title,musicmodel.title];
  219. music_VC.yunMusicPlayVCPanGesBlock =^(YunMusicPlayVC* yunMusicPlayVC,UIPanGestureRecognizer *pan)
  220. {
  221. CGPoint panPoint = [pan locationInView:yunMusicPlayVC.fwTLiveController.liveServiceController.liveUIViewController.liveView];
  222. CGPoint panPoint_moveValue = [pan translationInView:yunMusicPlayVC.fwTLiveController.liveServiceController.liveUIViewController.liveView];
  223. [yunMusicPlayVC setPanGesForYunMusicVC:panPoint withModeValue:panPoint_moveValue showInVC:yunMusicPlayVC];
  224. };
  225. return music_VC;
  226. }
  227. //新加的金山音乐播放
  228. else
  229. {
  230. BGKSYLiveController *live_VC = (BGKSYLiveController *)vc;
  231. for (UIView *one_View in live_VC.liveServiceController.liveUIViewController.liveView.subviews)
  232. {
  233. if ([one_View.nextResponder isKindOfClass:[YunMusicPlayVC class]])
  234. {
  235. [live_VC removeChild:(YunMusicPlayVC *)one_View.nextResponder];
  236. [one_View removeFromSuperview];
  237. }
  238. }
  239. YunMusicPlayVC *music_VC = [[YunMusicPlayVC alloc]initWithNibName:@"YunMusicPlayVC" bundle:nil];
  240. music_VC.recordBgmValue = 1.0;
  241. music_VC.recordMicValue = 1.0;
  242. music_VC.sdktype = playType;
  243. MUSIC_CENTER_MANAGER.dataDelegate = music_VC;
  244. [vc addChild:music_VC];
  245. [inview addSubview:music_VC.view];
  246. music_VC.view.frame = musicFrame;
  247. music_VC.fwTKsyController =(BGKSYLiveController *)vc;
  248. // 歌词处理
  249. __weak YunMusicPlayVC *weak_playVC = music_VC;
  250. music_VC.recordMusicModel = musicmodel;
  251. [[MusicCenterManager shareManager] showMusicLRCofLRCDataStr:musicmodel.title musicNameStr:musicmodel.title musicSingerStr:musicmodel.title complete:^(BOOL finished, NSMutableArray *lrcModelMArray, NSMutableArray *lrcPointTimeStrArray)
  252. {
  253. // 歌词处理完毕 得到数据 将处理好的数据 传给lrcView,等待歌词开启的时候使用
  254. weak_playVC.lrcView.lrcUpLab.text = @" ";
  255. weak_playVC.lrcView.lrcDowmLab.text = @" ";
  256. weak_playVC.lrcView.lrcModelMArray = lrcModelMArray;
  257. weak_playVC.lrcView.lrcTimePointMArray = lrcPointTimeStrArray;
  258. NSLog(@"==========filePath:%@",[music_VC getFullFilePath:musicmodel.title]);
  259. NSLog(@"eqqew ========== %ld",(long)[music_VC fileSizeAtPath:[music_VC getFullFilePath:musicmodel.title]]);
  260. // 开始播放,先结束之前播放的BGM
  261. [music_VC.fwTKsyController.agoraLinkMicStreamerController.gPUStreamerKit.bgmPlayer stopPlayBgm];
  262. // [music_VC.fwTLiveController.publishController.txLivePublisher stopBGM];
  263. weak_playVC.yunMusicPlaying = [music_VC.fwTKsyController.agoraLinkMicStreamerController.gPUStreamerKit.bgmPlayer startPlayBgm:[music_VC getFullFilePath:musicmodel.title] isLoop:YES];
  264. // weak_playVC.yunMusicPlaying = [music_VC.fwTLiveController.publishController.txLivePublisher playBGM:[music_VC getFullFilePath:musicmodel.title] withBeginNotify:^(NSInteger errCode) {
  265. //
  266. // } withProgressNotify:^(NSInteger progressMS, NSInteger durationMS) {
  267. //
  268. // // 再检修播放
  269. // weak_playVC.recordMusicTotalTime = durationMS;
  270. // weak_playVC.recordMusicCurrentTime = progressMS;
  271. // if (progressMS+200>=durationMS)
  272. // {
  273. // weak_playVC.yunMusicPlaying = NO;
  274. // [weak_playVC.pauseMusic_Btn setImage:[UIImage imageNamed:@"fw_relive_start"] forState:UIControlStateNormal];
  275. // }
  276. // [weak_playVC.lrcView setCurrentTime:progressMS*0.001 musicTotalTime:durationMS*0.001 present:durationMS ==0 ?0:(CGFloat)progressMS/durationMS];
  277. //
  278. // }andCompleteNotify:^(NSInteger errCode) {
  279. //
  280. // }];
  281. }];
  282. music_VC.musicName_Lab.text = [NSString stringWithFormat:@"%@(%@)",musicmodel.title,musicmodel.title];
  283. music_VC.yunMusicPlayVCPanGesBlock =^(YunMusicPlayVC* yunMusicPlayVC,UIPanGestureRecognizer *pan)
  284. {
  285. CGPoint panPoint = [pan locationInView:yunMusicPlayVC.fwTLiveController.liveServiceController.liveUIViewController.liveView];
  286. CGPoint panPoint_moveValue = [pan translationInView:yunMusicPlayVC.fwTLiveController.liveServiceController.liveUIViewController.liveView];
  287. [yunMusicPlayVC setPanGesForYunMusicVC:panPoint withModeValue:panPoint_moveValue showInVC:yunMusicPlayVC];
  288. };
  289. return music_VC;
  290. }
  291. }
  292. #pragma mark 完整的音乐文件地址
  293. - (NSString*)getFullFilePath:(NSString *)filePathStr
  294. {
  295. NSString *str = [NSString stringWithFormat:@"/Documents/%@/%@",@"music",filePathStr];
  296. if (str && ![str isEqualToString:@""])
  297. {
  298. return [NSHomeDirectory() stringByAppendingString:str];
  299. }
  300. return NSHomeDirectory();
  301. }
  302. #pragma -custom delegate ---------------------------------------------------自定义代理
  303. - (void)changeMusicBtnOfPlayStatewWithMusicPlayState:(BOOL)musicPlaySate
  304. {
  305. _yunMusicPlaying = !musicPlaySate;
  306. [self pauseMusic:nil];
  307. }
  308. #pragma mark - 音频效果的代理/调节伴奏 bgm
  309. - (void)setBGMValue:(CGFloat )bgmValue
  310. {
  311. _recordBgmValue = bgmValue;
  312. if(_sdktype == 0)
  313. {
  314. [self.fwTLiveController.publishController.txLivePublisher setBGMVolume:bgmValue];
  315. }
  316. else
  317. {
  318. [self.fwTKsyController.agoraLinkMicStreamerController.gPUStreamerKit.bgmPlayer setBgmVolume:bgmValue];
  319. }
  320. NSLog(@" current bgmValue = %f",bgmValue);
  321. }
  322. #pragma mark -调节人声 mic
  323. - (void)setMicValue:(CGFloat )micValue
  324. {
  325. _recordMicValue = micValue;
  326. if(_sdktype == 0)
  327. {
  328. [self.fwTLiveController.publishController.txLivePublisher setMicVolume:micValue];
  329. }
  330. else
  331. {
  332. }
  333. NSLog(@" current micValue = %f",micValue);
  334. }
  335. #pragma mark - 移除 VC
  336. - (void)removeMusicPlayVC
  337. {
  338. if(_sdktype == 0)
  339. {
  340. for (UIViewController *vc in self.fwTLiveController.childViewControllers)
  341. {
  342. if ([vc isKindOfClass:[self class]])
  343. {
  344. [self.fwTLiveController removeChild:vc];
  345. [vc.view removeFromSuperview];
  346. }
  347. }
  348. }
  349. else
  350. {
  351. for (UIViewController *vc in self.fwTKsyController.childViewControllers)
  352. {
  353. if ([vc isKindOfClass:[self class]])
  354. {
  355. [self.fwTKsyController removeChild:vc];
  356. [vc.view removeFromSuperview];
  357. }
  358. }
  359. }
  360. }
  361. #pragma mark ------------处理pan移动问题
  362. /**
  363. 移动到边界处理,当移动屏幕超出 默认window(界面)时,结束手势状态下,动画设置回弹效果 确保在屏幕内部
  364. @param panPoint 点击的点
  365. @param moveVlue 实时移动的店
  366. @param yunMusicPlayVC YunMusicPlayVC
  367. */
  368. - (void)setPanGesForYunMusicVC:(CGPoint)panPoint withModeValue:(CGPoint)moveVlue showInVC:(YunMusicPlayVC *)yunMusicPlayVC
  369. {
  370. yunMusicPlayVC.view.transform = CGAffineTransformTranslate( yunMusicPlayVC.view.transform, moveVlue.x, moveVlue.y);
  371. [yunMusicPlayVC.musicPlayVC_Pan_Ges setTranslation:CGPointZero inView:yunMusicPlayVC.view];
  372. if (yunMusicPlayVC.musicPlayVC_Pan_Ges.state == UIGestureRecognizerStateBegan)
  373. {
  374. }
  375. if (yunMusicPlayVC.musicPlayVC_Pan_Ges.state == UIGestureRecognizerStateChanged)
  376. {
  377. }
  378. if (yunMusicPlayVC.musicPlayVC_Pan_Ges.state == UIGestureRecognizerStateEnded)
  379. {
  380. if(yunMusicPlayVC.view.x<0)
  381. {
  382. [UIView animateWithDuration:0.2 animations:^{
  383. yunMusicPlayVC.view.x = 0;
  384. }completion:^(BOOL finished) {
  385. }];
  386. }
  387. if (yunMusicPlayVC.view.frame.origin.y<50)
  388. {
  389. [UIView animateWithDuration:0.2 animations:^{
  390. yunMusicPlayVC.view.y = 50;
  391. }completion:^(BOOL finished) {
  392. }];
  393. }
  394. if (yunMusicPlayVC.view.frame.origin.y> kScreenH - 300)
  395. {
  396. [UIView animateWithDuration:0.2 animations:^{
  397. yunMusicPlayVC.view.y = kScreenH-300;
  398. } completion:^(BOOL finished) {
  399. }];
  400. }
  401. if (yunMusicPlayVC.view.x>kScreenW-0.4*kScreenW)
  402. {
  403. [UIView animateWithDuration:0.2 animations:^{
  404. yunMusicPlayVC.view.x = kScreenW-0.4*kScreenW;
  405. } completion:^(BOOL finished) {
  406. }];
  407. }
  408. }
  409. }
  410. - (void)setRecordMusicModel:(musiceModel *)recordMusicModel
  411. {
  412. _recordMusicModel = recordMusicModel;
  413. }
  414. - (void)didReceiveMemoryWarning
  415. {
  416. [super didReceiveMemoryWarning];
  417. }
  418. @end