New_SimultaneousmusicController.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // New_SimultaneousmusicController.m
  3. // BuguLive
  4. //
  5. // Created by bugu on 2019/5/21.
  6. // Copyright © 2019年 xfg. All rights reserved.
  7. //
  8. #import "New_SimultaneousmusicController.h"
  9. #import "new_SimultaneousheaderView.h"
  10. #import "new_SimultaneousCell.h"
  11. #import "Music_manager.h"
  12. #import "HMVideoPlayerViewController.h"
  13. #import "TCVideoRecordViewController.h"
  14. #import "TCBGMHelper.h"
  15. @interface New_SimultaneousmusicController ()<UICollectionViewDelegate,UICollectionViewDataSource,headerDelegate,TCBGMHelperListener>
  16. {
  17. BOOL iscanDosimultane;
  18. }
  19. @property (nonatomic, strong)UICollectionView *collectionview;
  20. @property (nonatomic, strong)UIButton *doSameparagraph;//拍同款
  21. @property (nonatomic, strong)NSMutableArray *dataAry;
  22. @property (nonatomic, assign)int page;
  23. @property(nonatomic) TCBGMHelper* bgmHelper;
  24. @end
  25. @implementation New_SimultaneousmusicController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. _page =1;
  29. [self getdataWith:_page];
  30. _bgmHelper = [TCBGMHelper sharedInstance];
  31. [_bgmHelper setDelegate:self];
  32. // self.navtitle =@"";
  33. // self.needLeft =YES;
  34. // Do any additional setup after loading the view.
  35. }
  36. - (void)dosameAction
  37. {
  38. //拍同款
  39. if (iscanDosimultane)
  40. {
  41. TCVideoRecordViewController *vc = [[TCVideoRecordViewController alloc] init];
  42. vc.savePath = YES;
  43. vc.musicPatch = _model.localPatch;
  44. vc.model =_model;
  45. [[AppDelegate sharedAppDelegate] pushViewController:vc];
  46. }else{
  47. // [FanweMessage alertHUD:@"音乐不存在"];
  48. }
  49. }
  50. - (void)setModel:(music_obj *)model
  51. {
  52. _model =model;
  53. [self.collectionview.mj_header beginRefreshing];
  54. [self.doSameparagraph addTarget:self action:@selector(dosameAction) forControlEvents:UIControlEventTouchUpInside];
  55. TCBGMElement* ele = [TCBGMElement new];
  56. ele.name =model.music_name;
  57. ele.netUrl =model.music_url;
  58. ele.author =model.music_author;
  59. NSString *localpatch;
  60. if ([ele.name hasSuffix:@".mp3"])
  61. {
  62. localpatch = ele.name;
  63. }else
  64. {
  65. localpatch = [NSString stringWithFormat:@"%@.mp3",model.id];
  66. ele.name =localpatch;
  67. }
  68. if([[NSFileManager defaultManager] fileExistsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/bgm/%@", localpatch]]]){
  69. _model.localPatch =[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/bgm/%@", localpatch]];
  70. iscanDosimultane =YES;
  71. }
  72. else [_bgmHelper downloadBGM:ele];
  73. }
  74. - (void)startPlaymusic
  75. {
  76. if (_model.localPatch)
  77. {
  78. NSError *error =nil;
  79. AVAudioPlayer *player =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:_model.localPatch] error:&error];
  80. player.volume =0.6;
  81. player.numberOfLoops=1;
  82. [player prepareToPlay];
  83. [player play];
  84. if (error)
  85. {
  86. NSLog(@"%@",error);
  87. }
  88. // CGFloat _BGMDuration =[[TXUGCRecord shareInstance]setBGM:_model.localPatch];
  89. // [[TXUGCRecord shareInstance] setBGMVolume:.7];
  90. // [[TXUGCRecord shareInstance]playBGMFromTime:0 toTime:_BGMDuration withBeginNotify:^(NSInteger errCode) {
  91. // NSLog(@"%ld",errCode);
  92. // } withProgressNotify:^(NSInteger progressMS, NSInteger durationMS) {
  93. // NSLog(@"%ld",durationMS);
  94. // } andCompleteNotify:^(NSInteger errCode) {
  95. // NSLog(@"%ld",errCode);
  96. // }];
  97. }
  98. }
  99. - (void)getdataWith:(int) page
  100. {
  101. __block typeof(self)blockself =self;
  102. [Music_manager getMusicSameparagraphWIthmid:_model.id andPage:_page andCallback:^(id response) {
  103. if (response)
  104. {
  105. if (blockself.page ==1)
  106. {
  107. blockself.dataAry =[response mutableCopy];
  108. }else
  109. {
  110. [blockself.dataAry addObjectsFromArray:response];
  111. }
  112. [blockself.collectionview reloadData];
  113. }
  114. [blockself.collectionview.mj_header endRefreshing];
  115. [blockself.collectionview.mj_footer endRefreshing];
  116. }];
  117. }
  118. #pragma 代理
  119. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  120. {
  121. new_SimultaneousCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  122. cell.model =[SmallVideoListModel mj_objectWithKeyValues:self.dataAry[indexPath.item]];
  123. cell.index =indexPath.item;
  124. return cell;
  125. }
  126. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  127. {
  128. if ([kind isEqualToString:UICollectionElementKindSectionHeader])
  129. {
  130. new_SimultaneousheaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionheader" forIndexPath:indexPath];
  131. headerView.model =_model;
  132. headerView.delegate =self;
  133. return headerView;
  134. }else{
  135. return nil;
  136. }
  137. }
  138. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  139. {
  140. return 1;
  141. }
  142. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  143. {
  144. return self.dataAry.count;
  145. }
  146. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  147. {
  148. NSMutableArray *listAry =[NSMutableArray new];
  149. for (id obj in self.dataAry)
  150. {
  151. SmallVideoListModel* model =[SmallVideoListModel mj_objectWithKeyValues:obj];
  152. [listAry addObject:model];
  153. }
  154. HMVideoPlayerViewController *vc = [[HMVideoPlayerViewController alloc]initWithVideos:listAry index:indexPath.item IsPushed:YES requestDict:@{}];
  155. [[AppDelegate sharedAppDelegate] pushViewController:vc];
  156. }
  157. - (void)didselectWithIndex:(int)index
  158. {
  159. switch (index)
  160. {
  161. case 1:
  162. {
  163. //返回
  164. [self.navigationController popViewControllerAnimated:YES];
  165. }
  166. break;
  167. case 2:
  168. {
  169. //分享
  170. }
  171. break;
  172. default:
  173. break;
  174. }
  175. }
  176. -(void) onBGMDownloadDone:(TCBGMElement*)element
  177. {
  178. if([[element isValid] boolValue]){
  179. BGMLog(@"Download \"%@\" success!", [element name]);
  180. NSString *localpatch;
  181. if ([element.name hasSuffix:@".mp3"])
  182. {
  183. localpatch =element.name;
  184. }else
  185. {
  186. localpatch = [NSString stringWithFormat:@"%@.mp3",element.name];
  187. element.name =localpatch;
  188. }
  189. _model.localPatch =[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/bgm/%@", localpatch]];
  190. iscanDosimultane =YES;
  191. }
  192. else BGMLog(@"Download \"%@\" failed!", [element name]);
  193. }
  194. -(void) onBGMListLoad:(NSDictionary*)dict
  195. {
  196. }
  197. - (void)onBGMDownloading:(TCBGMElement *)current percent:(float)percent
  198. {
  199. }
  200. #pragma 懒加载
  201. - (UICollectionView *)collectionview
  202. {
  203. if (!_collectionview)
  204. {
  205. CGFloat item_w =(kScreenW-3) /3;
  206. UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];
  207. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  208. layout.minimumLineSpacing =0;
  209. layout.minimumInteritemSpacing =0;
  210. layout.itemSize =CGSizeMake(item_w, 340*w_bili);
  211. layout.headerReferenceSize =CGSizeMake(kScreenW, 470*w_bili);
  212. _collectionview =[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
  213. _collectionview.delegate =self;
  214. _collectionview.dataSource =self;
  215. [self.view addSubview:_collectionview];
  216. [_collectionview registerClass:[new_SimultaneousCell class] forCellWithReuseIdentifier:@"cell"];
  217. [_collectionview registerClass:[new_SimultaneousheaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionheader"];
  218. __block typeof(self)blockself =self;
  219. _collectionview.mj_footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  220. blockself.page ++;
  221. [blockself getdataWith:blockself.page];
  222. }];
  223. _collectionview.mj_header =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
  224. blockself.page =1;
  225. [blockself getdataWith:blockself.page];
  226. }];
  227. }
  228. return _collectionview;
  229. }
  230. - (NSMutableArray *)dataAry
  231. {
  232. if (!_dataAry)
  233. {
  234. _dataAry =[NSMutableArray new];
  235. }
  236. return _dataAry;
  237. }
  238. - (UIButton *)doSameparagraph
  239. {
  240. if (!_doSameparagraph)
  241. {
  242. _doSameparagraph =[[UIButton alloc]init];
  243. [self.view addSubview:_doSameparagraph];
  244. [_doSameparagraph mas_makeConstraints:^(MASConstraintMaker *make) {
  245. make.bottom.equalTo(self.view).offset(-40*w_bili);
  246. make.centerX.equalTo(self.view);
  247. make.width.height.equalTo(@(140*w_bili));
  248. }];
  249. [_doSameparagraph setBackgroundImage:[UIImage imageNamed:@"dosame_icon"] forState:0];
  250. }
  251. return _doSameparagraph;
  252. }
  253. - (void)didReceiveMemoryWarning {
  254. [super didReceiveMemoryWarning];
  255. // Dispose of any resources that can be recreated.
  256. }
  257. /*
  258. #pragma mark - Navigation
  259. // In a storyboard-based application, you will often want to do a little preparation before navigation
  260. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  261. // Get the new view controller using [segue destinationViewController].
  262. // Pass the selected object to the new view controller.
  263. }
  264. */
  265. @end