MusicClassVC.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //
  2. // MusicClassVC.m
  3. // YBVideo
  4. //
  5. // Created by YunBao on 2018/7/28.
  6. // Copyright © 2018年 cat. All rights reserved.
  7. //
  8. #import "MusicClassVC.h"
  9. #import "MusicModel.h"
  10. #import "MusicCell.h"
  11. #import "NSString+StringSize.h"
  12. #import "TCVideoRecordViewController.h"
  13. #import "JCHATAlertViewWait.h"
  14. #import <MediaPlayer/MediaPlayer.h>
  15. @interface MusicClassVC ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
  16. {
  17. int _paging;
  18. AVAudioPlayer *_musicPlayer; //音乐播放器
  19. CGFloat _originVolume; //记录系统音量
  20. MPVolumeView *_volumeView;
  21. UISlider *_volumeViewSlider;
  22. BOOL _changeVolume;
  23. }
  24. /** 记录 cell 是否打开 */
  25. @property (nonatomic, assign) BOOL isOpen;
  26. /** 记录当前点击的 cell 行数 */
  27. @property (nonatomic, assign) int curRow;
  28. @property(nonatomic,strong)UITableView *tableView;
  29. @property(nonatomic,strong)NSArray *models;
  30. @property(nonatomic,strong)NSMutableArray *allArray;
  31. @end
  32. @implementation MusicClassVC
  33. -(void)creatVoluem {
  34. if (_volumeViewSlider == nil) {
  35. _volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, -100, 40, 40)];
  36. [self.view addSubview:_volumeView];
  37. for (UIView* newView in _volumeView.subviews) {
  38. if ([newView.class.description isEqualToString:@"MPVolumeSlider"]){
  39. _volumeViewSlider = (UISlider*)newView;
  40. break;
  41. }
  42. }
  43. }
  44. [_volumeView setHidden:NO];
  45. [_volumeView setShowsRouteButton:YES];
  46. [_volumeView setShowsVolumeSlider:YES];
  47. }
  48. -(void)recoveryVolume{
  49. if (_changeVolume) {
  50. _changeVolume = NO;
  51. _volumeViewSlider.value = _originVolume ;
  52. }
  53. }
  54. -(void)viewWillAppear:(BOOL)animated{
  55. [super viewWillAppear:animated];
  56. [self pullData];
  57. }
  58. -(void)viewDidAppear:(BOOL)animated {
  59. [super viewDidAppear:animated];
  60. if ([PublicObj getSysOutputVolume] < 0.2) {
  61. _changeVolume = YES;
  62. _originVolume = [AVAudioSession sharedInstance].outputVolume;
  63. _volumeViewSlider.value = 0.3;
  64. }
  65. }
  66. -(void)viewWillDisappear:(BOOL)animated {
  67. [super viewWillDisappear:animated];
  68. }
  69. - (void)viewDidLoad {
  70. [super viewDidLoad];
  71. self.naviView.hidden = YES;
  72. [self creatVoluem];
  73. [self creatNavi];
  74. _paging = 1;
  75. self.curRow = -1;
  76. self.allArray = [NSMutableArray array];
  77. self.models = [NSArray array];
  78. [self.view addSubview:self.tableView];
  79. self.view.backgroundColor = UIColor.whiteColor;
  80. }
  81. -(NSArray *)models{
  82. NSMutableArray *array = [NSMutableArray array];
  83. for (NSDictionary *dic in _allArray) {
  84. MusicModel *model = [MusicModel modelWithDic:dic];
  85. [array addObject:model];
  86. }
  87. _models = array;
  88. return _models;
  89. }
  90. -(void)pullData {
  91. NSString *url = [NSString stringWithFormat:@"Music.music_list&classify=%@&uid=%@&p=%d",_class_id,[Config getOwnID],_paging];
  92. YBWeakSelf;
  93. [YBNetworking postWithUrl:url Dic:@{} Suc:^(int code, id info, NSString *msg) {
  94. [_tableView.mj_header endRefreshing];
  95. [_tableView.mj_footer endRefreshing];
  96. if (code == 0 ) {
  97. NSArray *infoA = [NSArray arrayWithArray:info];
  98. if (_paging == 1) {
  99. [_allArray removeAllObjects];
  100. }
  101. if (infoA.count <=0) {
  102. [_tableView.mj_footer endRefreshingWithNoMoreData];
  103. }else{
  104. [_allArray addObjectsFromArray:infoA];
  105. }
  106. if (_allArray.count <= 0) {
  107. [PublicView showTextNoData:weakSelf.tableView text1:YZMsg(@"") text2:YZMsg(@"去看看其他音乐类型吧") centerY:0.8];
  108. }else{
  109. [PublicView hiddenTextNoData:weakSelf.tableView];
  110. }
  111. [_tableView reloadData];
  112. }else{
  113. [PublicView showTextNoData:weakSelf.tableView text1:YZMsg(@"") text2:YZMsg(@"去看看其他音乐类型吧") centerY:0.8];
  114. [MBProgressHUD showPop:msg];
  115. }
  116. } Fail:^(id fail) {
  117. [_tableView.mj_header endRefreshing];
  118. [_tableView.mj_footer endRefreshing];
  119. }];
  120. }
  121. #pragma mark - 音乐播放、暂停
  122. -(void)playMusic:(NSString *)path currentCell:(MusicCell *)cell currentIndex:(NSIndexPath*)indexPath{
  123. NSLog(@"播放");
  124. if (self.curRow !=indexPath.row) {
  125. //点击过cell并且非同一行时候,清除掉上一个播放器
  126. [self stopMusic];
  127. }
  128. if (!_musicPlayer) {
  129. //创建音乐播放器
  130. NSError *error = nil;
  131. _musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
  132. //准备播放
  133. if ([PublicObj getSysOutputVolume]<0.2) {
  134. _musicPlayer.volume = 0.4;
  135. }
  136. [_musicPlayer prepareToPlay];
  137. //播放歌曲
  138. [_musicPlayer play];
  139. _musicPlayer.numberOfLoops = -1;
  140. [cell.StateBtn setImage:[UIImage imageNamed:@"music_pause"] forState:0];
  141. }else {
  142. if (_musicPlayer.isPlaying) {
  143. [_musicPlayer pause];
  144. [cell.StateBtn setImage:[UIImage imageNamed:@"music_play"] forState:0];
  145. }else {
  146. [_musicPlayer play];
  147. [cell.StateBtn setImage:[UIImage imageNamed:@"music_pause"] forState:0];
  148. }
  149. }
  150. }
  151. -(void)stopMusic {
  152. if (_musicPlayer) {
  153. [_musicPlayer stop];
  154. _musicPlayer = nil;
  155. }
  156. }
  157. #pragma mark - tableview UITableViewDataSource UITableViewDelegate
  158. //删除
  159. //-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
  160. //
  161. // MusicModel *model = _models[indexPath.row];
  162. // NSFileManager* fileManager=[NSFileManager defaultManager];
  163. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  164. // NSString *docDir = [paths objectAtIndex:0];
  165. // NSString *loadPath = [docDir stringByAppendingFormat:@"/*%@*%@*%@*%@.mp3",model.musicNameStr,model.singerStr,model.timeStr,model.songID];
  166. // BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:loadPath];
  167. // if (blHave) {
  168. // [fileManager removeItemAtPath:loadPath error:nil];
  169. // [self.tableView reloadData];
  170. // [MBProgressHUD showPop:@"音乐缓存清除成功"];
  171. // }else{
  172. // [MBProgressHUD showPop:@"音乐缓存为空"];
  173. // }
  174. // [self stopMusic];
  175. //
  176. //}
  177. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  178. return self.models.count;
  179. }
  180. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  181. MusicCell *cell = [MusicCell cellWithTab:tableView andIndexPath:indexPath];
  182. //10-31添加
  183. (self.curRow == (int)indexPath.row && self.isOpen) ? (cell.startRecoedBtn.hidden = NO) : (cell.startRecoedBtn.hidden = YES);
  184. cell.backgroundColor = [UIColor whiteColor];
  185. MusicModel *model = _models[indexPath.row];
  186. cell.model = model;
  187. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  188. YBWeakSelf;
  189. //回调事件处理
  190. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  191. NSString *docDir = [paths objectAtIndex:0];
  192. NSString *loadPath = [docDir stringByAppendingFormat:@"/*%@*%@*%@*%@.mp3",model.musicNameStr,model.singerStr,model.timeStr,model.songID];
  193. cell.recordEvent = ^(NSString *type) {
  194. //停止播放音乐
  195. [weakSelf stopMusic];
  196. //开拍之前()---开拍之后(编辑页面)
  197. if ([_fromWhere isEqual:@"edit"]) {
  198. [self recoveryVolume];
  199. //回调音频路径
  200. //先back 在dismis
  201. if (weakSelf.backEvent) {
  202. weakSelf.backEvent(@"分类音乐", loadPath, model.songID);
  203. }
  204. //[weakSelf.navigationController popViewControllerAnimated:NO];
  205. [weakSelf dismissViewControllerAnimated:YES completion:nil];
  206. }else{
  207. TCVideoRecordViewController *videoRecord = [TCVideoRecordViewController new];
  208. videoRecord.musicPath = loadPath;
  209. videoRecord.musicID = model.songID;
  210. videoRecord.haveBGM = YES;
  211. YBNavigationController *nav = [[YBNavigationController alloc]initWithRootViewController:videoRecord];
  212. nav.navigationBarHidden = YES;
  213. nav.modalPresentationStyle = 0;
  214. [self presentViewController:nav animated:YES completion:nil];
  215. }
  216. };
  217. __weak MusicCell *weakCell = cell;
  218. cell.rsEvent = ^(NSString *rs, NSString *erro) {
  219. [[JCHATAlertViewWait ins] hidenAll];
  220. if ([rs isEqual:@"sucess"]) {
  221. [self stopMusic];
  222. [weakSelf playMusic:loadPath currentCell:weakCell currentIndex:indexPath];
  223. }else{
  224. [MBProgressHUD showPop:erro];
  225. }
  226. };
  227. cell.selectedBackgroundView = [[UIImageView alloc]initWithImage:[PublicObj getImgWithColor:SelCell_Col]];
  228. return cell;
  229. }
  230. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  231. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  232. MusicCell *cell = (MusicCell *)[tableView cellForRowAtIndexPath:indexPath];
  233. MusicModel *model = _models[indexPath.row];
  234. if (self.curRow == (int)indexPath.row) {
  235. self.curRow = -99999;
  236. self.isOpen = NO;
  237. [self stopMusic];
  238. [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  239. return;
  240. }else{
  241. self.isOpen = YES;
  242. }
  243. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  244. NSString *docDir = [paths objectAtIndex:0];
  245. NSString *loadPath = [docDir stringByAppendingFormat:@"/*%@*%@*%@*%@.mp3",model.musicNameStr,model.singerStr,model.timeStr,model.songID];
  246. NSFileManager *manager = [NSFileManager defaultManager];
  247. if ([manager fileExistsAtPath:loadPath]) {
  248. //已下载
  249. [self playMusic:loadPath currentCell:cell currentIndex:indexPath];
  250. }else{
  251. [[JCHATAlertViewWait ins] showInView];
  252. //下载歌曲
  253. [cell musicDownLoad];
  254. }
  255. //处理显示、隐藏开拍按钮
  256. if (self.curRow == (int)indexPath.row) {
  257. return;
  258. }
  259. MusicCell *lastCell = (MusicCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.curRow inSection:0]];
  260. [lastCell.StateBtn setImage:[UIImage imageNamed:@"music_play"] forState:0];
  261. self.isOpen = YES;
  262. self.curRow = (int)indexPath.row;
  263. /** 刷新tableView,系统默认会有一个自带的动画 */
  264. [tableView beginUpdates];
  265. //10-31添加
  266. lastCell.startRecoedBtn.hidden = YES;
  267. cell.startRecoedBtn.hidden = NO;
  268. //(self.curRow == (int)indexPath.row && self.isOpen) ? (cell.startRecoedBtn.hidden = NO) : (cell.startRecoedBtn.hidden = YES);
  269. [tableView endUpdates];
  270. }
  271. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  272. //普通cell 80 播放音乐+50
  273. return (self.curRow == (int)indexPath.row && self.isOpen) ? 130 : 80;
  274. }
  275. - (void)didReceiveMemoryWarning {
  276. [super didReceiveMemoryWarning];
  277. }
  278. #pragma mark - set/get
  279. - (UITableView *)tableView {
  280. if (!_tableView) {
  281. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,64+statusbarHeight, _window_width, _window_height-64-statusbarHeight-ShowDiff) style:UITableViewStylePlain];
  282. _tableView.dataSource = self;
  283. _tableView.delegate = self;
  284. _tableView.backgroundColor = [UIColor whiteColor];
  285. _tableView.separatorStyle = UITableViewCellAccessoryNone;
  286. _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  287. _paging = 1;
  288. [self pullData];
  289. }];
  290. _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  291. _paging +=1;
  292. [self pullData];
  293. }];
  294. }
  295. return _tableView;
  296. }
  297. #pragma mark - 导航
  298. -(void)creatNavi {
  299. UIView *navi = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, 64+statusbarHeight)];
  300. navi.backgroundColor = Normal_Color;
  301. [self.view addSubview:navi];
  302. UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, statusbarHeight+20,_window_width ,44)];
  303. bgView.backgroundColor = [UIColor whiteColor];
  304. [navi addSubview:bgView];
  305. UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerTopLeft cornerRadii:CGSizeMake(5, 5)];
  306. CAShapeLayer * maskLayer = [[CAShapeLayer alloc]init];
  307. maskLayer.frame = bgView.bounds;
  308. maskLayer.path = maskPath.CGPath;
  309. bgView.layer.mask = maskLayer;
  310. UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  311. [leftBtn setImage:[UIImage imageNamed:@"gray_back"] forState:0];
  312. leftBtn.frame = CGRectMake(0, 12+statusbarHeight, 50, 50);
  313. leftBtn.imageEdgeInsets = UIEdgeInsetsMake(20, 16, 12, 16);
  314. [leftBtn addTarget:self action:@selector(clickBackBtn) forControlEvents:UIControlEventTouchUpInside];
  315. [navi addSubview:leftBtn];
  316. CGFloat top_space = 22+statusbarHeight;
  317. if (IS_IPHONE_X) {
  318. top_space = 18+statusbarHeight;
  319. }
  320. UILabel *midL = [[UILabel alloc]initWithFrame:CGRectMake(50, top_space, _window_width-100, 40)];
  321. midL.text = _navi_title;
  322. midL.textColor = [UIColor blackColor];
  323. midL.font = SYS_Font(17);
  324. midL.textAlignment = NSTextAlignmentCenter;
  325. [navi addSubview:midL];
  326. }
  327. -(void)clickBackBtn {
  328. [self recoveryVolume];
  329. //停止播放音乐
  330. [self stopMusic];
  331. [self.navigationController popViewControllerAnimated:YES];
  332. }
  333. @end