TCMusicMixView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // MusicMixView.m
  3. // DeviceManageIOSApp
  4. //
  5. // Created by rushanting on 2017/5/12.
  6. // Copyright © 2017年 tencent. All rights reserved.
  7. //
  8. #import "TCMusicMixView.h"
  9. //#import "UIView+AdditionsX12.h"
  10. #import "ColorMacro.h"
  11. #import "TCRangeContent.h"
  12. #import "TCMusicCollectionCell.h"
  13. #import <AVFoundation/AVFoundation.h>
  14. #define PIN_WIDTH 13
  15. #define THUMB_HEIGHT 60
  16. #define BORDER_HEIGHT 2
  17. @interface TCMusicMixView()<TCRangeContentDelegate>
  18. @end
  19. @implementation TCMusicMixView
  20. {
  21. NSString* _selectedFilePath;
  22. CGFloat _musicDuration;
  23. //UIView* _editView; //放到.h在外部需要控制其hidden属性
  24. UIImageView* _musicIcon;
  25. UILabel* _songName;
  26. UIButton* _deleteBtn;
  27. UILabel* _cutTitleLabel;
  28. TCRangeContent* _musicCutSlider;
  29. UILabel* _startTimeLabel;
  30. UILabel* _endTimeLabel;
  31. UIView* _voiceView;
  32. UILabel* _originalL;
  33. UILabel* _bgmL;
  34. UISlider* _originalSlider;
  35. UISlider* _bgmSlider;
  36. CGFloat _originalVolume; //原声音量
  37. CGFloat _bgmVolume; //背景音乐音量
  38. }
  39. - (id)initWithFrame:(CGRect)frame haveBgm:(BOOL)haveBGM{
  40. if (self = [super initWithFrame:frame]) {
  41. //默认
  42. //_originalVolume = 0.8;
  43. _originalVolume = ([PublicObj getSysOutputVolume] < 0.2 )? 0.3 : [PublicObj getSysOutputVolume];
  44. _bgmVolume = 0.0;
  45. if (haveBGM==YES) {
  46. _originalVolume = 0.0;
  47. _bgmVolume = ([PublicObj getSysOutputVolume] < 0.2 )? 0.3 : [PublicObj getSysOutputVolume];
  48. }
  49. _editView = [[UIView alloc] init];
  50. _editView.backgroundColor = Normal_Color;
  51. [self addSubview:_editView];
  52. //外部(edit)控制其显示和隐藏
  53. _editView.hidden = YES;
  54. _musicIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"voice"]];
  55. [_editView addSubview:_musicIcon];
  56. _songName = [[UILabel alloc] init];
  57. _songName.text = YZMsg(@"歌曲名");
  58. _songName.textColor = UIColorFromRGB(0x777777);
  59. _songName.font = [UIFont systemFontOfSize:14];
  60. _songName.textAlignment = NSTextAlignmentLeft;
  61. [_editView addSubview:_songName];
  62. _deleteBtn = [[UIButton alloc] init];
  63. [_deleteBtn setTitle:YZMsg(@"删除") forState:UIControlStateNormal];
  64. [_deleteBtn setTitleColor:Pink_Cor forState:UIControlStateNormal];//UIColorFromRGB(0x0accac)
  65. [_deleteBtn addTarget:self action:@selector(onDeleteBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  66. [_editView addSubview:_deleteBtn];
  67. _cutTitleLabel = [[UILabel alloc] init];
  68. _cutTitleLabel.textColor = UIColorFromRGB(0xcccccc);
  69. _cutTitleLabel.text = YZMsg(@"截取所需音频片段");
  70. _cutTitleLabel.textAlignment = NSTextAlignmentCenter;
  71. _cutTitleLabel.font = [UIFont systemFontOfSize:14];
  72. [_editView addSubview:_cutTitleLabel];
  73. TCRangeContentConfig* sliderConfig = [TCRangeContentConfig new];
  74. sliderConfig.pinWidth = 16;
  75. sliderConfig.borderHeight = 1;
  76. sliderConfig.thumbHeight = 58;
  77. sliderConfig.leftPinImage = [UIImage imageNamed:@"left"];
  78. sliderConfig.rightPigImage = [UIImage imageNamed:@"right"];
  79. _musicCutSlider = [[TCRangeContent alloc] initWithImageList:@[[UIImage imageNamed:@"wave_chosen"]] config:sliderConfig];
  80. _musicCutSlider.middleLine.hidden = YES;
  81. _musicCutSlider.delegate = self;
  82. [_editView addSubview:_musicCutSlider];
  83. _startTimeLabel = [[UILabel alloc] init];
  84. _startTimeLabel.textColor = UIColorFromRGB(0x777777);
  85. _startTimeLabel.font = [UIFont systemFontOfSize:10];
  86. _startTimeLabel.text = @"0:00";
  87. [_editView addSubview:_startTimeLabel];
  88. _endTimeLabel = [[UILabel alloc] init];
  89. _endTimeLabel.textColor = UIColorFromRGB(0x777777);
  90. _endTimeLabel.font = [UIFont systemFontOfSize:10];
  91. _endTimeLabel.text = @"0:00";
  92. [_editView addSubview:_endTimeLabel];
  93. _voiceView = [[UIView alloc]init];
  94. _voiceView.backgroundColor = Normal_Color;
  95. [self addSubview:_voiceView];
  96. _originalL = [[UILabel alloc] init];
  97. _originalL.textColor = UIColorFromRGB(0x777777);
  98. _originalL.font = [UIFont systemFontOfSize:14];
  99. _originalL.text = YZMsg(@"原声");
  100. [_voiceView addSubview:_originalL];
  101. _bgmL = [[UILabel alloc] init];
  102. _bgmL.textColor = UIColorFromRGB(0x777777);
  103. _bgmL.font = [UIFont systemFontOfSize:14];
  104. _bgmL.text = YZMsg(@"配乐");
  105. [_voiceView addSubview:_bgmL];
  106. _originalSlider = [[UISlider alloc] init];
  107. _originalSlider.minimumValue = 0;
  108. _originalSlider.maximumValue = 1;
  109. _originalSlider.thumbTintColor = Pink_Cor;
  110. _originalSlider.minimumTrackTintColor = Pink_Cor;
  111. _originalSlider.maximumTrackTintColor = UIColorFromRGB(0x777777);
  112. _originalSlider.value = _originalVolume;
  113. [_originalSlider setThumbImage:[UIImage imageNamed:@"button_slider"] forState:UIControlStateNormal];
  114. [_originalSlider addTarget:self action:@selector(onOriginalSliderChange:) forControlEvents:UIControlEventValueChanged];
  115. [_voiceView addSubview:_originalSlider];
  116. _bgmSlider = [[UISlider alloc] init];
  117. _bgmSlider.minimumValue = 0;
  118. _bgmSlider.maximumValue = 1;
  119. _bgmSlider.thumbTintColor = Pink_Cor;
  120. _bgmSlider.minimumTrackTintColor = Pink_Cor;
  121. _bgmSlider.maximumTrackTintColor = UIColorFromRGB(0x777777);
  122. _bgmSlider.value = _bgmVolume;
  123. [_bgmSlider setThumbImage:[UIImage imageNamed:@"button_slider"] forState:UIControlStateNormal];
  124. [_bgmSlider addTarget:self action:@selector(onBgmSliderChange:) forControlEvents:UIControlEventValueChanged];
  125. [_voiceView addSubview:_bgmSlider];
  126. //haveBGM这里初始值一旦为YES“原声”再不可编辑
  127. if (haveBGM == YES) {
  128. _originalSlider.enabled = NO;
  129. _originalSlider.alpha = 0.5;
  130. }else{
  131. _bgmSlider.enabled = NO;
  132. _bgmSlider.alpha = 0.5;
  133. }
  134. }
  135. return self;
  136. }
  137. - (void)dealloc {
  138. NSLog(@"MusicMixView dealloc");
  139. }
  140. - (void)layoutSubviews {
  141. [super layoutSubviews];
  142. //数据计算不要随意改动
  143. //--||_editViewfram ||原声、背景、底部空白
  144. //--||5+30 +10+15+5+60 +5+10=140 ||+15+15 +10+15 +20=75 =215
  145. _editView.frame = CGRectMake(0, 0, self.bounds.size.width, 140);
  146. _musicIcon.frame = CGRectMake(15, 5+(30-_musicIcon.height)/2, _musicIcon.width, _musicIcon.height);
  147. _songName.frame = CGRectMake(_musicIcon.right +5, 5, _editView.width / 2, 30);
  148. _deleteBtn.frame = CGRectMake(_editView.width - 50, 5, 40, 30);
  149. _cutTitleLabel.frame = CGRectMake(15, _musicIcon.bottom + 10, _editView.width - 30, 15);
  150. _musicCutSlider.frame = CGRectMake((_editView.width - _musicCutSlider.width) / 2, _cutTitleLabel.bottom +5, _musicCutSlider.width, _musicCutSlider.height);
  151. _startTimeLabel.frame = CGRectMake(_musicCutSlider.x + _musicCutSlider.leftScale * _musicCutSlider.width + _musicCutSlider.leftPin.width / 2, _musicCutSlider.bottom + 5, 30, 10);
  152. _endTimeLabel.frame = CGRectMake(_musicCutSlider.x + _musicCutSlider.rightScale * _musicCutSlider.width - 30 + _musicCutSlider.rightPin.width / 2, _musicCutSlider.bottom + 5, 30, 10);
  153. _voiceView.frame = CGRectMake(0, _editView.bottom, self.bounds.size.width, 75+ShowDiff);
  154. _originalL.frame = CGRectMake(15,15, 0, 15);
  155. [_originalL sizeToFit];
  156. _originalSlider.frame = CGRectMake(_originalL.right+5, _originalL.top, _editView.width-30-_originalL.width-5, 15);
  157. _bgmL.frame = CGRectMake(15 , _originalL.bottom+10, 0, 15);
  158. [_bgmL sizeToFit];
  159. _bgmSlider.frame = CGRectMake(_bgmL.right+5, _bgmL.top, _editView.width-30-_bgmL.width-5, 15);
  160. }
  161. - (void)addMusicInfo:(TCMusicInfo *)musicInfo{
  162. _bgmSlider.enabled = YES;
  163. _bgmSlider.alpha = 1;
  164. _bgmVolume = ([PublicObj getSysOutputVolume] < 0.2 )? 0.3 : [PublicObj getSysOutputVolume];
  165. _bgmSlider.value = _bgmVolume;
  166. [self showMusicInfo:musicInfo];
  167. }
  168. - (void)showMusicInfo:(TCMusicInfo *)musicInfo {
  169. _selectedFilePath = musicInfo.filePath;
  170. _musicDuration = musicInfo.duration;
  171. _songName.text = musicInfo.soneName;
  172. if (musicInfo.singerName.length > 0) {
  173. _songName.text = [NSString stringWithFormat:@"%@_%@", musicInfo.soneName, musicInfo.singerName];
  174. }
  175. _musicCutSlider.leftPinCenterX = _musicCutSlider.pinWidth / 2;
  176. _musicCutSlider.rightPinCenterX = _musicCutSlider.width - _musicCutSlider.pinWidth / 2;
  177. [_musicCutSlider setNeedsLayout];
  178. _startTimeLabel.frame = CGRectMake(_musicCutSlider.x + _musicCutSlider.leftPin.x, _musicCutSlider.bottom + 5, 30, 10);
  179. _startTimeLabel.text = [NSString stringWithFormat:@"%d:%02d", (int)(_musicCutSlider.leftScale *_musicDuration) / 60, (int)(_musicCutSlider.leftScale *_musicDuration) % 60];
  180. _endTimeLabel.frame = CGRectMake(_musicCutSlider.x + _musicCutSlider.rightPin.x + _musicCutSlider.pinWidth - 30, _musicCutSlider.bottom + 5, 30, 10);
  181. _endTimeLabel.text = [NSString stringWithFormat:@"%d:%02d", (int)(_musicCutSlider.rightScale *_musicDuration) / 60, (int)(_musicCutSlider.rightScale *_musicDuration) % 60];
  182. _cutTitleLabel.text = [NSString stringWithFormat:@"%@%.02f'",YZMsg(@"截取所需音频片段"),(_musicCutSlider.rightScale - _musicCutSlider.leftScale) * _musicDuration];
  183. [self.delegate onSetBGMWithFilePath:_selectedFilePath startTime:_musicCutSlider.leftScale * _musicDuration endTime:_musicCutSlider.rightScale * _musicDuration];
  184. [self.delegate onSetVideoVolume:_originalVolume musicVolume:_bgmVolume];
  185. }
  186. #pragma mark - UI control event Handle
  187. - (void)onDeleteBtnClicked:(UIButton*)sender {
  188. _musicDuration = 1;
  189. _editView.hidden = YES;
  190. _bgmSlider.enabled = NO;
  191. _bgmSlider.alpha = 0.5;
  192. _bgmSlider.value = 0;
  193. _bgmVolume = 0;
  194. //[self.delegate onSetBGMWithFilePath:@"delate" startTime:0 endTime:0];
  195. //更换收费版SDK 不能用[self.delegate onSetBGMWithFilePath:@"delate" startTime:0 endTime:0];,改为delBGM
  196. [self.delegate delBGM];
  197. }
  198. -(void)onOriginalSliderChange:(UISlider*)sender{
  199. _originalVolume = sender.value;
  200. [self.delegate onSetVideoVolume:_originalVolume musicVolume:_bgmVolume];
  201. }
  202. - (void)onBgmSliderChange:(UISlider*)sender {
  203. _bgmVolume = sender.value;
  204. [self.delegate onSetVideoVolume:_originalVolume musicVolume:_bgmVolume];
  205. }
  206. #pragma mark - RangeContentDelegate
  207. - (void)onRangeLeftChangeEnded:(TCRangeContent *)sender {
  208. CGFloat startTime = _musicCutSlider.leftScale *_musicDuration;
  209. CGFloat endTime = _musicCutSlider.rightScale * _musicDuration;
  210. assert(startTime >= 0 && endTime >= 0 && endTime >= startTime);
  211. _startTimeLabel.frame = CGRectMake(_musicCutSlider.x + _musicCutSlider.leftPin.x, _musicCutSlider.bottom + 5, 30, 10);
  212. _startTimeLabel.text = [NSString stringWithFormat:@"%d:%02d", (int)(startTime) / 60, (int)(startTime) % 60];
  213. _cutTitleLabel.text = [NSString stringWithFormat:@"%@%.02f'",YZMsg(@"截取所需音频片段"),(endTime - startTime)];
  214. [self.delegate onSetBGMWithFilePath:_selectedFilePath startTime:startTime endTime:endTime];
  215. }
  216. - (void)onRangeRightChangeEnded:(TCRangeContent *)sender {
  217. CGFloat startTime = _musicCutSlider.leftScale *_musicDuration;
  218. CGFloat endTime = _musicCutSlider.rightScale * _musicDuration;
  219. assert(startTime >= 0 && endTime >= 0 && endTime >= startTime);
  220. _endTimeLabel.frame = CGRectMake(MAX(_startTimeLabel.right, _musicCutSlider.x + _musicCutSlider.rightPin.x + _musicCutSlider.pinWidth - 30), _musicCutSlider.bottom + 5, 30, 10);
  221. _endTimeLabel.text = [NSString stringWithFormat:@"%d:%02d", (int)(endTime) / 60, (int)(endTime) % 60];
  222. _cutTitleLabel.text = [NSString stringWithFormat:@"%@%.02f'",YZMsg(@"截取所需音频片段"),(endTime - startTime)];
  223. [self.delegate onSetBGMWithFilePath:_selectedFilePath startTime:startTime endTime:endTime];
  224. }
  225. @end