TCVideoRecordMusicView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // VideoRecordMusicView.m
  3. // TXLiteAVDemo
  4. //
  5. // Created by zhangxiang on 2017/9/13.
  6. // Copyright © 2017年 Tencent. All rights reserved.
  7. //
  8. #import "TCVideoRecordMusicView.h"
  9. #import "ColorMacro.h"
  10. #import "UIView+Additions.h"
  11. //#import "TCBGMSliderCutView.h"
  12. #define L(x) NSLocalizedString((x),nil)
  13. @interface TCVideoRecordMusicView(){
  14. }
  15. @end
  16. @implementation TCVideoRecordMusicView
  17. {
  18. UISlider *_sldVolumeForBGM;
  19. UISlider *_sldVolumeForVoice;
  20. // TCBGMSliderCutView* _musicCutSlider;
  21. UILabel* _startTimeLabel;
  22. // TCBGMSliderCutViewConfig* sliderConfig;
  23. NSMutableArray* _audioEffectArry;
  24. NSMutableArray* _audioEffectArry2;
  25. UIScrollView* _audioScrollView;
  26. UIScrollView* _audioScrollView2;
  27. }
  28. -(instancetype)initWithFrame:(CGRect)frame needEffect:(BOOL)needEffect;
  29. {
  30. self = [super initWithFrame:frame];
  31. if (self) {
  32. _audioEffectArry = [NSMutableArray arrayWithObjects:L(@"TCVideoRecordMusicView.Origin"),
  33. L(@"TCVideoRecordMusicView.KTV"),
  34. L(@"TCVideoRecordMusicView.Room"),
  35. L(@"TCVideoRecordMusicView.Hall"),
  36. L(@"TCVideoRecordMusicView.Low"),
  37. L(@"TCVideoRecordMusicView.Bright"),
  38. L(@"TCVideoRecordMusicView.Metal"),
  39. L(@"TCVideoRecordMusicView.Magnetic"),
  40. nil];
  41. _audioEffectArry2 = [NSMutableArray arrayWithObjects:L(@"TCVideoRecordMusicView.Origin"),
  42. L(@"TCVideoRecordMusicView.Child"),
  43. L(@"TCVideoRecordMusicView.Loli"),
  44. L(@"TCVideoRecordMusicView.Uncle"),
  45. L(@"TCVideoRecordMusicView.HeavyMetal"),
  46. L(@"TCVideoRecordMusicView.Foreigner"),
  47. L(@"TCVideoRecordMusicView.Beast"),
  48. L(@"TCVideoRecordMusicView.Fatty"),
  49. L(@"TCVideoRecordMusicView.StrongCurrent"),
  50. L(@"TCVideoRecordMusicView.HeavyMachinery"),
  51. L(@"TCVideoRecordMusicView.Ethereal"),
  52. nil];
  53. [self initUI:needEffect];
  54. }
  55. return self;
  56. }
  57. -(void)initUI:(BOOL)needEffect{
  58. self.backgroundColor = [UIColor clearColor];
  59. /***
  60. //混响,功能展示用,暂时先放这里
  61. if (needEffect) {
  62. CGFloat btnSpace = 10;
  63. CGFloat btnWidth = 40 * kScaleY;
  64. _audioScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 5, self.width, btnWidth)];
  65. _audioScrollView.contentSize = CGSizeMake((btnWidth + btnSpace) * _audioEffectArry.count, btnWidth);
  66. _audioScrollView.showsVerticalScrollIndicator = NO;
  67. _audioScrollView.showsHorizontalScrollIndicator = NO;
  68. for (int i=0; i<_audioEffectArry.count; ++i) {
  69. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(btnSpace +(btnWidth + btnSpace) * i, 0, btnWidth, btnWidth)];
  70. btn.titleLabel.font = [UIFont systemFontOfSize:12.f];
  71. [btn setTitle:[_audioEffectArry objectAtIndex:i] forState:UIControlStateNormal];
  72. [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  73. [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
  74. [btn setBackgroundColor:[UIColor clearColor]];
  75. [btn.layer setMasksToBounds:YES];
  76. [btn.layer setCornerRadius:btnWidth/2];
  77. [btn addTarget:self action:@selector(selectEffect:) forControlEvents:UIControlEventTouchUpInside];
  78. btn.tag = i;
  79. [_audioScrollView addSubview:btn];
  80. if (i == 0) {
  81. btn.selected = YES;
  82. [btn setBackgroundColor:[UIColor redColor]];
  83. }
  84. }
  85. //变声类型
  86. _audioScrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _audioScrollView.bottom + 5, self.width, btnWidth)];
  87. _audioScrollView2.contentSize = CGSizeMake((btnWidth + btnSpace) * _audioEffectArry2.count, btnWidth);
  88. _audioScrollView2.showsVerticalScrollIndicator = NO;
  89. _audioScrollView2.showsHorizontalScrollIndicator = NO;
  90. for (int i=0; i<_audioEffectArry2.count; ++i) {
  91. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(btnSpace +(btnWidth + btnSpace) * i, 0, btnWidth, btnWidth)];
  92. btn.titleLabel.font = [UIFont systemFontOfSize:12.f];
  93. [btn setTitle:[_audioEffectArry2 objectAtIndex:i] forState:UIControlStateNormal];
  94. [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  95. [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
  96. [btn setBackgroundColor:[UIColor clearColor]];
  97. [btn.layer setMasksToBounds:YES];
  98. [btn.layer setCornerRadius:btnWidth/2];
  99. [btn addTarget:self action:@selector(selectEffect2:) forControlEvents:UIControlEventTouchUpInside];
  100. btn.tag = i;
  101. [_audioScrollView2 addSubview:btn];
  102. if (i == 0) {
  103. btn.selected = YES;
  104. [btn setBackgroundColor:[UIColor redColor]];
  105. }
  106. }
  107. [self addSubview:_audioScrollView];
  108. [self addSubview:_audioScrollView2];
  109. }
  110. */
  111. //BGM
  112. UIButton *btnSelectBGM = [[UIButton alloc] initWithFrame:CGRectMake(15, 5, 30, 30)];
  113. [btnSelectBGM setImage:[UIImage imageNamed:@"music_change_normal"] forState:UIControlStateNormal];
  114. [btnSelectBGM setImage:[UIImage imageNamed:@"music_change_press"] forState:UIControlStateHighlighted];
  115. [btnSelectBGM addTarget:self action:@selector(onBtnMusicSelected) forControlEvents:UIControlEventTouchUpInside];
  116. btnSelectBGM.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
  117. UIButton *btnStopBGM = [[UIButton alloc] initWithFrame:CGRectMake(btnSelectBGM.width + 30, 5, 30, 30)];
  118. [btnStopBGM setImage:[UIImage imageNamed:@"music_delete_normal"] forState:UIControlStateNormal];
  119. [btnStopBGM setImage:[UIImage imageNamed:@"music_delete_press"] forState:UIControlStateHighlighted];
  120. [btnStopBGM addTarget:self action:@selector(onBtnMusicStoped) forControlEvents:UIControlEventTouchUpInside];
  121. btnStopBGM.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
  122. UILabel *labVolumeForVoice = [[UILabel alloc] initWithFrame:CGRectMake(15, btnSelectBGM.bottom + 10, 80, 16)];
  123. [labVolumeForVoice setText:L(@"TCVideoRecordMusicView.VolumeRecord")];
  124. [labVolumeForVoice setFont:[UIFont systemFontOfSize:14.f]];
  125. labVolumeForVoice.textColor = UIColorFromRGB(0xFFFFFF);
  126. [labVolumeForVoice sizeToFit];
  127. _sldVolumeForVoice = [[UISlider alloc] initWithFrame:CGRectMake(labVolumeForVoice.left, labVolumeForVoice.bottom + 10,self.width - 30, 20)];
  128. _sldVolumeForVoice.minimumValue = 0;
  129. _sldVolumeForVoice.maximumValue = 2;
  130. _sldVolumeForVoice.value = 1;
  131. _sldVolumeForVoice.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  132. [_sldVolumeForVoice setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
  133. [_sldVolumeForVoice setMinimumTrackTintColor:RGB(238, 100, 85)];
  134. [_sldVolumeForVoice setMaximumTrackImage:[UIImage imageNamed:@"gray"] forState:UIControlStateNormal];
  135. [_sldVolumeForVoice addTarget:self action:@selector(onVoiceValueChange:) forControlEvents:UIControlEventValueChanged];
  136. UILabel *labVolumeForBGM = [[UILabel alloc] initWithFrame:CGRectMake(labVolumeForVoice.left, _sldVolumeForVoice.bottom + 20 , 80 , 16)];
  137. [labVolumeForBGM setText:L(@"TCVideoRecordMusicView.VolumeBGM")];
  138. [labVolumeForBGM sizeToFit];
  139. [labVolumeForBGM setFont:[UIFont systemFontOfSize:14.f]];
  140. labVolumeForBGM.textColor = UIColorFromRGB(0xFFFFFF);
  141. _sldVolumeForBGM = [[UISlider alloc] initWithFrame:CGRectMake(labVolumeForVoice.left, labVolumeForBGM.bottom + 10,self.width - 30, 20)];
  142. _sldVolumeForBGM.minimumValue = 0;
  143. _sldVolumeForBGM.maximumValue = 2;
  144. _sldVolumeForBGM.value = 1;
  145. _sldVolumeForBGM.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  146. [_sldVolumeForBGM setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
  147. [_sldVolumeForBGM setMinimumTrackTintColor:RGB(238, 100, 85)];
  148. [_sldVolumeForBGM setMaximumTrackImage:[UIImage imageNamed:@"gray"] forState:UIControlStateNormal];
  149. [_sldVolumeForBGM addTarget:self action:@selector(onBGMValueChange:) forControlEvents:UIControlEventValueChanged];
  150. _startTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15,_sldVolumeForBGM.bottom + 20,200,16)];
  151. [_startTimeLabel setTextColor:[UIColor whiteColor]];
  152. [_startTimeLabel setFont:[UIFont systemFontOfSize:14.f]];
  153. // [_startTimeLabel setText:[NSString stringWithFormat:L(@"TCVideoRecordMusicView.StartFrom"),[TCBGMSliderCutView timeString:0]]];
  154. [self addSubview:btnSelectBGM];
  155. [self addSubview:btnStopBGM];
  156. [self addSubview:labVolumeForBGM];
  157. [self addSubview:_sldVolumeForBGM];
  158. [self addSubview:labVolumeForVoice];
  159. [self addSubview:_sldVolumeForVoice];
  160. [self addSubview:_startTimeLabel];
  161. [self freshCutView:150];
  162. }
  163. -(void) freshCutView:(CGFloat) duration{
  164. dispatch_async(dispatch_get_main_queue(), ^(){
  165. // [_musicCutSlider removeFromSuperview];
  166. //1.thumbHeight + 2* borderHeight =_musicCutSlider.frame.y;
  167. //2._musicCutSlider.frame.y目前只支持40
  168. // sliderConfig = [TCBGMSliderCutViewConfig new];
  169. // sliderConfig.duration = duration;
  170. // sliderConfig.frame = CGRectMake(15, _startTimeLabel.bottom + 10, self.width - 30, 54);
  171. // _musicCutSlider = [[TCBGMSliderCutView alloc] initWithImage:[UIImage imageNamed:@"wave_chosen"] config:sliderConfig];
  172. // _musicCutSlider.delegate = self;
  173. // [self addSubview:_musicCutSlider];
  174. });
  175. }
  176. -(void)onBtnMusicSelected
  177. {
  178. if (_delegate && [_delegate respondsToSelector:@selector(onBtnMusicSelected)]) {
  179. [_delegate onBtnMusicSelected];
  180. }
  181. }
  182. //-(void) resetCutView{
  183. // [_musicCutSlider resetCutView];
  184. //}
  185. - (void)selectEffect:(UIButton *)button {
  186. for(UIView *view in _audioScrollView.subviews){
  187. if ([view isKindOfClass:[UIButton class]]) {
  188. UIButton *btn = (UIButton *)view;
  189. btn.selected = NO;
  190. [btn setBackgroundColor:[UIColor clearColor]];
  191. }
  192. }
  193. button.selected = YES;
  194. [button setBackgroundColor:[UIColor redColor]];
  195. if (self.delegate) [self.delegate selectAudioEffect:button.tag];
  196. }
  197. /// 变声类型
  198. - (void)selectEffect2:(UIButton *)button {
  199. for(UIButton *view in _audioScrollView2.subviews){
  200. if ([view isKindOfClass:[UIButton class]]) {
  201. UIButton *btn = (UIButton *)view;
  202. btn.selected = NO;
  203. [btn setBackgroundColor:[UIColor clearColor]];
  204. }
  205. }
  206. button.selected = YES;
  207. [button setBackgroundColor:[UIColor redColor]];
  208. if (self.delegate) [self.delegate selectAudioEffect2:button.tag >= 5 ? button.tag + 1 : button.tag];
  209. }
  210. -(void)onBtnMusicStoped
  211. {
  212. if (_delegate && [_delegate respondsToSelector:@selector(onBtnMusicStoped)]) {
  213. [_delegate onBtnMusicStoped];
  214. }
  215. }
  216. -(void)onBGMValueChange:(UISlider*)slider
  217. {
  218. if (_delegate && [_delegate respondsToSelector:@selector(onBGMValueChange:)]) {
  219. [_delegate onBGMValueChange:slider.value];
  220. }
  221. }
  222. -(void)onVoiceValueChange:(UISlider*)slider
  223. {
  224. if (_delegate && [_delegate respondsToSelector:@selector(onVoiceValueChange:)]) {
  225. [_delegate onVoiceValueChange:slider.value];
  226. }
  227. }
  228. #pragma mark - RangeContentDelegate
  229. //- (void)onRangeLeftChanged:(TCBGMSliderCutView*)sender percent:(CGFloat)percent{
  230. // if(sliderConfig){
  231. // [_startTimeLabel setText:[NSString stringWithFormat:L(@"TCVideoRecordMusicView.StartFrom"),[TCBGMSliderCutView timeString:percent*sliderConfig.duration]]];
  232. // }
  233. // else{
  234. // [_startTimeLabel setText:[NSString stringWithFormat:L(@"TCVideoRecordMusicView.StartFrom"),[TCBGMSliderCutView timeString:0]]];
  235. // }
  236. //}
  237. //
  238. //- (void)onRangeLeftChangeEnded:(TCBGMSliderCutView*)sender percent:(CGFloat)percent
  239. //{
  240. //// NSLog(@"end:%f",percent*sliderConfig.duration);
  241. // if (_delegate && [_delegate respondsToSelector:@selector(onBGMRangeChange:endPercent:)]) {
  242. // [_delegate onBGMRangeChange:_musicCutSlider.leftScale endPercent:_musicCutSlider.rightScale];
  243. // }
  244. //}
  245. - (void)onRangeRightChangeEnded:(id)sender
  246. {
  247. // NSLog(@"left:%f right:%f",_musicCutSlider.leftScale, _musicCutSlider.rightScale);
  248. if (_delegate && [_delegate respondsToSelector:@selector(onBGMRangeChange:endPercent:)]) {
  249. // [_delegate onBGMRangeChange:_musicCutSlider.leftScale endPercent:_musicCutSlider.rightScale];
  250. }
  251. }
  252. -(void)resetVolume
  253. {
  254. _sldVolumeForBGM.value = 1;
  255. _sldVolumeForVoice.value = 1;
  256. }
  257. @end