YBTakeSameVideoVC.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. //
  2. // YBTakeSameVideoVC.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2019/11/27.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "YBTakeSameVideoVC.h"
  9. #import "VideoCollectionCell.h"
  10. #import "NearbyVideoModel.h"
  11. #import "YBLookVideoVC.h"
  12. #import "TCVideoRecordViewController.h"
  13. #import "YBGetVideoObj.h"
  14. @interface YBTakeSameVideoVC ()<UICollectionViewDelegate,UICollectionViewDataSource,NSURLConnectionDelegate,NSURLConnectionDataDelegate>
  15. {
  16. int _paging;
  17. NSMutableData *musicData;
  18. long long allLength;
  19. float currentLength;
  20. }
  21. @property(nonatomic,strong)UIView *headerView;
  22. @property(nonatomic,strong)UIImageView *bgImgView;
  23. @property(nonatomic,strong)UIView *effectView;
  24. @property(nonatomic,strong)UIImageView *musicIV;
  25. @property(nonatomic,strong)UILabel *musicNameL;
  26. @property(nonatomic,strong)UILabel *musicAuthorL;
  27. @property(nonatomic,strong)UILabel *musicUsesL;
  28. @property(nonatomic,strong)UICollectionView *collectionView;
  29. @property(nonatomic,strong)NSMutableArray *dataArray;
  30. @property(nonatomic,strong)UIButton *sameBtn;
  31. @end
  32. @implementation YBTakeSameVideoVC
  33. -(void)viewWillAppear:(BOOL)animated {
  34. [super viewWillAppear:animated];
  35. [self pullData];
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. self.subNavi.backgroundColor = UIColor.clearColor;
  40. self.dataArray = [NSMutableArray array];
  41. _paging = 1;
  42. [self.view addSubview:self.collectionView];
  43. _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  44. [self.view addSubview:self.sameBtn];
  45. [_sameBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  46. make.width.height.mas_equalTo(70);
  47. make.centerX.equalTo(self.view);
  48. make.bottom.equalTo(self.view.mas_bottom).offset(-ShowDiff-34);
  49. }];
  50. [self.view bringSubviewToFront:self.naviView];
  51. }
  52. -(void)pullData {
  53. YBWeakSelf;
  54. [YBNetworking postWithUrl:@"Video.getVideoListByMusic" Dic:@{@"p":@(_paging),@"musicid":minstr([_musicDic valueForKey:@"id"])} Suc:^(int code, id info, NSString *msg) {
  55. [_collectionView.mj_footer endRefreshing];
  56. [_collectionView.mj_header endRefreshing];
  57. if (code == 0) {
  58. NSDictionary *infoDic = [info firstObject];
  59. NSDictionary *music_dic = [infoDic valueForKey:@"musicinfo"];
  60. if (_paging == 1) {
  61. [_dataArray removeAllObjects];
  62. [_musicIV sd_setImageWithURL:[NSURL URLWithString:minstr([music_dic valueForKey:@"img_url"])]];
  63. [_bgImgView sd_setImageWithURL:[NSURL URLWithString:minstr([music_dic valueForKey:@"img_url"])]];
  64. _musicNameL.text = minstr([music_dic valueForKey:@"title"]);
  65. _musicAuthorL.text = minstr([music_dic valueForKey:@"author"]);
  66. _musicUsesL.text = minstr([music_dic valueForKey:@"use_nums"]);
  67. }
  68. NSArray *listA = [infoDic valueForKey:@"videolist"];
  69. if (listA.count<=0) {
  70. [_collectionView.mj_footer endRefreshingWithNoMoreData];
  71. }else {
  72. [_dataArray addObjectsFromArray:listA];
  73. }
  74. if (_dataArray.count <= 0) {
  75. [PublicView showTextNoData:_collectionView text1:@"" text2:YZMsg(@"暂无视频哦~") centerY:0.8];
  76. }else {
  77. [PublicView hiddenTextNoData:_collectionView];
  78. }
  79. [_collectionView reloadData];
  80. }else {
  81. [MBProgressHUD showError:msg];
  82. [weakSelf.navigationController popViewControllerAnimated:YES];
  83. }
  84. } Fail:^(id fail) {
  85. [_collectionView.mj_footer endRefreshing];
  86. [_collectionView.mj_header endRefreshing];
  87. }];
  88. }
  89. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  90. }
  91. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  92. CGFloat moveY = scrollView.contentOffset.y < 0 ? 0 : scrollView.contentOffset.y;
  93. _sameBtn.bottom = _window_height-ShowDiff-34+moveY;
  94. NSLog(@"ssss=%f",scrollView.contentOffset.y);
  95. }
  96. #pragma mark - CollectionView 代理
  97. /*
  98. * minimumLineSpacing、minimumInteritemSpacing去设置
  99. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  100. return CGSizeMake(0,0);
  101. }
  102. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  103. {
  104. return UIEdgeInsetsMake(0,0,0,0);
  105. }
  106. */
  107. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  108. if (kind == UICollectionElementKindSectionHeader) {
  109. UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YBTackSameHeader" forIndexPath:indexPath];
  110. [reusableView addSubview:self.headerView];
  111. return reusableView;
  112. }
  113. return nil;
  114. }
  115. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  116. return CGSizeMake(_window_width, 120+64+statusbarHeight);
  117. }
  118. //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  119. // return 0.01;
  120. //}
  121. //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  122. // return 0.01;
  123. //}
  124. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  125. return 1;
  126. }
  127. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  128. return _dataArray.count;
  129. }
  130. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  131. VideoCollectionCell *cell = (VideoCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"VideoCollectionCell" forIndexPath:indexPath];
  132. NSDictionary *subdic = _dataArray[indexPath.row];
  133. cell.isTakeSameList = YES;
  134. cell.model = [[NearbyVideoModel alloc] initWithDic:subdic];
  135. return cell;
  136. }
  137. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  138. NSString *vidoeID = minstr([_dataArray[indexPath.row] valueForKey:@"id"]);
  139. [YBGetVideoObj lookManeger].fromWhere = @"YBTakeSameVideoVC";
  140. [YBGetVideoObj lookManeger].videoID = vidoeID;
  141. [YBGetVideoObj lookManeger].playIndex = (int)indexPath.row;
  142. [YBGetVideoObj lookManeger].videoList = [_dataArray mutableCopy];
  143. [YBGetVideoObj lookManeger].paging = _paging;
  144. NSString *basUrl = [NSString stringWithFormat:@"Video.getVideoListByMusic&musicid=%@",[_musicDic valueForKey:@"id"]];
  145. [YBGetVideoObj lookManeger].baseUrl = basUrl;
  146. [[YBGetVideoObj lookManeger]goLookVC];
  147. }
  148. #pragma mark - set/get
  149. - (UIView *)effectView {
  150. if (!_effectView) {
  151. if (@available(iOS 8.0, *)) {
  152. UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  153. _effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
  154. } else {
  155. UIToolbar *effectView = [[UIToolbar alloc] init];
  156. effectView.barStyle = UIBarStyleBlackTranslucent;
  157. _effectView = effectView;
  158. }
  159. }
  160. return _effectView;
  161. }
  162. - (UICollectionView *)collectionView {
  163. if (!_collectionView) {
  164. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
  165. flow.scrollDirection = UICollectionViewScrollDirectionVertical;
  166. flow.itemSize = CGSizeMake((_window_width-2)/3, (_window_width-2)/3 * 1.4);
  167. flow.minimumLineSpacing = 1;
  168. flow.minimumInteritemSpacing = 1;
  169. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, _window_width, _window_height) collectionViewLayout:flow];
  170. [_collectionView registerNib:[UINib nibWithNibName:@"VideoCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"VideoCollectionCell"];
  171. [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YBTackSameHeader"];
  172. _collectionView.delegate =self;
  173. _collectionView.dataSource = self;
  174. _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  175. _paging ++;
  176. [self pullData];
  177. }];
  178. _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  179. _paging = 1;
  180. [self pullData];
  181. }];
  182. _collectionView.contentInset = UIEdgeInsetsMake(0, 0, ShowDiff, 0);
  183. _collectionView.backgroundColor = Normal_Color;
  184. }
  185. return _collectionView;
  186. }
  187. - (UIView *)headerView {
  188. if (!_headerView) {
  189. _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, 120+64+statusbarHeight)];
  190. _headerView.backgroundColor = RGB_COLOR(@"#eeeeee", 1);
  191. _bgImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _headerView.width, _headerView.height)];
  192. [_headerView addSubview:_bgImgView];
  193. [_bgImgView addSubview:self.effectView];
  194. [_effectView mas_makeConstraints:^(MASConstraintMaker *make) {
  195. make.width.height.centerX.centerY.equalTo(_bgImgView);
  196. }];
  197. _musicIV = [[UIImageView alloc]init];
  198. _musicIV.layer.cornerRadius = 5;
  199. _musicIV.layer.masksToBounds = YES;
  200. [_headerView addSubview:_musicIV];
  201. [_musicIV mas_makeConstraints:^(MASConstraintMaker *make) {
  202. make.left.equalTo(_headerView.mas_left).offset(15);
  203. make.top.equalTo(_headerView.mas_top).offset(10+64+statusbarHeight);
  204. make.width.height.mas_equalTo(100);
  205. }];
  206. //2.作者名称
  207. _musicAuthorL = [[UILabel alloc]init];
  208. _musicAuthorL.font = SYS_Font(13);
  209. _musicAuthorL.textColor = RGB_COLOR(@"#969696", 1);
  210. [_headerView addSubview:_musicAuthorL];
  211. [_musicAuthorL mas_makeConstraints:^(MASConstraintMaker *make) {
  212. make.centerY.equalTo(_musicIV);
  213. make.left.equalTo(_musicIV.mas_right).offset(12);
  214. make.right.lessThanOrEqualTo(_headerView.mas_right).offset(-15);
  215. }];
  216. //1.音乐名称
  217. _musicNameL = [[UILabel alloc]init];
  218. _musicNameL.font = [UIFont boldSystemFontOfSize:17];
  219. _musicNameL.textColor = RGB_COLOR(@"#ffffff", 1);
  220. [_headerView addSubview:_musicNameL];
  221. [_musicNameL mas_makeConstraints:^(MASConstraintMaker *make) {
  222. make.bottom.equalTo(_musicAuthorL.mas_top).offset(-8);
  223. make.left.equalTo(_musicAuthorL);
  224. make.right.lessThanOrEqualTo(_headerView.mas_right).offset(-15);
  225. }];
  226. //3.使用人数
  227. _musicUsesL = [[UILabel alloc]init];
  228. _musicUsesL.font = SYS_Font(10);
  229. _musicUsesL.textColor = RGB_COLOR(@"#969696", 1);
  230. [_headerView addSubview:_musicUsesL];
  231. [_musicUsesL mas_makeConstraints:^(MASConstraintMaker *make) {
  232. make.top.equalTo(_musicAuthorL.mas_bottom).offset(8);
  233. make.left.equalTo(_musicAuthorL);
  234. make.right.lessThanOrEqualTo(_headerView.mas_right).offset(-15);
  235. }];
  236. }
  237. return _headerView;
  238. }
  239. - (UIButton *)sameBtn {
  240. if (!_sameBtn) {
  241. _sameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  242. [_sameBtn setImage:[UIImage imageNamed:getImagename(@"视频-拍同款")] forState:0];
  243. [_sameBtn addTarget:self action:@selector(clickSameBtn) forControlEvents:UIControlEventTouchUpInside];
  244. _sameBtn.backgroundColor = UIColor.clearColor;
  245. }
  246. return _sameBtn;
  247. }
  248. -(void)clickSameBtn {
  249. if ([[Config getOwnID] intValue] <= 0) {
  250. [PublicObj warnLogin];
  251. return;
  252. }
  253. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  254. if (0 < app.allUploadPro && app.allUploadPro < 1) {
  255. [MBProgressHUD showPop:YZMsg(@"视频发布中,暂不支持操作")];
  256. return;
  257. }
  258. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  259. NSString *docDir = [paths objectAtIndex:0];
  260. NSString *loadPath = [docDir stringByAppendingFormat:@"/*%@*%@*%@*%@.mp3",minstr([_musicDic valueForKey:@"title"]),minstr([_musicDic valueForKey:@"author"]),minstr([_musicDic valueForKey:@"length"]),minstr([_musicDic valueForKey:@"id"])];
  261. NSFileManager *manager = [NSFileManager defaultManager];
  262. if ([manager fileExistsAtPath:loadPath]) {
  263. //已下载
  264. [self goVideoRecord:loadPath];
  265. }else{
  266. //下载歌曲
  267. [self musicDownLoad];
  268. }
  269. }
  270. -(void)goVideoRecord:(NSString *)localPath {
  271. [MBProgressHUD showMessage:@""];
  272. YBWeakSelf;
  273. [YBNetworking postWithUrl:@"User.checkLiveVipStatus" Dic:nil Suc:^(int code, id info, NSString *msg) {
  274. [MBProgressHUD hideHUD];
  275. if (code == 0) {
  276. NSDictionary *dic = [info firstObject];
  277. [YBPower saveUnified:dic];
  278. [weakSelf judgeCanGoTake:localPath];
  279. }else {
  280. [MBProgressHUD showPop:msg];
  281. }
  282. } Fail:^(id fail) {
  283. [MBProgressHUD hideHUD];
  284. }];
  285. }
  286. -(void)judgeCanGoTake:(NSString *)localPath {
  287. NSString *videoLimit = minstr([YBPower getVideoStatus]);
  288. NSString *videoMsg = minstr([YBPower getVideoMsg]);
  289. if ([videoLimit isEqual:@"1"]) {
  290. TCVideoRecordViewController *video = [[TCVideoRecordViewController alloc]init];
  291. video.haveBGM = YES;
  292. video.musicPath = localPath;
  293. video.musicID = minstr([_musicDic valueForKey:@"id"]);
  294. video.isTakeSame = YES;
  295. video.recordType = RecordType_Normal;
  296. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:video animated:YES];
  297. }else {
  298. [MBProgressHUD showPop:videoMsg];
  299. }
  300. }
  301. #pragma mark - 下载
  302. -(void)musicDownLoad{
  303. dispatch_async(dispatch_get_main_queue(), ^{
  304. [MBProgressHUD showMessage:@""];
  305. });
  306. //下载
  307. NSURL *url = [NSURL URLWithString:minstr([_musicDic valueForKey:@"file_url"])];
  308. // NSURLRequest *request = [NSURLRequest requestWithURL:url];
  309. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  310. request.allHTTPHeaderFields = @{@"referer":h5url};
  311. [NSURLConnection connectionWithRequest:request delegate:self];
  312. }
  313. //接收到服务器响应的时候开始调用这个方法
  314. -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
  315. musicData = [NSMutableData data];
  316. allLength = [response expectedContentLength];//返回服务器链接数据的有效大小
  317. }
  318. //开始进行数据传输的时候执行这个方法
  319. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
  320. [musicData appendData:data];
  321. currentLength += data.length;
  322. NSString *string = [NSString stringWithFormat:@"%.f%%",(currentLength/allLength)*100];
  323. }
  324. //数据传输完成的时候执行这个方法
  325. -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
  326. NSLog(@"已经完成数据的接收-------------");
  327. dispatch_async(dispatch_get_main_queue(), ^{
  328. [MBProgressHUD hideHUD];
  329. });
  330. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  331. NSString *docDir = [paths objectAtIndex:0];
  332. NSString *boxPath = [docDir stringByAppendingFormat:@"/*%@*%@*%@*%@.mp3",minstr([_musicDic valueForKey:@"title"]),minstr([_musicDic valueForKey:@"author"]),minstr([_musicDic valueForKey:@"length"]),minstr([_musicDic valueForKey:@"id"])];
  333. if([musicData writeToFile:boxPath atomically:YES]){
  334. NSLog(@"保存成功");
  335. //下载事件
  336. [self goVideoRecord:boxPath];
  337. }else{
  338. //下载事件
  339. [MBProgressHUD showError:YZMsg(@"音乐获取失败")];
  340. NSLog(@"保存失败");
  341. }
  342. }
  343. //数据传输错误的时候执行这个方法
  344. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
  345. NSLog(@"请求错误的时候 %@",[error localizedDescription]);
  346. dispatch_async(dispatch_get_main_queue(), ^{
  347. [MBProgressHUD hideHUD];
  348. });
  349. //下载事件
  350. [MBProgressHUD showError:YZMsg(@"音乐获取失败")];
  351. NSLog(@"保存失败");
  352. }
  353. @end