MusicCell.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // MusicCell.m
  3. // YBVideo
  4. //
  5. // Created by YunBao on 2018/6/20.
  6. // Copyright © 2018年 cat. All rights reserved.
  7. //
  8. #import "MusicCell.h"
  9. #import "MusicModel.h"
  10. @interface MusicCell()<NSURLConnectionDelegate,NSURLConnectionDataDelegate>
  11. {
  12. NSMutableData *musicData;
  13. long long allLength;
  14. float currentLength;
  15. }
  16. @end
  17. @implementation MusicCell
  18. - (void)awakeFromNib {
  19. [super awakeFromNib];
  20. // Initialization code
  21. _startRecoedBtn.layer.cornerRadius = 5;
  22. _startRecoedBtn.layer.masksToBounds = YES;
  23. _startRecoedBtn.backgroundColor = Pink_Cor;
  24. [_startRecoedBtn setTitle:YZMsg(@"确认使用并开拍") forState:0];
  25. /** 撑起开拍按钮以上80像素高度,避免cell点击事件改变cell高度时cell内部控件变形 */
  26. _topBgV.backgroundColor = [UIColor whiteColor];
  27. }
  28. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  29. [super setSelected:selected animated:animated];
  30. // Configure the view for the selected state
  31. }
  32. +(MusicCell*)cellWithTab:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
  33. MusicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicCell"];
  34. if (!cell) {
  35. cell = [[[NSBundle mainBundle]loadNibNamed:@"MusicCell" owner:nil options:nil]objectAtIndex:0];
  36. // cell.useBtn.layer.borderWidth = 1;
  37. // cell.useBtn.layer.borderColor = Pink_Cor.CGColor;
  38. // cell.useBtn.layer.masksToBounds = YES;
  39. // cell.useBtn.layer.cornerRadius = 13;
  40. }
  41. return cell;
  42. }
  43. - (void)setModel:(MusicModel *)model{
  44. _model = model;
  45. _songID = _model.songID;
  46. _path = _model.urlStr;
  47. [_bgIV sd_setImageWithURL:[NSURL URLWithString:_model.bgStr]];
  48. _musicNameL.text = _model.musicNameStr;
  49. _singerL.text = _model.singerStr;
  50. _timeL.text = _model.timeStr;
  51. if ([_model.isCollectStr isEqual:@"1"]) {
  52. [_useBtn setImage:[UIImage imageNamed:@"music_collect"] forState:0];
  53. }else{
  54. [_useBtn setImage:[UIImage imageNamed:@"music_uncollect"] forState:0];
  55. }
  56. [_StateBtn setImage:[UIImage imageNamed:@"music_play"] forState:0];
  57. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  58. // NSString *docDir = [paths objectAtIndex:0];
  59. // NSString *loadPath = [docDir stringByAppendingFormat:@"/*%@*%@*%@*%@.mp3",_model.musicNameStr,_model.singerStr,_model.timeStr,_model.songID];
  60. // NSFileManager *manager = [NSFileManager defaultManager];
  61. // if ([manager fileExistsAtPath:loadPath]) {
  62. // NSURL *url = [NSURL URLWithString:_path];
  63. // NSURLRequest *request = [NSURLRequest requestWithURL:url];
  64. // [NSURLConnection connectionWithRequest:request delegate:self];
  65. //
  66. // }
  67. // [self.useBtn setTitle:@"使用" forState:UIControlStateNormal];
  68. }
  69. - (IBAction)clickUseBtn:(UIButton *)sender {
  70. //收藏按钮
  71. NSString *url = [NSString stringWithFormat:@"Music.collectMusic&uid=%@&token=%@&musicid=%@",[Config getOwnID],[Config getOwnToken],_model.songID];
  72. [YBNetworking postWithUrl:url Dic:nil Suc:^(int code, id info, NSString *msg) {
  73. if (code == 0) {
  74. NSDictionary *infoDic = [info firstObject];
  75. NSString *isCollect = [NSString stringWithFormat:@"%@",[infoDic valueForKey:@"iscollect"]];
  76. if ([isCollect isEqual:@"1"]) {
  77. [_useBtn setImage:[UIImage imageNamed:@"music_collect"] forState:0];
  78. }else{
  79. [_useBtn setImage:[UIImage imageNamed:@"music_uncollect"] forState:0];
  80. }
  81. [_useBtn.imageView.layer addAnimation:[PublicObj originToBigToSmallRecovery] forKey:nil];
  82. [MBProgressHUD showPop:msg];
  83. }else{
  84. [MBProgressHUD showPop:msg];
  85. }
  86. } Fail:^(id fail) {
  87. }];
  88. }
  89. - (IBAction)clickStartRecordBtn:(UIButton *)sender {
  90. _startRecoedBtn.backgroundColor = RGB_COLOR(@"#da2c72", 1);
  91. self.recordEvent(@"");
  92. }
  93. -(void)musicDownLoad{
  94. if (![_path hasPrefix:@"http"]) {
  95. [MBProgressHUD showPop:YZMsg(@"链接不合法")];
  96. return;
  97. }
  98. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  99. NSString *docDir = [paths objectAtIndex:0];
  100. NSString *loadPath = [docDir stringByAppendingFormat:@"/*%@*%@*%@*%@.mp3",_model.musicNameStr,_model.singerStr,_model.timeStr,_model.songID];
  101. NSFileManager *manager = [NSFileManager defaultManager];
  102. if (![manager fileExistsAtPath:loadPath]) {
  103. //下载
  104. NSURL *url = [NSURL URLWithString:_path];
  105. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  106. // NSURLRequest *request = [NSURLRequest requestWithURL:url];
  107. request.allHTTPHeaderFields = @{@"referer":h5url};
  108. [NSURLConnection connectionWithRequest:request delegate:self];
  109. return;
  110. }else{
  111. //选择
  112. }
  113. }
  114. //接收到服务器响应的时候开始调用这个方法
  115. -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
  116. musicData = [NSMutableData data];
  117. allLength = [response expectedContentLength];//返回服务器链接数据的有效大小
  118. }
  119. //开始进行数据传输的时候执行这个方法
  120. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
  121. [musicData appendData:data];
  122. currentLength += data.length;
  123. NSString *string = [NSString stringWithFormat:@"%.f%%",(currentLength/allLength)*100];
  124. NSLog(@"down%@",string);
  125. }
  126. //数据传输完成的时候执行这个方法
  127. -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
  128. NSLog(@"已经完成数据的接收-------------");
  129. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  130. NSString *docDir = [paths objectAtIndex:0];
  131. NSString *boxPath = [docDir stringByAppendingFormat:@"/*%@*%@*%@*%@.mp3",_model.musicNameStr,_model.singerStr,_model.timeStr,_model.songID];
  132. if([musicData writeToFile:boxPath atomically:YES]){
  133. NSLog(@"保存成功");
  134. //下载事件
  135. self.rsEvent(@"sucess",@"");
  136. }else{
  137. //下载事件
  138. self.rsEvent(@"fail",@"save fail");
  139. NSLog(@"保存失败");
  140. }
  141. }
  142. //数据传输错误的时候执行这个方法
  143. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
  144. NSLog(@"请求错误的时候 %@",[error localizedDescription]);
  145. //下载事件
  146. self.rsEvent(@"fail",[NSString stringWithFormat:@"%@",[error localizedDescription]]);
  147. NSLog(@"保存失败");
  148. }
  149. @end