TCVideoEditViewController.m 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. //
  2. // TCVideoEditViewController.m
  3. // TCLVBIMDemo
  4. //
  5. // Created by xiang zhang on 2017/4/10.
  6. // Copyright © 2017年 tencent. All rights reserved.
  7. //
  8. #import "TCVideoEditViewController.h"
  9. #import <TXLiteAVSDK_Professional/TXVideoEditer.h>
  10. //#import "TXVideoEditer.h"
  11. #import <MediaPlayer/MPMediaPickerController.h>
  12. #import <AVFoundation/AVFoundation.h>
  13. #import "TCVideoRangeSlider.h"
  14. #import "TCVideoRangeConst.h"
  15. #import "TCVideoPublishController.h"
  16. //#import "UIView+AdditionsX12.h"
  17. #import "UIColor+MLPFlatColors.h"
  18. #import "TCFilterSettingView.h"
  19. #import "TCVideoPreview.h"
  20. #import "TCBottomTabBar.h"
  21. #import "TCVideoCutView.h"
  22. #import "TCMusicMixView.h"
  23. #import "TCTextAddView.h"
  24. #import "YBVideoMusicView.h"
  25. #import "EffectSelectView.h"
  26. #import "TimeSelectView.h"
  27. #import <SDWebImage/SDWebImageDownloader.h>
  28. #import "YBProgressObj.h"
  29. #import "YBTakeSameVideoVC.h"
  30. #import <MediaPlayer/MediaPlayer.h>
  31. typedef NS_ENUM(NSInteger,ActionType) {
  32. ActionType_Save,
  33. ActionType_Publish,
  34. ActionType_Save_Publish,
  35. };
  36. typedef NS_ENUM(NSInteger,TimeType) {
  37. TimeType_Clear,
  38. TimeType_Back,
  39. TimeType_Repeat,
  40. TimeType_Speed,
  41. };
  42. @interface TCVideoEditViewController ()<TXVideoGenerateListener,TCVideoPreviewDelegate, TCVideoPreviewDelegate, TCFilterSettingViewDelegate, TCBottomTabBarDelegate, TCVideoCutViewDelegate, TCMusicMixViewDelegate, MPMediaPickerControllerDelegate, UIActionSheetDelegate,VideoEffectViewDelegate,TimeSelectViewDelegate>
  43. @property(nonatomic,strong)UIButton *backBtn;
  44. @property(nonatomic,strong)UIButton *selMusicBtn; //选择音乐
  45. @property(nonatomic,strong)UIButton *selVolumeBtn; //选择音量
  46. /** 底部按钮组合:特效、封面、滤镜、下一步 */
  47. @property(nonatomic,strong)UIView *botBtnMix;
  48. @property(nonatomic,strong)UIButton *effectBtn;
  49. @property(nonatomic,strong)UIButton *coverBtn;
  50. @property(nonatomic,strong)UIButton *filterfBtn;
  51. @property(nonatomic,strong)UIButton *nextBtn;
  52. /** 底部功能组合:botBar、videoRangeSlider、剪裁、时间特效、其他特效 */
  53. @property(nonatomic,strong)UIView *botFunctionMix;
  54. @property(nonatomic,strong)TCBottomTabBar *bottomBar;
  55. @property(nonatomic,strong) UILabel *cutTipsLabel;
  56. @property(nonatomic,strong)TCVideoCutView *videoCutView;
  57. @property(nonatomic,strong)TCFilterSettingView *filterView;
  58. @property(nonatomic,strong)TCMusicMixView *musixMixView;
  59. @property(nonatomic,strong)EffectSelectView *effectSelectView; //动效选择
  60. @property(nonatomic,strong)TimeSelectView *timeSelectView; //时间特效栏
  61. @property(nonatomic,strong)NSString *filePath;
  62. @property(nonatomic,assign)ActionType actionType;
  63. @property(nonatomic,strong)NSString *waterVideoPath;
  64. @end
  65. @implementation TCVideoEditViewController {
  66. TXVideoEditer *_ugcEdit;
  67. TCVideoPreview *_videoPreview;
  68. unsigned long long _fileSize;
  69. NSMutableArray *_cutPathList;
  70. NSString *_videoOutputPath;
  71. UIProgressView *_playProgressView;
  72. UILabel *_startTimeLabel;
  73. UILabel *_endTimeLabel;
  74. CGFloat _leftTime;
  75. CGFloat _rightTime;
  76. int _effectType;
  77. BOOL _isReverse;
  78. CGFloat _playTime;
  79. TimeType _timeType;
  80. BOOL _isRecordBg; //是否是录制开始设置的背景音乐(改收费版SDK添加)
  81. UIImage *watermark;
  82. BOOL isUploadWater;
  83. CGFloat _originVolume; //记录系统音量
  84. MPVolumeView *_volumeView;
  85. UISlider *_volumeViewSlider;
  86. BOOL _changeVolume;
  87. }
  88. static int MIXBTN_W = 55;
  89. -(void)creatVoluem {
  90. if (_volumeViewSlider == nil) {
  91. _volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, -100, 40, 40)];
  92. [self.view addSubview:_volumeView];
  93. for (UIView* newView in _volumeView.subviews) {
  94. if ([newView.class.description isEqualToString:@"MPVolumeSlider"]){
  95. _volumeViewSlider = (UISlider*)newView;
  96. break;
  97. }
  98. }
  99. }
  100. [_volumeView setHidden:NO];
  101. [_volumeView setShowsRouteButton:YES];
  102. [_volumeView setShowsVolumeSlider:YES];
  103. }
  104. -(void)recoveryVolume{
  105. if (_changeVolume) {
  106. _changeVolume = NO;
  107. _volumeViewSlider.value = _originVolume ;
  108. }
  109. }
  110. #pragma mark - get/set
  111. -(UIButton *)backBtn {
  112. if (!_backBtn) {
  113. _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  114. _backBtn.frame = CGRectMake(10, 40, 40, 40);
  115. [_backBtn setImage:[UIImage imageNamed:@"pub_back"] forState:0];
  116. [_backBtn addTarget:self action:@selector(clickBackBtn) forControlEvents:UIControlEventTouchUpInside];
  117. }
  118. return _backBtn;
  119. }
  120. - (UIButton *)selVolumeBtn {
  121. if (!_selVolumeBtn) {
  122. _selVolumeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  123. _selVolumeBtn.frame = CGRectMake(_window_width-MIXBTN_W*2-8-12, 40, MIXBTN_W, MIXBTN_W);
  124. [_selVolumeBtn setImage:[UIImage imageNamed:@"音量"] forState:0];
  125. [_selVolumeBtn setTitle:YZMsg(@"音量") forState:0];
  126. _selVolumeBtn.titleLabel.font = SYS_Font(13);
  127. [_selVolumeBtn addTarget:self action:@selector(clickSelVolumeBtn) forControlEvents:UIControlEventTouchUpInside];
  128. _selVolumeBtn = [PublicObj setUpImgDownText:_selVolumeBtn];
  129. }
  130. return _selVolumeBtn;
  131. }
  132. - (UIButton *)selMusicBtn {
  133. if (!_selMusicBtn) {
  134. _selMusicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  135. _selMusicBtn.frame = CGRectMake(_window_width-MIXBTN_W-8-12, 40, MIXBTN_W+12, MIXBTN_W);
  136. [_selMusicBtn setImage:[UIImage imageNamed:@"音乐"] forState:0];
  137. [_selMusicBtn setTitle:YZMsg(@"选音乐") forState:0];
  138. _selMusicBtn.titleLabel.font = SYS_Font(13);
  139. [_selMusicBtn addTarget:self action:@selector(clickMusicBtn) forControlEvents:UIControlEventTouchUpInside];
  140. _selMusicBtn = [PublicObj setUpImgDownText:_selMusicBtn];
  141. }
  142. return _selMusicBtn;
  143. }
  144. -(UIView *)botBtnMix{
  145. if (!_botBtnMix) {
  146. _botBtnMix = [[UIView alloc]initWithFrame:CGRectMake(0,_window_height-80-ShowDiff, _window_width, 80+ShowDiff)];
  147. _botBtnMix.backgroundColor = [UIColor clearColor];
  148. //特效
  149. _effectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  150. _effectBtn.frame = CGRectMake(8, 0, MIXBTN_W, MIXBTN_W);
  151. [_effectBtn setImage:[UIImage imageNamed:@"特效"] forState:0];
  152. [_effectBtn setTitle:YZMsg(@"特效") forState:0];
  153. _effectBtn.titleLabel.font = SYS_Font(13);
  154. [_effectBtn addTarget:self action:@selector(clickEffectBtn) forControlEvents:UIControlEventTouchUpInside];
  155. _effectBtn = [PublicObj setUpImgDownText:_effectBtn];
  156. [_botBtnMix addSubview:_effectBtn];
  157. //封面(隐藏了 CGRectMake(_effectBtn.right+3, 0, MIXBTN_W, MIXBTN_W);)
  158. _coverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  159. _coverBtn.frame = CGRectMake(_effectBtn.right+3, 0, 0, 0);
  160. [_coverBtn setImage:[UIImage imageNamed:@"封面"] forState:0];
  161. [_coverBtn setTitle:YZMsg(@"封面") forState:0];
  162. _coverBtn.titleLabel.font = SYS_Font(13);
  163. [_coverBtn addTarget:self action:@selector(clickCovertBtn) forControlEvents:UIControlEventTouchUpInside];
  164. _coverBtn = [PublicObj setUpImgDownText:_coverBtn];
  165. [_botBtnMix addSubview:_coverBtn];
  166. _coverBtn.hidden = YES;
  167. //滤镜
  168. _filterfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  169. _filterfBtn.frame = CGRectMake(_coverBtn.right+3, 0, MIXBTN_W, MIXBTN_W);
  170. [_filterfBtn setImage:[UIImage imageNamed:@"滤镜"] forState:0];
  171. [_filterfBtn setTitle:YZMsg(@"滤镜") forState:0];
  172. _filterfBtn.titleLabel.font = SYS_Font(13);
  173. [_filterfBtn addTarget:self action:@selector(clickFilterBtn) forControlEvents:UIControlEventTouchUpInside];
  174. _filterfBtn = [PublicObj setUpImgDownText:_filterfBtn];
  175. [_botBtnMix addSubview:_filterfBtn];
  176. //下一步
  177. _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  178. _nextBtn.frame = CGRectMake(_window_width-10-65, 12.5, 65, 30);
  179. [_nextBtn setTitle:YZMsg(@"下一步") forState:0];
  180. _nextBtn.titleLabel.font = SYS_Font(14);
  181. [_nextBtn setTitleColor:[UIColor whiteColor] forState:0];
  182. _nextBtn.backgroundColor = Pink_Cor;
  183. _nextBtn.layer.masksToBounds = YES;
  184. _nextBtn.layer.cornerRadius = 15;
  185. [_nextBtn addTarget:self action:@selector(clickNextBtn) forControlEvents:UIControlEventTouchUpInside];
  186. [_botBtnMix addSubview:_nextBtn];
  187. }
  188. return _botBtnMix;
  189. }
  190. -(UIView *)botFunctionMix {
  191. if (!_botFunctionMix) {
  192. CGFloat botFunH = _window_height/2;
  193. _botFunctionMix = [[UIView alloc]initWithFrame:CGRectMake(0, _window_height-botFunH-ShowDiff, _window_width, botFunH+ShowDiff)];
  194. _botFunctionMix.backgroundColor = [UIColor clearColor];
  195. //底部bar
  196. _bottomBar = [[TCBottomTabBar alloc] initWithFrame:CGRectMake(0, botFunH - 40 * kScaleY, _window_width, 40 * kScaleY+ShowDiff)];
  197. _bottomBar.delegate = self;
  198. [_botFunctionMix addSubview:_bottomBar];
  199. CGFloat selectViewHeight = _window_height >= 667 ? 120 * kScaleY : 120 * kScaleY;
  200. UIView *alphaV = [[UIView alloc]initWithFrame:CGRectMake(0, _bottomBar.top-selectViewHeight, _window_width, selectViewHeight)];
  201. alphaV.backgroundColor = [UIColor blackColor];
  202. alphaV.alpha = 0.4;
  203. [_botFunctionMix addSubview:alphaV];
  204. _cutTipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _bottomBar.top - selectViewHeight, _window_width, selectViewHeight)];
  205. _cutTipsLabel.textAlignment = NSTextAlignmentCenter;
  206. _cutTipsLabel.text = YZMsg(@"请拖拽两侧滑块选择裁剪区域");
  207. _cutTipsLabel.textColor = [UIColor whiteColor];
  208. _cutTipsLabel.font = [UIFont systemFontOfSize:16];
  209. [_cutTipsLabel setAdjustsFontSizeToFitWidth:YES];
  210. [_botFunctionMix addSubview:_cutTipsLabel];
  211. _videoCutView = [[TCVideoCutView alloc] initWithFrame:CGRectMake(0,40, _window_width, botFunH-_bottomBar.height-selectViewHeight-40) videoPath:_videoPath videoAssert:_videoAsset];
  212. _videoCutView.delegate = self;
  213. //_videoCutView.backgroundColor = [UIColor grayColor];
  214. [_botFunctionMix addSubview:_videoCutView];
  215. _timeSelectView = [[TimeSelectView alloc] initWithFrame:CGRectMake(0, _bottomBar.top - selectViewHeight, _window_width, selectViewHeight)];
  216. //_timeSelectView.backgroundColor = [UIColor yellowColor];
  217. _timeSelectView.delegate = self;
  218. _timeSelectView.isAlbum = _isAlbum;
  219. _effectSelectView = [[EffectSelectView alloc] initWithFrame:_timeSelectView.frame];
  220. //_effectSelectView.backgroundColor = [UIColor greenColor];
  221. _effectSelectView.delegate = self;
  222. }
  223. return _botFunctionMix;
  224. }
  225. - (TCMusicMixView *)musixMixView {
  226. if (!_musixMixView) {
  227. //haveBGM这里初始值一旦为YES“原声”再不可编辑
  228. _musixMixView = [[TCMusicMixView alloc] initWithFrame:CGRectMake(0, _window_height-ShowDiff-215, _window_width, 215+ShowDiff)haveBgm:_haveBGM];
  229. _musixMixView.delegate = self;
  230. _musixMixView.backgroundColor = [UIColor clearColor];
  231. }
  232. return _musixMixView;
  233. }
  234. -(TCFilterSettingView *)filterView {
  235. if (!_filterView) {
  236. _filterView = [[TCFilterSettingView alloc] initWithFrame:CGRectMake(0, _window_height-ShowDiff-90, _window_width, 90+ShowDiff)];
  237. _filterView.delegate = self;
  238. }
  239. return _filterView;
  240. }
  241. #pragma mark - 按钮点击事件
  242. -(void)showBotBtnMix{
  243. _musixMixView.hidden = YES;
  244. _botFunctionMix.hidden = YES;
  245. _filterView.hidden = YES;
  246. _botBtnMix.hidden = NO;
  247. }
  248. - (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  249. if (_botFunctionMix.hidden == NO ||_filterView.hidden == NO ||_musixMixView.hidden == NO) {
  250. UITouch *touch = [[event allTouches] anyObject];
  251. CGPoint _touchPoint = [touch locationInView:self.view];
  252. if (YES == CGRectContainsPoint(CGRectMake(0, 64, _window_width, _window_height*2/3), _touchPoint)) {
  253. [self showBotBtnMix];
  254. }
  255. }else{
  256. //视频暂停
  257. [_videoPreview setPlayBtn:_videoPreview.isPlaying];
  258. [_videoPreview playVideo];
  259. }
  260. }
  261. -(void)clickBackBtn {
  262. //[_ugcEdit setBGM:nil result:nil];
  263. YBWeakSelf;
  264. NSDictionary *contentDic = @{@"title":YZMsg(@"提示"),@"msg":YZMsg(@"是否退出视频编辑"),@"left":YZMsg(@"取消"),@"right":YZMsg(@"确定")};
  265. [YBAlertView showAlertView:contentDic complete:^(int eventType) {
  266. if (eventType == 1) {
  267. [weakSelf sureExit];
  268. }
  269. }];
  270. }
  271. -(void)sureExit {
  272. [self recoveryVolume];
  273. [self pauseBack];
  274. //[self.navigationController popToRootViewControllerAnimated:YES];
  275. if (_isTakeSame == YES) {
  276. //拍同款返回到同款音乐视频列表
  277. BOOL findVC = NO;
  278. for (UIViewController *forVC in self.navigationController.childViewControllers) {
  279. if ([forVC isKindOfClass:[YBTakeSameVideoVC class]]) {
  280. findVC = YES;
  281. [self.navigationController popToViewController:forVC animated:YES];
  282. break;
  283. }
  284. }
  285. if (findVC == NO) {
  286. [self.navigationController popToRootViewControllerAnimated:YES];
  287. }
  288. }else if (![PublicObj checkNull:[YBActiveManager shareManager].activeId]){
  289. if ([YBActiveManager shareManager].startVC) {
  290. YBBaseViewController *gobackVC = [YBActiveManager shareManager].startVC;
  291. [self.navigationController popToViewController:gobackVC animated:YES];
  292. [[YBActiveManager shareManager] resetActive];
  293. }else {
  294. [self.navigationController popToRootViewControllerAnimated:YES];
  295. }
  296. }else {
  297. [self.navigationController popToRootViewControllerAnimated:YES];
  298. }
  299. }
  300. -(void)clickSelVolumeBtn {
  301. [self showBotBtnMix];
  302. //选择音量-剪辑音乐
  303. [self cutMusic];
  304. }
  305. -(void)clickMusicBtn {
  306. //暂停播放
  307. [self onVideoPause];
  308. [self showBotBtnMix];
  309. //更改音乐
  310. YBVideoMusicView *mVC = [[YBVideoMusicView alloc]init];
  311. mVC.fromWhere = @"edit";
  312. __weak TCVideoEditViewController *weakSelf = self;
  313. mVC.pathEvent = ^(NSString *event, NSString *musicID) {
  314. weakSelf.musicPath = event;
  315. weakSelf.musicID = musicID;
  316. //删除时editView隐藏重新选择后显示
  317. weakSelf.musixMixView.editView.hidden = NO;
  318. //重新选择后_filePath 不再是delate状态将其置空
  319. weakSelf.filePath = nil;
  320. [self cutMusic];
  321. };
  322. YBNavigationController *nav = [[YBNavigationController alloc]initWithRootViewController:mVC];
  323. nav.modalPresentationStyle = 0;
  324. [self presentViewController:nav animated:YES completion:nil];
  325. }
  326. -(void)clickEffectBtn {
  327. //特效-展示botbar
  328. self.botFunctionMix.hidden = NO;
  329. self.botBtnMix.hidden = YES;
  330. }
  331. -(void)clickCovertBtn {
  332. //封面
  333. [MBProgressHUD showPop:YZMsg(@"敬请期待")];
  334. }
  335. -(void)clickFilterBtn {
  336. //滤镜
  337. self.filterView.hidden = NO;
  338. self.botBtnMix.hidden = YES;
  339. }
  340. -(void)clickNextBtn {
  341. //下一步
  342. [self pause];
  343. // if (_fileSize > 200 * 1024 * 1024) {
  344. // [MBProgressHUD showPop:@"视频文件过大,超过200M!"];
  345. // return;
  346. // }
  347. YBWeakSelf;
  348. RKActionSheet *sheet = [[RKActionSheet alloc]initWithTitle:@""];
  349. [sheet addActionWithType:RKSheet_Default andTitle:YZMsg(@"仅发布") complete:^{
  350. weakSelf.actionType = ActionType_Publish;
  351. [weakSelf clickEvent];
  352. }];
  353. [sheet addActionWithType:RKSheet_Default andTitle:YZMsg(@"仅保存") complete:^{
  354. weakSelf.actionType = ActionType_Save;
  355. [weakSelf clickEvent];
  356. }];
  357. [sheet addActionWithType:RKSheet_Default andTitle:YZMsg(@"保存并发布") complete:^{
  358. weakSelf.actionType = ActionType_Save_Publish;
  359. [weakSelf clickEvent];
  360. }];
  361. [sheet addActionWithType:RKSheet_Cancle andTitle:YZMsg(@"取消") complete:^{
  362. }];
  363. [sheet showSheet];
  364. }
  365. -(void)clickEvent {
  366. YBWeakSelf;
  367. [[YBProgressObj progressManeger]setUpViewCancelHidden:NO andComplete:^{
  368. [weakSelf onGenerateCancelBtnClicked:nil];
  369. }];
  370. [YBProgressObj progressManeger].generationHidden = NO;
  371. [_ugcEdit setCutFromTime:_leftTime toTime:_rightTime];
  372. [self checkVideoOutputPath];
  373. //VIDEO_COMPRESSED_540P
  374. [_ugcEdit generateVideo:VIDEO_COMPRESSED_720P videoOutputPath:_videoOutputPath];
  375. [self onVideoPause];
  376. [_videoPreview setPlayBtn:NO];
  377. }
  378. - (void)checkVideoOutputPath{
  379. NSFileManager *manager = [[NSFileManager alloc] init];
  380. if ([manager fileExistsAtPath:_videoOutputPath]) {
  381. BOOL success = [manager removeItemAtPath:_videoOutputPath error:nil];
  382. if (success) {
  383. NSLog(@"Already exist. Removed!");
  384. }
  385. }
  386. }
  387. -(void)didReceiveMemoryWarning {
  388. [super didReceiveMemoryWarning];
  389. NSLog(@"rk20200118===编辑====内存内存内存=======");
  390. //[MBProgressHUD showError:@"TC Memory"];
  391. }
  392. //返回、音量、音乐保持最前面
  393. -(void)keepViewFront{
  394. [self.view bringSubviewToFront:self.backBtn];
  395. [self.view bringSubviewToFront:self.selVolumeBtn];
  396. [self.view bringSubviewToFront:self.selMusicBtn];
  397. }
  398. -(instancetype)init {
  399. self = [super init];
  400. if (self) {
  401. _cutPathList = [NSMutableArray array];
  402. _videoOutputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"outputCut.mp4"];
  403. _waterVideoPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"outputCut_water.mp4"];
  404. _effectType = -1;
  405. // _videoTextInfos = [NSMutableArray new];
  406. [self getWaterImage];
  407. }
  408. return self;
  409. }
  410. - (void)getWaterImage{
  411. NSString *waterUrl = [[NSUserDefaults standardUserDefaults] objectForKey:@"watermarklogo"];
  412. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:waterUrl] options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
  413. watermark = image;
  414. }];
  415. }
  416. - (void)viewWillAppear:(BOOL)animated {
  417. [super viewWillAppear:animated];
  418. [UIApplication sharedApplication].statusBarHidden = YES;
  419. }
  420. - (void)viewWillDisappear:(BOOL)animated {
  421. [super viewWillDisappear:animated];
  422. [UIApplication sharedApplication].statusBarHidden = NO;
  423. }
  424. - (void)viewDidDisappear:(BOOL)animated {
  425. [super viewDidDisappear:animated];
  426. }
  427. - (void)dealloc {
  428. [_videoPreview removeNotification];
  429. _videoPreview = nil;
  430. NSLog(@"rk20200118===编辑===dealloc");
  431. }
  432. - (void)viewDidLoad {
  433. [super viewDidLoad];
  434. [self creatVoluem];
  435. self.naviView.hidden = YES;
  436. self.navigationController.interactivePopGestureRecognizer.enabled = NO;
  437. self.navigationController.interactivePopGestureRecognizer.delegate =nil;
  438. self.view.backgroundColor = UIColor.blackColor;
  439. self.navigationController.navigationBarHidden = YES;
  440. _isRecordBg = _haveBGM;
  441. if (_videoAsset == nil && _videoPath != nil) {
  442. NSURL *avUrl = [NSURL fileURLWithPath:_videoPath];
  443. _videoAsset = [AVAsset assetWithURL:avUrl];
  444. }
  445. //返回、音量、音乐
  446. [self.view addSubview:self.backBtn];
  447. [self.view addSubview:self.selVolumeBtn];
  448. [self.view addSubview:self.selMusicBtn];
  449. if (_recordType == RecordType_Chorus) {
  450. _selVolumeBtn.hidden = _selMusicBtn.hidden = YES;
  451. }
  452. _videoPreview = [[TCVideoPreview alloc] initWithFrame:CGRectMake(0, 0, _window_width, _window_height) coverImage:nil];
  453. _videoPreview.delegate = self;
  454. [self.view addSubview:_videoPreview];
  455. _playProgressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, _videoPreview.top+2, _window_width, 6)];
  456. _playProgressView.trackTintColor = UIColorFromRGB(0xd8d8d8);
  457. _playProgressView.progressTintColor = Pink_Cor;//UIColorFromRGB(0x0accac);
  458. [self.view addSubview:_playProgressView];
  459. //保持view-btn不被遮挡
  460. [self keepViewFront];
  461. //底部按钮区域
  462. [self.view addSubview:self.botBtnMix];
  463. //底部功能区域
  464. [self.view addSubview:self.botFunctionMix];
  465. self.botFunctionMix.hidden = YES;
  466. //滤镜种类
  467. [self.view addSubview:self.filterView];
  468. self.filterView.hidden = YES;
  469. //音乐剪辑
  470. [self.view addSubview:self.musixMixView];
  471. self.musixMixView.hidden = YES;
  472. TXPreviewParam *param = [[TXPreviewParam alloc] init];
  473. param.videoView = _videoPreview.renderView;
  474. if (IS_IPHONE_X && _recordType != RecordType_Chorus ){
  475. if(!_isAlbum){
  476. param.renderMode = PREVIEW_RENDER_MODE_FILL_SCREEN;
  477. }else{
  478. param.renderMode = PREVIEW_RENDER_MODE_FILL_EDGE;
  479. }
  480. }else{
  481. param.renderMode = PREVIEW_RENDER_MODE_FILL_EDGE;
  482. }
  483. _ugcEdit = [[TXVideoEditer alloc] initWithPreview:param];
  484. _ugcEdit.generateDelegate = self;
  485. _ugcEdit.previewDelegate = _videoPreview;
  486. [_ugcEdit setVideoAsset:_videoAsset];
  487. TXVideoInfo *videoMsg = [TXVideoInfoReader getVideoInfoWithAsset:_videoAsset];
  488. _fileSize = videoMsg.fileSize;
  489. CGFloat duration = videoMsg.duration;
  490. _rightTime = duration;
  491. _endTimeLabel.text = [NSString stringWithFormat:@"%d:%02d", (int)duration / 60, (int)duration % 60];
  492. if (_isTakeSame) {
  493. //拍同款选择的 转场、本地视频要加一下音乐
  494. _musixMixView.editView.hidden = NO;
  495. _filePath = nil;
  496. [self cutMusic];
  497. self.musixMixView.hidden = YES;
  498. self.botBtnMix.hidden = NO;
  499. }
  500. }
  501. - (void)viewDidAppear:(BOOL)animated {
  502. [super viewDidAppear:animated];
  503. if ([PublicObj getSysOutputVolume] < 0.2) {
  504. _changeVolume = YES;
  505. _originVolume = [AVAudioSession sharedInstance].outputVolume;
  506. _volumeViewSlider.value = 0.3;
  507. }
  508. [_videoPreview setPlayBtn:NO];
  509. [_videoPreview playVideo];
  510. }
  511. //- (void)goBack {
  512. // [_ugcEdit setBGM:nil result:nil];
  513. // [self pause];
  514. // [self.navigationController popViewControllerAnimated:YES];
  515. //
  516. //}
  517. //rk-1-16
  518. - (void)pauseBack{
  519. [_ugcEdit setBGM:nil result:nil];
  520. [_ugcEdit pausePlay];
  521. [_videoPreview setPlayBtn:NO];
  522. [_ugcEdit stopPlay];
  523. _ugcEdit.generateDelegate = nil;
  524. _ugcEdit = nil;
  525. [_videoPreview removeNotification];
  526. [_videoPreview removeFromSuperview];
  527. _videoPreview = nil;
  528. }
  529. - (void)pause {
  530. [_ugcEdit stopPlay];
  531. [_videoPreview setPlayBtn:NO];
  532. }
  533. - (void)onGenerateCancelBtnClicked:(UIButton*)sender {
  534. isUploadWater = NO;
  535. [_ugcEdit cancelGenerate];
  536. [self destoryProgressView];
  537. }
  538. -(void)destoryProgressView {
  539. [YBProgressObj progressManeger].generationHidden = YES;
  540. [[YBProgressObj progressManeger] progressDestroy];
  541. }
  542. #pragma mark FilterSettingViewDelegate
  543. - (void)onSetFilterWithImage:(UIImage *)image {
  544. [_ugcEdit setFilter:image];
  545. }
  546. #pragma mark - BottomTabBarDelegate
  547. - (void)onCutBtnClicked {
  548. //[self pause];
  549. [_timeSelectView removeFromSuperview];
  550. [_effectSelectView removeFromSuperview];
  551. [_botFunctionMix addSubview:_videoCutView];
  552. [_botFunctionMix addSubview:_cutTipsLabel];
  553. [_videoCutView setEffectDeleteBtnHidden:YES];
  554. }
  555. - (void)onFilterBtnClicked {
  556. //[self pause];
  557. [_videoCutView removeFromSuperview];
  558. [_musixMixView removeFromSuperview];
  559. [_cutTipsLabel removeFromSuperview];
  560. [_timeSelectView removeFromSuperview];
  561. [_effectSelectView removeFromSuperview];
  562. [_botFunctionMix addSubview:_filterView];
  563. _videoCutView.videoRangeSlider.hidden = NO;
  564. }
  565. - (void)onMusicBtnClicked {
  566. //[self pause];
  567. [_filterView removeFromSuperview];
  568. [_videoCutView removeFromSuperview];
  569. [_cutTipsLabel removeFromSuperview];
  570. [_timeSelectView removeFromSuperview];
  571. [_effectSelectView removeFromSuperview];
  572. [_botFunctionMix addSubview:_musixMixView];
  573. _videoCutView.videoRangeSlider.hidden = NO;
  574. }
  575. - (void)onTextBtnClicked {
  576. //[self pause];
  577. [_filterView removeFromSuperview];
  578. [_videoCutView removeFromSuperview];
  579. [_musixMixView removeFromSuperview];
  580. [_cutTipsLabel removeFromSuperview];
  581. [_timeSelectView removeFromSuperview];
  582. [_effectSelectView removeFromSuperview];
  583. _videoCutView.videoRangeSlider.hidden = NO;
  584. }
  585. - (void)onEffectBtnClicked {
  586. [_cutTipsLabel removeFromSuperview];
  587. [_timeSelectView removeFromSuperview];
  588. [_botFunctionMix addSubview:_videoCutView];
  589. [_botFunctionMix addSubview:_effectSelectView];
  590. [_videoCutView setEffectDeleteBtnHidden:NO];
  591. }
  592. -(void)onTimeBtnClicked {
  593. [_effectSelectView removeFromSuperview];
  594. [_cutTipsLabel removeFromSuperview];
  595. [_botFunctionMix addSubview:_videoCutView];
  596. [_botFunctionMix addSubview:_timeSelectView];
  597. [_videoCutView setEffectDeleteBtnHidden:YES];
  598. }
  599. #pragma mark TXVideoGenerateListener
  600. -(void) onGenerateProgress:(float)progress {
  601. //[MBProgressHUD HUDForView:self.view].progress = progress;
  602. if (watermark) {
  603. if (isUploadWater && _actionType != ActionType_Save) {
  604. [YBProgressObj progressManeger].generationProgress = progress/2 + 0.5;
  605. }else{
  606. [YBProgressObj progressManeger].generationProgress = progress/2;
  607. }
  608. }else{
  609. [YBProgressObj progressManeger].generationProgress = progress;
  610. }
  611. NSLog(@"==正在合成:%f",progress);
  612. }
  613. -(void) onGenerateComplete:(TXGenerateResult *)result {
  614. if (result.retCode == 0) {
  615. if (_actionType == ActionType_Save_Publish) {
  616. if (watermark) {
  617. if (isUploadWater) {
  618. [self destoryProgressView];
  619. UISaveVideoAtPathToSavedPhotosAlbum(_videoOutputPath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
  620. }else{
  621. isUploadWater = YES;
  622. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  623. [_ugcEdit setWaterMark:watermark normalizationFrame:CGRectMake(0.03, 0.04, 0.25, 0)];
  624. [_ugcEdit generateVideo:VIDEO_COMPRESSED_720P videoOutputPath:_waterVideoPath];
  625. });
  626. }
  627. }else{
  628. [self destoryProgressView];
  629. UISaveVideoAtPathToSavedPhotosAlbum(_videoOutputPath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
  630. }
  631. }else if (_actionType == ActionType_Save){
  632. [self destoryProgressView];
  633. UISaveVideoAtPathToSavedPhotosAlbum(_videoOutputPath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
  634. }
  635. else if (_actionType == ActionType_Publish){
  636. if (isUploadWater) {
  637. [self destoryProgressView];
  638. [self publish:_waterVideoPath];
  639. }else{
  640. if (watermark) {
  641. isUploadWater = YES;
  642. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  643. [_ugcEdit setWaterMark:watermark normalizationFrame:CGRectMake(0.03, 0.04, 0.25, 0)];
  644. [_ugcEdit generateVideo:VIDEO_COMPRESSED_720P videoOutputPath:_waterVideoPath];
  645. });
  646. }else{
  647. [self destoryProgressView];
  648. [self publish:nil];
  649. }
  650. }
  651. }
  652. }else{
  653. [self destoryProgressView];
  654. NSDictionary *contentDic = @{@"title":YZMsg(@"视频生成失败"),@"msg":[NSString stringWithFormat:@"%@:%ld %@:%@",YZMsg(@"错误码"),(long)result.retCode,YZMsg(@"错误信息"),result.descMsg],@"left":@"",@"right":YZMsg(@"确定")};
  655. [YBAlertView showAlertView:contentDic complete:^(int eventType) {
  656. }];
  657. }
  658. }
  659. - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
  660. if (_actionType == ActionType_Save) {
  661. //[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
  662. [MBProgressHUD showPop:YZMsg(@"保存成功")];
  663. [self pauseBack];
  664. /*
  665. for (UIViewController *subVC in [[[[UIApplication sharedApplication].delegate window] rootViewController] childViewControllers]) {
  666. if ([subVC isKindOfClass:[YBVideoMusicView class]]) {
  667. [self dismissViewControllerAnimated:NO completion:^{
  668. [subVC.navigationController popToRootViewControllerAnimated:NO];
  669. }];
  670. }
  671. }
  672. */
  673. [self.navigationController popToRootViewControllerAnimated:YES];
  674. return;
  675. }
  676. if (isUploadWater) {
  677. [self publish:_waterVideoPath];
  678. }else{
  679. [self publish:nil];
  680. }
  681. }
  682. - (void)publish:(NSString *)waterPath {
  683. [self recoveryVolume];
  684. isUploadWater = NO;
  685. //[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
  686. //TXVideoInfo *videoMsg = [TXUGCVideoInfoReader getVideoInfo:_videoOutputPath];
  687. [UIApplication sharedApplication].statusBarHidden = NO;
  688. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  689. TCVideoPublishController *vc = [[TCVideoPublishController alloc] initWithPath:_videoOutputPath
  690. videoMsg:[TXVideoInfoReader getVideoInfo:_videoOutputPath]];
  691. vc.musicID = _musicID;
  692. vc.waterVideoPath = waterPath;
  693. vc.isTakeSame = _isTakeSame;
  694. vc.videoAsset = _videoAsset;
  695. [self.navigationController pushViewController:vc animated:YES];
  696. [self pauseBack];
  697. });
  698. }
  699. #pragma mark TCVideoPreviewDelegate
  700. - (void)onVideoPlay {
  701. CGFloat currentPos = _videoCutView.videoRangeSlider.currentPos;
  702. if (currentPos < _leftTime || currentPos > _rightTime)
  703. currentPos = _leftTime;
  704. if(_isReverse && currentPos != 0){
  705. [_ugcEdit startPlayFromTime:0 toTime:currentPos];
  706. }
  707. else if(_videoCutView.videoRangeSlider.rightPos != 0){
  708. [_ugcEdit startPlayFromTime:currentPos toTime:_videoCutView.videoRangeSlider.rightPos];
  709. }
  710. else{
  711. [_ugcEdit startPlayFromTime:currentPos toTime:_rightTime];
  712. }
  713. }
  714. - (void)onVideoPause {
  715. [_ugcEdit pausePlay];
  716. }
  717. - (void)onVideoResume {
  718. //[_ugcEdit resumePlay];
  719. [self onVideoPlay];
  720. }
  721. - (void)onVideoPlayProgress:(CGFloat)time {
  722. _playTime = time;
  723. _playProgressView.progress = (time - _leftTime) / (_rightTime - _leftTime);
  724. [_videoCutView setPlayTime:time];
  725. }
  726. - (void)onVideoPlayFinished {
  727. if (_effectType != -1) {
  728. [self onVideoEffectEndClick:_effectType];
  729. }else{
  730. [_ugcEdit startPlayFromTime:_leftTime toTime:_rightTime];
  731. }
  732. }
  733. - (void)onVideoEnterBackground {
  734. //[MBProgressHUD hideHUDForView:self.view animated:YES];
  735. [self onVideoPause];
  736. //视频生成中不能进后台,因为使用硬编,会导致失败
  737. if ([YBProgressObj progressManeger].isExist) {
  738. [self destoryProgressView];
  739. [_ugcEdit cancelGenerate];
  740. NSDictionary *contentDic = @{@"title":YZMsg(@"视频生成失败"),@"msg":YZMsg(@"中途切后台或则被电话,闹钟等打断导致,请重新生成"),@"left":@"",@"right":YZMsg(@"确定")};
  741. [YBAlertView showAlertView:contentDic complete:^(int eventType) {
  742. }];
  743. }
  744. }
  745. #pragma mark - MusicMixViewDelegate
  746. -(void)cutMusic{
  747. //当首次进入这里并且没有选择音乐的时候前两个条件就不能满足要求了,这时要加第三个条件(_musicPath.length > 0)
  748. if (![_musicPath isEqual:_filePath] && ![_filePath isEqual:@"delate"] && _musicPath.length > 0) {
  749. NSFileManager *managers=[NSFileManager defaultManager];
  750. NSArray *subArray = [_musicPath componentsSeparatedByString:@"*"];
  751. NSString *musicNameStr = [subArray objectAtIndex:1];
  752. NSString *singerStr = [subArray objectAtIndex:2];
  753. /*
  754. NSString *timeStr = [subArray objectAtIndex:3];
  755. NSString *songID = [subArray objectAtIndex:4];
  756. NSArray *IDArray = [songID componentsSeparatedByString:@"."];
  757. songID = [IDArray objectAtIndex:0];
  758. */
  759. TCMusicInfo* musicInfo = [TCMusicInfo new];
  760. NSURL *url = [NSURL fileURLWithPath:_musicPath];
  761. AVURLAsset *musicAsset = [AVURLAsset URLAssetWithURL:url options:nil];
  762. musicInfo.duration = musicAsset.duration.value / musicAsset.duration.timescale;
  763. musicInfo.filePath = _musicPath;
  764. musicInfo.soneName = musicNameStr;
  765. musicInfo.singerName = singerStr;
  766. __weak TCVideoEditViewController *weakSelf = self;
  767. if ([managers fileExistsAtPath:_musicPath]) {
  768. dispatch_async(dispatch_get_main_queue(), ^{
  769. weakSelf.musixMixView.editView.hidden = NO;
  770. [self saveAssetURLToFile:musicInfo assetURL:url];
  771. });
  772. }
  773. }
  774. self.musixMixView.hidden = NO;
  775. self.botBtnMix.hidden = YES;
  776. }
  777. - (void)onSetVideoVolume:(CGFloat)videoVolume musicVolume:(CGFloat)musicVolume {
  778. [_ugcEdit setVideoVolume:videoVolume];
  779. [_ugcEdit setBGMVolume:musicVolume];
  780. }
  781. - (void)onSetBGMWithFilePath:(NSString *)filePath startTime:(CGFloat)startTime endTime:(CGFloat)endTime {
  782. if (![_filePath isEqualToString:filePath]) {
  783. __weak __typeof(self) weakSelf = self;
  784. [_ugcEdit setBGM:filePath result:^(int result) {
  785. dispatch_async(dispatch_get_main_queue(), ^{
  786. if (result == -1){
  787. NSDictionary *contentDic = @{@"title":YZMsg(@"设置背景音乐失败"),@"msg":YZMsg(@"不支持当前格式的背景音乐!"),@"left":@"",@"right":YZMsg(@"确定")};
  788. [YBAlertView showAlertView:contentDic complete:^(int eventType) {
  789. }];
  790. }else{
  791. [weakSelf setBGMVolume:filePath startTime:startTime endTime:endTime];
  792. }
  793. });
  794. }];
  795. }else{
  796. [self setBGMVolume:filePath startTime:startTime endTime:endTime];
  797. }
  798. }
  799. - (void)delBGM {
  800. _musicID = @"";
  801. _musicPath = @"";
  802. _haveBGM = NO;
  803. YBWeakSelf;
  804. [_ugcEdit setBGM:nil result:^(int result) {
  805. NSLog(@"del:%d",result);
  806. dispatch_async(dispatch_get_main_queue(), ^{
  807. [weakSelf setBGMVolume:nil startTime:0 endTime:0];
  808. });
  809. }];
  810. }
  811. -(void)setBGMVolume:(NSString *)filePath startTime:(CGFloat)startTime endTime:(CGFloat)endTime {
  812. _filePath = filePath;
  813. [_ugcEdit setBGMStartTime:startTime endTime:endTime];
  814. if (_filePath == nil && _isRecordBg==NO) {
  815. CGFloat bgVolume = ([PublicObj getSysOutputVolume] < 0.2 )? 0.3 : [PublicObj getSysOutputVolume];
  816. [_ugcEdit setVideoVolume:bgVolume];
  817. }
  818. [_ugcEdit startPlayFromTime:_leftTime toTime:_rightTime];
  819. [_ugcEdit setBGMLoop:YES];
  820. [_videoPreview setPlayBtn:YES];
  821. }
  822. #pragma mark - VideoCutViewDelegate
  823. - (void)onVideoLeftCutChanged:(TCVideoRangeSlider *)sender {
  824. //[_ugcEdit pausePlay];
  825. [_videoPreview setPlayBtn:NO];
  826. [_ugcEdit previewAtTime:sender.leftPos];
  827. }
  828. - (void)onVideoRightCutChanged:(TCVideoRangeSlider *)sender {
  829. [_videoPreview setPlayBtn:NO];
  830. [_ugcEdit previewAtTime:sender.rightPos];
  831. }
  832. - (void)onVideoCutChangedEnd:(TCVideoRangeSlider *)sender {
  833. _leftTime = sender.leftPos;
  834. _rightTime = sender.rightPos;
  835. _startTimeLabel.text = [NSString stringWithFormat:@"%d:%02d", (int)sender.leftPos / 60, (int)sender.leftPos % 60];
  836. _endTimeLabel.text = [NSString stringWithFormat:@"%d:%02d", (int)sender.rightPos / 60, (int)sender.rightPos % 60];
  837. [_ugcEdit startPlayFromTime:sender.leftPos toTime:sender.rightPos];
  838. [_videoPreview setPlayBtn:YES];
  839. }
  840. - (void)onVideoCenterRepeatChanged:(TCVideoRangeSlider*)sender {
  841. [_videoPreview setPlayBtn:NO];
  842. [_ugcEdit previewAtTime:sender.centerPos];
  843. }
  844. - (void)onVideoCenterRepeatEnd:(TCVideoRangeSlider*)sender {
  845. _leftTime = sender.leftPos;
  846. _rightTime = sender.rightPos;
  847. if (_timeType == TimeType_Repeat) {
  848. TXRepeat *repeat = [[TXRepeat alloc] init];
  849. repeat.startTime = sender.centerPos;
  850. repeat.endTime = sender.centerPos + 0.5;
  851. repeat.repeatTimes = 3;
  852. [_ugcEdit setRepeatPlay:@[repeat]];
  853. [_ugcEdit setSpeedList:nil];
  854. }
  855. else if (_timeType == TimeType_Speed) {
  856. TXSpeed *speed = [[TXSpeed alloc] init];
  857. speed.startTime = sender.centerPos;
  858. speed.endTime = sender.rightPos;
  859. speed.speedLevel = SPEED_LEVEL_SLOW;
  860. [_ugcEdit setSpeedList:@[speed]];
  861. [_ugcEdit setRepeatPlay:nil];
  862. }
  863. if (_isReverse) {
  864. [_ugcEdit startPlayFromTime:sender.leftPos toTime:sender.centerPos + 1.5];
  865. }else{
  866. [_ugcEdit startPlayFromTime:sender.centerPos toTime:sender.rightPos];
  867. }
  868. [_videoPreview setPlayBtn:YES];
  869. }
  870. - (void)onVideoCutChange:(TCVideoRangeSlider *)sender seekToPos:(CGFloat)pos {
  871. _playTime = pos;
  872. [_ugcEdit previewAtTime:pos];
  873. [_videoPreview setPlayBtn:NO];
  874. _playProgressView.progress = (pos - _leftTime) / (_rightTime - _leftTime);
  875. }
  876. //- (void)onSetSpeedUp:(BOOL)isSpeedUp
  877. //{
  878. // if (isSpeedUp) {
  879. // [_ugcEdit setSpeedLevel:2.0];
  880. // } else {
  881. // [_ugcEdit setSpeedLevel:1.0];
  882. // }
  883. //}
  884. //- (void)onSetSpeedUpLevel:(CGFloat)level
  885. //{
  886. // [_ugcEdit setSpeedLevel:level];
  887. //}
  888. - (void)onSetBeautyDepth:(float)beautyDepth WhiteningDepth:(float)whiteningDepth {
  889. [_ugcEdit setBeautyFilter:beautyDepth setWhiteningLevel:whiteningDepth];
  890. }
  891. - (void)onEffectDelete {
  892. [_ugcEdit deleteLastEffect];
  893. }
  894. #pragma mark - MPMediaPickerControllerDelegate
  895. - (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
  896. NSArray *items = mediaItemCollection.items;
  897. MPMediaItem *songItem = [items objectAtIndex:0];
  898. NSURL *url = [songItem valueForProperty:MPMediaItemPropertyAssetURL];
  899. NSString* songName = [songItem valueForProperty: MPMediaItemPropertyTitle];
  900. NSString* authorName = [songItem valueForProperty:MPMediaItemPropertyArtist];
  901. NSNumber* duration = [songItem valueForKey:MPMediaItemPropertyPlaybackDuration];
  902. NSLog(@"MPMediaItemPropertyAssetURL = %@", url);
  903. TCMusicInfo* musicInfo = [TCMusicInfo new];
  904. musicInfo.duration = duration.floatValue;
  905. musicInfo.soneName = songName;
  906. musicInfo.singerName = authorName;
  907. if (mediaPicker.editing) {
  908. mediaPicker.editing = NO;
  909. dispatch_async(dispatch_get_main_queue(), ^{
  910. [self saveAssetURLToFile:musicInfo assetURL:url];
  911. });
  912. [self dismissViewControllerAnimated:YES completion:nil];
  913. }
  914. }
  915. //点击取消时回调
  916. - (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker{
  917. [self dismissViewControllerAnimated:YES completion:nil];
  918. }
  919. // 将AssetURL(音乐)导出到app的文件夹并播放
  920. - (void)saveAssetURLToFile:(TCMusicInfo*)musicInfo assetURL:(NSURL*)assetURL {
  921. [_musixMixView addMusicInfo:musicInfo];
  922. }
  923. #pragma mark - VideoEffectViewDelegate
  924. - (void)onVideoEffectBeginClick:(TXEffectType)effectType {
  925. _effectType = effectType;
  926. switch ((TXEffectType)_effectType) {
  927. case TXEffectType_ROCK_LIGHT:
  928. [_videoCutView startColoration:UIColorFromRGB(0xEC5F9B) alpha:0.7];
  929. break;
  930. case TXEffectType_DARK_DRAEM:
  931. [_videoCutView startColoration:UIColorFromRGB(0xEC8435) alpha:0.7];
  932. break;
  933. case TXEffectType_SOUL_OUT:
  934. [_videoCutView startColoration:UIColorFromRGB(0x1FBCB6) alpha:0.7];
  935. break;
  936. case TXEffectType_SCREEN_SPLIT:
  937. [_videoCutView startColoration:UIColorFromRGB(0x449FF3) alpha:0.7];
  938. break;
  939. default:
  940. break;
  941. }
  942. [_ugcEdit startEffect:(TXEffectType)_effectType startTime:_playTime];
  943. if (!_isReverse) {
  944. [_ugcEdit startPlayFromTime:_videoCutView.videoRangeSlider.currentPos toTime:_videoCutView.videoRangeSlider.rightPos];
  945. }else{
  946. [_ugcEdit startPlayFromTime:_videoCutView.videoRangeSlider.leftPos toTime:_videoCutView.videoRangeSlider.currentPos];
  947. }
  948. [_videoPreview setPlayBtn:YES];
  949. }
  950. - (void)onVideoEffectEndClick:(TXEffectType)effectType {
  951. if (_effectType != -1) {
  952. [_videoPreview setPlayBtn:NO];
  953. [_videoCutView stopColoration];
  954. [_ugcEdit stopEffect:effectType endTime:_playTime];
  955. [_ugcEdit pausePlay];
  956. _effectType = -1;
  957. }
  958. }
  959. -(void)onEffectSelDelete{
  960. //删除特效
  961. [_videoCutView.videoRangeSlider removeLastColoration];
  962. [_ugcEdit deleteLastEffect];
  963. if (_videoCutView.videoRangeSlider.colorInfos.count<=0) {
  964. _effectSelectView.delEffectBtn.hidden = YES;
  965. }
  966. }
  967. #pragma mark TimeSelectViewDelegate
  968. - (void)onVideoTimeEffectsClear {
  969. _timeType = TimeType_Clear;
  970. _isReverse = NO;
  971. [_ugcEdit setReverse:_isReverse];
  972. [_ugcEdit setRepeatPlay:nil];
  973. [_ugcEdit setSpeedList:nil];
  974. [_ugcEdit startPlayFromTime:_videoCutView.videoRangeSlider.leftPos toTime:_videoCutView.videoRangeSlider.rightPos];
  975. [_videoPreview setPlayBtn:YES];
  976. [_videoCutView setCenterPanHidden:YES];
  977. }
  978. - (void)onVideoTimeEffectsBackPlay {
  979. _timeType = TimeType_Back;
  980. _isReverse = YES;
  981. [_ugcEdit setReverse:_isReverse];
  982. [_ugcEdit setRepeatPlay:nil];
  983. [_ugcEdit setSpeedList:nil];
  984. [_ugcEdit startPlayFromTime:_videoCutView.videoRangeSlider.leftPos toTime:_videoCutView.videoRangeSlider.rightPos];
  985. NSLog(@"left:%f\n right:%f",_videoCutView.videoRangeSlider.leftPos,_videoCutView.videoRangeSlider.rightPos);
  986. [_videoPreview setPlayBtn:YES];
  987. [_videoCutView setCenterPanHidden:YES];
  988. _videoCutView.videoRangeSlider.hidden = NO;
  989. }
  990. - (void)onVideoTimeEffectsRepeat {
  991. _timeType = TimeType_Repeat;
  992. _isReverse = NO;
  993. [_ugcEdit setReverse:_isReverse];
  994. [_ugcEdit setSpeedList:nil];
  995. TXRepeat *repeat = [[TXRepeat alloc] init];
  996. repeat.startTime = _leftTime + (_rightTime - _leftTime) / 5;
  997. repeat.endTime = repeat.startTime + 0.5;
  998. repeat.repeatTimes = 3;
  999. [_ugcEdit setRepeatPlay:@[repeat]];
  1000. [_ugcEdit startPlayFromTime:_videoCutView.videoRangeSlider.leftPos toTime:_videoCutView.videoRangeSlider.rightPos];
  1001. [_videoPreview setPlayBtn:YES];
  1002. [_videoCutView setCenterPanHidden:NO];
  1003. [_videoCutView setCenterPanFrame:repeat.startTime];
  1004. }
  1005. - (void)onVideoTimeEffectsSpeed {
  1006. _timeType = TimeType_Speed;
  1007. _isReverse = NO;
  1008. [_ugcEdit setReverse:_isReverse];
  1009. [_ugcEdit setRepeatPlay:nil];
  1010. TXSpeed *speed =[[TXSpeed alloc] init];
  1011. speed.startTime = _leftTime + (_rightTime - _leftTime) * 1.5 / 5;
  1012. speed.endTime = _videoCutView.videoRangeSlider.rightPos;
  1013. speed.speedLevel = SPEED_LEVEL_SLOW;
  1014. [_ugcEdit setSpeedList:@[speed]];
  1015. [_ugcEdit startPlayFromTime:_videoCutView.videoRangeSlider.leftPos toTime:_videoCutView.videoRangeSlider.rightPos];
  1016. [_videoPreview setPlayBtn:YES];
  1017. [_videoCutView setCenterPanHidden:NO];
  1018. [_videoCutView setCenterPanFrame:speed.startTime];
  1019. }
  1020. //- (BOOL)prefersStatusBarHidden{
  1021. // return NO;
  1022. //}
  1023. @end
  1024. /*
  1025. _startTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, _playProgressView.bottom + 10 * kScaleY, 50, 12)];
  1026. _startTimeLabel.text = @"0:00";
  1027. _startTimeLabel.textAlignment = NSTextAlignmentLeft;
  1028. _startTimeLabel.font = [UIFont systemFontOfSize:12];
  1029. _startTimeLabel.textColor = UIColor.lightTextColor;
  1030. [self.view addSubview:_startTimeLabel];
  1031. _endTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.width - 15 - 50, _playProgressView.bottom + 10, 50, 12)];
  1032. _endTimeLabel.text = @"0:00";
  1033. _endTimeLabel.textAlignment = NSTextAlignmentRight;
  1034. _endTimeLabel.font = [UIFont systemFontOfSize:12];
  1035. _endTimeLabel.textColor = UIColor.lightTextColor;
  1036. [self.view addSubview:_endTimeLabel];
  1037. */
  1038. /**
  1039. - (void)saveAssetURLToFile:(TCMusicInfo*)musicInfo assetURL:(NSURL*)assetURL {
  1040. [_musixMixView addMusicInfo:musicInfo];
  1041. // AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
  1042. //
  1043. // AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:songAsset presetName:AVAssetExportPresetAppleM4A];
  1044. // NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
  1045. // exporter.outputFileType = @"com.apple.m4a-audio";
  1046. //
  1047. // [AVAssetExportSession exportPresetsCompatibleWithAsset:songAsset];
  1048. // NSString *docDir = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"LocalMusics/"];
  1049. // NSString *exportFilePath = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@.m4a", musicInfo.soneName, musicInfo.singerName]];
  1050. //
  1051. // exporter.outputURL = [NSURL fileURLWithPath:exportFilePath];
  1052. // musicInfo.filePath = exportFilePath;
  1053. //
  1054. // if ([[NSFileManager defaultManager] fileExistsAtPath:exportFilePath]) {
  1055. // [_musixMixView addMusicInfo:musicInfo];
  1056. // return;
  1057. // }
  1058. //
  1059. // MBProgressHUD* hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  1060. // hub.labelText = @"音频读取中...";
  1061. // do the export
  1062. //__weak typeof(self) weakSelf = self;
  1063. // [exporter exportAsynchronouslyWithCompletionHandler:^{
  1064. // dispatch_async(dispatch_get_main_queue(), ^{
  1065. // [MBProgressHUD hideHUDForView:self.view animated:YES];
  1066. // });
  1067. // int exportStatus = exporter.status;
  1068. // switch (exportStatus) {
  1069. // case AVAssetExportSessionStatusFailed: {
  1070. // NSLog (@"AVAssetExportSessionStatusFailed: %@", exporter.error);
  1071. // break;
  1072. //
  1073. // }
  1074. // case AVAssetExportSessionStatusCompleted: {
  1075. // NSLog(@"AVAssetExportSessionStatusCompleted: %@", exporter.outputURL);
  1076. //
  1077. // // 播放背景音乐
  1078. // dispatch_async(dispatch_get_main_queue(), ^{
  1079. // [_musixMixView addMusicInfo:musicInfo];
  1080. // });
  1081. // break;
  1082. // }
  1083. // case AVAssetExportSessionStatusUnknown: { NSLog (@"AVAssetExportSessionStatusUnknown"); break;}
  1084. // case AVAssetExportSessionStatusExporting: { NSLog (@"AVAssetExportSessionStatusExporting"); break;}
  1085. // case AVAssetExportSessionStatusCancelled: { NSLog (@"AVAssetExportSessionStatusCancelled"); break;}
  1086. // case AVAssetExportSessionStatusWaiting: { NSLog (@"AVAssetExportSessionStatusWaiting"); break;}
  1087. // default: { NSLog (@"didn't get export status"); break;}
  1088. // }
  1089. // }];
  1090. }
  1091. */