TCVideoRecordViewController.m 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  1. #import <Foundation/Foundation.h>
  2. #import <AVFoundation/AVFoundation.h>
  3. #import <MediaPlayer/MPMediaPickerController.h>
  4. #import "TCVideoRecordViewController.h"
  5. #import "TCVideoPublishController.h"
  6. //#import "TCVideoEditViewController.h"
  7. #import "TCVideoRecordMusicView.h"
  8. #import "TCVideoRecordProcessView.h"
  9. #import "V8HorizontalPickerView.h"
  10. #import <MBProgressHUD/MBProgressHUD.h>
  11. //#import "TCBGMListViewController.h"
  12. #import "BeautySettingPanel.h"
  13. #import "MBProgressHUD.h"
  14. #import "UIAlertView+BlocksKit.h"
  15. #import "SoundMixView.h"
  16. #import "Label.h"
  17. #import <Masonry/Masonry.h>
  18. #import "new_BGMListController.h"
  19. #import "MGTCVideoRecordManager.h"
  20. #import <TXLiteAVSDK_Professional/TXUGCRecordListener.h>
  21. #import <TXLiteAVSDK_Professional/TXVideoEditerListener.h>
  22. #import <TXLiteAVSDK_Professional/TXVideoEditer.h>
  23. #import <TXLiteAVSDK_Professional/TXUGCRecord.h>
  24. #define BUTTON_RECORD_SIZE 75
  25. #define BUTTON_CONTROL_SIZE 40
  26. #define BUTTON_PROGRESS_HEIGHT 3
  27. #define BUTTON_MASK_HEIGHT 170
  28. #define BUTTON_SPEED_HEIGHT 34
  29. #define BUTTON_SPEED_INTERVAL 30
  30. #define BUTTON_SPEED_COUNT 5
  31. #define BUTTON_SPEED_CHANGE_WIDTH 50
  32. #define BUTTON_SPEED_CHANGE_HEIGHT 34
  33. typedef NS_ENUM(NSInteger,SpeedMode)
  34. {
  35. SpeedMode_VerySlow,
  36. SpeedMode_Slow,
  37. SpeedMode_Standard,
  38. SpeedMode_Quick,
  39. SpeedMode_VeryQuick,
  40. };
  41. typedef NS_ENUM(NSInteger,RecordType)
  42. {
  43. RecordType_Normal,
  44. RecordType_Chorus,
  45. };
  46. typedef NS_ENUM(NSInteger,CaptureMode)
  47. {
  48. CaptureModeStill,
  49. CaptureModeTap,
  50. CaptureModePress
  51. };
  52. @implementation RecordMusicInfo
  53. @end
  54. #if POD_PITU
  55. #import "MCCameraDynamicView.h"
  56. #import "MaterialManager.h"
  57. #import "MCTip.h"
  58. @interface TCVideoRecordViewController () <MCCameraDynamicDelegate,VideoRecordMusicViewDelegate,BeautySettingPanelDelegate,BeautyLoadPituDelegate, SoundMixViewDelegate>
  59. @end
  60. #endif
  61. @interface TCVideoRecordViewController()<TXUGCRecordListener,V8HorizontalPickerViewDelegate,V8HorizontalPickerViewDataSource,MPMediaPickerControllerDelegate,TXVideoJoinerListener>
  62. {
  63. BOOL _cameraFront;
  64. BOOL _lampOpened;
  65. BOOL _vBeautyShow;
  66. CGSize _size;
  67. int _fps;
  68. TXAudioSampleRate _sampleRate;
  69. TXVideoBeautyStyle _beautyStyle;
  70. float _beautyDepth;
  71. float _whitenDepth;
  72. float _ruddinessDepth;
  73. float _eye_level;
  74. float _face_level;
  75. BOOL _cameraPreviewing;
  76. BOOL _videoRecording;
  77. UIView * _videoRecordView;
  78. UIView * _videoPlayView;
  79. UIButton * _btnTorch;
  80. CGFloat _currentRecordTime;
  81. UIButton * _btnRatio;
  82. V8HorizontalPickerView * _filterPickerView;
  83. NSMutableArray * _filterArray;
  84. NSInteger _filterIndex;
  85. SoundMixView *_soundMixView;
  86. BOOL _navigationBarHidden;
  87. BOOL _statusBarHidden;
  88. BOOL _appForeground;
  89. BOOL _isPaused;
  90. UIButton *_motionBtn;
  91. UIButton * __weak * _ratioMenuButtonIvarPtr;
  92. // 倒计时
  93. UILabel *_countDownLabel;
  94. UIView *_countDownView;
  95. NSTimer *_countDownTimer;
  96. #if POD_PITU
  97. MCCameraDynamicView *_tmplBar;
  98. NSString *_materialID;
  99. #else
  100. UIView *_tmplBar;
  101. #endif
  102. V8HorizontalPickerView *_greenPickerView;
  103. NSMutableArray *_greenArray;
  104. // TCBGMListViewController* _bgmListVC;
  105. UIButton *_speedChangeBtn;
  106. NSInteger _greenIndex;;
  107. NSMutableArray * _speedBtnList;
  108. NSObject* _BGMPath;
  109. CGFloat _BGMDuration;
  110. CGFloat _recordTime;
  111. int _deleteCount;
  112. float _zoom;
  113. BOOL _isBackDelete;
  114. BOOL _isFlash;
  115. TCVideoRecordMusicView * _musicView;
  116. TXVideoAspectRatio _aspectRatio;
  117. SpeedMode _speedMode;
  118. BeautySettingPanel * _vBeauty;
  119. MBProgressHUD* _hub;
  120. CGFloat _bgmBeginTime;
  121. BOOL _bgmRecording;
  122. TXVideoEditer * _videoEditer;
  123. TXVideoJoiner * _videoJoiner;
  124. RecordType _recordType;
  125. NSString * _recordVideoPath;
  126. NSString * _joinVideoPath;
  127. TXVideoVoiceChangerType _voiceChangeType; // 变声参数
  128. NSInteger _soundMixChangeType; // 混音参数
  129. }
  130. @property (weak, nonatomic) IBOutlet UILabel *stillModeLabel;
  131. @property (weak, nonatomic) IBOutlet UILabel *tapModeLabel;
  132. @property (weak, nonatomic) IBOutlet UILabel *pressModeLabel;
  133. @property (weak, nonatomic) IBOutlet Label *recordTimeLabel;
  134. @property (weak, nonatomic) IBOutlet UIButton *btnNext;
  135. @property (weak, nonatomic) IBOutlet UIButton *btnMusic;
  136. @property (weak, nonatomic) IBOutlet UIButton *btnRatio169;
  137. @property (weak, nonatomic) IBOutlet UIButton *btnRatio43;
  138. @property (weak, nonatomic) IBOutlet UIButton *btnRatio11;
  139. @property (weak, nonatomic) IBOutlet UIButton *btnRatioMenu;
  140. @property (weak, nonatomic) IBOutlet UIButton *btnBeauty;
  141. @property (weak, nonatomic) IBOutlet UIButton *btnAudioMix;
  142. @property (weak, nonatomic) IBOutlet UIButton *btnCountDown;
  143. @property (weak, nonatomic) IBOutlet UIButton *btnStartRecord;
  144. @property (weak, nonatomic) IBOutlet UIButton *btnFlash;
  145. @property (weak, nonatomic) IBOutlet UIButton *btnCamera;
  146. @property (weak, nonatomic) IBOutlet UIButton *btnDelete;
  147. @property (weak, nonatomic) IBOutlet UIButton *beautyBtn;
  148. @property (weak, nonatomic) IBOutlet UIButton *filterBtn;
  149. @property (weak, nonatomic) IBOutlet UIView *speedView;
  150. @property (weak, nonatomic) IBOutlet UIView *captureModeView;
  151. @property (weak, nonatomic) IBOutlet UIView *bottomMask;
  152. @property (weak, nonatomic) IBOutlet TCVideoRecordProcessView *progressView;
  153. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomMaskHeight;
  154. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *centerConstraint;
  155. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *speedViewHeight;
  156. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *ratioTopConstraint;
  157. @property (assign, nonatomic) CaptureMode captureMode;
  158. @end
  159. @implementation TCVideoRecordViewController
  160. -(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  161. {
  162. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  163. if (self)
  164. {
  165. _appForeground = YES;
  166. _cameraFront = YES;
  167. _lampOpened = NO;
  168. _vBeautyShow = NO;
  169. _beautyStyle = VIDOE_BEAUTY_STYLE_SMOOTH;
  170. _beautyDepth = 6.3;
  171. _whitenDepth = 2.7;
  172. _cameraPreviewing = NO;
  173. _videoRecording = NO;
  174. _currentRecordTime = 0;
  175. _sampleRate = AUDIO_SAMPLERATE_48000;
  176. _speedMode = SpeedMode_Standard;
  177. _voiceChangeType = -1; // 无变声
  178. _soundMixChangeType = -1; // 无混音效果
  179. _greenArray = [NSMutableArray new];
  180. [_greenArray addObject:({
  181. V8LabelNode *v = [V8LabelNode new];
  182. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label1", nil);
  183. v.file = nil;
  184. v.face = [UIImage imageNamed:@"greens_no"];
  185. v;
  186. })];
  187. [_greenArray addObject:({
  188. V8LabelNode *v = [V8LabelNode new];
  189. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label2", nil);
  190. v.file = [[NSBundle mainBundle] URLForResource:@"goodluck" withExtension:@"mp4"];;
  191. v.face = [UIImage imageNamed:@"greens_1"];
  192. v;
  193. })];
  194. [_greenArray addObject:({
  195. V8LabelNode *v = [V8LabelNode new];
  196. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label3", nil);
  197. v.file = [[NSBundle mainBundle] URLForResource:@"2gei_5" withExtension:@"mp4"];
  198. v.face = [UIImage imageNamed:@"greens_2"];
  199. v;
  200. })];
  201. _filterIndex = 0;
  202. _filterArray = [NSMutableArray new];
  203. [_filterArray addObject:({
  204. V8LabelNode *v = [V8LabelNode new];
  205. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label4", nil);
  206. v.face = [UIImage imageNamed:@"orginal"];
  207. v;
  208. })];
  209. [_filterArray addObject:({
  210. V8LabelNode *v = [V8LabelNode new];
  211. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label5", nil);
  212. v.face = [UIImage imageNamed:@"biaozhun"];
  213. v;
  214. })];
  215. [_filterArray addObject:({
  216. V8LabelNode *v = [V8LabelNode new];
  217. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label6", nil);
  218. v.face = [UIImage imageNamed:@"yinghong"];
  219. v;
  220. })];
  221. [_filterArray addObject:({
  222. V8LabelNode *v = [V8LabelNode new];
  223. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label7", nil);
  224. v.face = [UIImage imageNamed:@"yunshang"];
  225. v;
  226. })];
  227. [_filterArray addObject:({
  228. V8LabelNode *v = [V8LabelNode new];
  229. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label8", nil);
  230. v.face = [UIImage imageNamed:@"chunzhen"];
  231. v;
  232. })];
  233. [_filterArray addObject:({
  234. V8LabelNode *v = [V8LabelNode new];
  235. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label9", nil);
  236. v.face = [UIImage imageNamed:@"bailan"];
  237. v;
  238. })];
  239. [_filterArray addObject:({
  240. V8LabelNode *v = [V8LabelNode new];
  241. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label10", nil);
  242. v.face = [UIImage imageNamed:@"yuanqi"];
  243. v;
  244. })];
  245. [_filterArray addObject:({
  246. V8LabelNode *v = [V8LabelNode new];
  247. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label11", nil);
  248. v.face = [UIImage imageNamed:@"chaotuo"];
  249. v;
  250. })];
  251. [_filterArray addObject:({
  252. V8LabelNode *v = [V8LabelNode new];
  253. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label12", nil);
  254. v.face = [UIImage imageNamed:@"xiangfen"];
  255. v;
  256. })];
  257. [_filterArray addObject:({
  258. V8LabelNode *v = [V8LabelNode new];
  259. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label13", nil);
  260. v.face = [UIImage imageNamed:@"fwhite"];
  261. v;
  262. })];
  263. [_filterArray addObject:({
  264. V8LabelNode *v = [V8LabelNode new];
  265. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label14", nil);
  266. v.face = [UIImage imageNamed:@"langman"];
  267. v;
  268. })];
  269. [_filterArray addObject:({
  270. V8LabelNode *v = [V8LabelNode new];
  271. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label15", nil);
  272. v.face = [UIImage imageNamed:@"qingxin"];
  273. v;
  274. })];
  275. [_filterArray addObject:({
  276. V8LabelNode *v = [V8LabelNode new];
  277. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label16", nil);
  278. v.face = [UIImage imageNamed:@"weimei"];
  279. v;
  280. })];
  281. [_filterArray addObject:({
  282. V8LabelNode *v = [V8LabelNode new];
  283. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label17", nil);
  284. v.face = [UIImage imageNamed:@"fennen"];
  285. v;
  286. })];
  287. [_filterArray addObject:({
  288. V8LabelNode *v = [V8LabelNode new];
  289. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label18", nil);
  290. v.face = [UIImage imageNamed:@"huaijiu"];
  291. v;
  292. })];
  293. [_filterArray addObject:({
  294. V8LabelNode *v = [V8LabelNode new];
  295. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label19", nil);
  296. v.face = [UIImage imageNamed:@"landiao"];
  297. v;
  298. })];
  299. [_filterArray addObject:({
  300. V8LabelNode *v = [V8LabelNode new];
  301. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label20", nil);
  302. v.face = [UIImage imageNamed:@"qingliang"];
  303. v;
  304. })];
  305. [_filterArray addObject:({
  306. V8LabelNode *v = [V8LabelNode new];
  307. v.title = NSLocalizedString(@"TCVideoRecordView.V8Label21", nil);
  308. v.face = [UIImage imageNamed:@"rixi"];
  309. v;
  310. })];
  311. _aspectRatio = VIDEO_ASPECT_RATIO_9_16;
  312. [TXUGCRecord shareInstance].recordDelegate = self;
  313. // _bgmListVC = [[TCBGMListViewController alloc] init];
  314. // [_bgmListVC setBGMControllerListener:self];
  315. _recordVideoPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"outputRecord.mp4"];
  316. _joinVideoPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"outputJoin.mp4"];
  317. self.captureMode = CaptureModeTap;
  318. }
  319. return self;
  320. }
  321. - (void)didReceiveMemoryWarning
  322. {
  323. [super didReceiveMemoryWarning];
  324. }
  325. -(void)viewDidLoad
  326. {
  327. [super viewDidLoad];
  328. if (@available(iOS 11, *)) {
  329. self.bottomMaskHeight.constant += [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom;
  330. }
  331. _ratioMenuButtonIvarPtr = &_btnRatio169;
  332. [self initUI];
  333. [self initBeautyUI];
  334. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidEnterBackGround:) name:UIApplicationDidEnterBackgroundNotification object:nil];
  335. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
  336. [[NSNotificationCenter defaultCenter] addObserver:self
  337. selector:@selector(onAudioSessionEvent:)
  338. name:AVAudioSessionInterruptionNotification
  339. object:nil];
  340. }
  341. - (void)viewDidAppear:(BOOL)animated
  342. {
  343. [super viewDidAppear:animated];
  344. if (_musicPatch)
  345. {
  346. [self onBGMControllerPlay:_musicPatch];
  347. }
  348. }
  349. - (void)viewWillAppear:(BOOL)animated{
  350. [super viewWillAppear:animated];
  351. self.btnNext.userInteractionEnabled = YES;
  352. // _navigationBarHidden = self.navigationController.navigationBar.hidden;
  353. _statusBarHidden = [UIApplication sharedApplication].statusBarHidden;
  354. [self.navigationController setNavigationBarHidden:YES];
  355. [[UIApplication sharedApplication]setStatusBarHidden:YES];
  356. if (_cameraPreviewing == NO) {
  357. [self startCameraPreview];
  358. }else{
  359. //停止特效的声音
  360. [[TXUGCRecord shareInstance] setMotionMute:NO];
  361. }
  362. // 恢复变声与混音效果
  363. if (_voiceChangeType >= 0) {
  364. [[TXUGCRecord shareInstance] setVoiceChangerType:_voiceChangeType];
  365. }
  366. if (_soundMixChangeType >= 0) {
  367. [[TXUGCRecord shareInstance] setReverbType:_soundMixChangeType];
  368. }
  369. }
  370. - (void)viewWillDisappear:(BOOL)animated
  371. {
  372. [super viewWillDisappear:animated];
  373. // [self.navigationController setNavigationBarHidden:_navigationBarHidden];
  374. [[UIApplication sharedApplication]setStatusBarHidden:_statusBarHidden];
  375. }
  376. #pragma mark - Notification Handler
  377. -(void)onAudioSessionEvent:(NSNotification*)notification
  378. {
  379. NSDictionary *info = notification.userInfo;
  380. AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
  381. if (type == AVAudioSessionInterruptionTypeBegan) {
  382. // 在10.3及以上的系统上,分享跳其它app后再回来会收到AVAudioSessionInterruptionWasSuspendedKey的通知,不处理这个事件。
  383. if ([info objectForKey:@"AVAudioSessionInterruptionWasSuspendedKey"]) {
  384. return;
  385. }
  386. _appForeground = NO;
  387. if (!_isPaused && _videoRecording)
  388. [self onBtnRecordStartClicked];
  389. }else{
  390. AVAudioSessionInterruptionOptions options = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
  391. if (options == AVAudioSessionInterruptionOptionShouldResume) {
  392. _appForeground = YES;
  393. }
  394. }
  395. }
  396. - (void)onAppDidEnterBackGround:(UIApplication*)app
  397. {
  398. _appForeground = NO;
  399. if (!_isPaused && _videoRecording)
  400. [self onBtnRecordStartClicked];
  401. }
  402. - (void)onAppWillEnterForeground:(UIApplication*)app
  403. {
  404. _appForeground = YES;
  405. }
  406. - (void)handlePinch:(UIPinchGestureRecognizer*)recognizer
  407. {
  408. if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged) {
  409. [[TXUGCRecord shareInstance] setZoom:MIN(MAX(1.0, _zoom * recognizer.scale),5.0)];
  410. }else if (recognizer.state == UIGestureRecognizerStateEnded){
  411. _zoom = MIN(MAX(1.0, _zoom * recognizer.scale),5.0);
  412. recognizer.scale = 1;
  413. }
  414. }
  415. #pragma mark ---- Common UI ----
  416. -(void)initUI
  417. {
  418. //拍摄视频时隐藏掉拍照。
  419. self.stillModeLabel.hidden = YES;
  420. self.title = @"";
  421. self.view.backgroundColor = UIColor.blackColor;
  422. [_btnNext setTitle:NSLocalizedString(@"Common.Next", nil)
  423. forState:UIControlStateNormal];
  424. [_btnMusic setTitle:NSLocalizedString(@"TCVideoRecordView.BeautyLabelMusic", nil)
  425. forState:UIControlStateNormal];
  426. [_btnBeauty setTitle:NSLocalizedString(@"TCVideoRecordView.BeautyLabelBeauty", nil)
  427. forState:UIControlStateNormal];
  428. [_btnAudioMix setTitle:NSLocalizedString(@"TCVideoRecordView.AudioMix", nil)
  429. forState:UIControlStateNormal];
  430. [_btnCountDown setTitle:NSLocalizedString(@"TCVideoRecordView.CountDown", nil)
  431. forState:UIControlStateNormal];
  432. _stillModeLabel.text = NSLocalizedString(@"TCVideoRecordView.StillPhoto",nil);
  433. _tapModeLabel.text = @"相册";
  434. // NSLocalizedString(@"TCVideoRecordView.TapCapture", nil);
  435. _pressModeLabel.text = NSLocalizedString(@"TCVideoRecordView.PressCapture", nil);
  436. self.recordTimeLabel.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
  437. self.recordTimeLabel.edgeInsets = UIEdgeInsetsMake(2, 8, 2, 8);
  438. self.recordTimeLabel.layer.cornerRadius = self.recordTimeLabel.height / 2;
  439. self.recordTimeLabel.layer.masksToBounds = YES;
  440. _videoRecordView = [UIView new];
  441. _videoRecordView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  442. [self.view insertSubview:_videoRecordView atIndex:0];
  443. if (_videoPath) {
  444. // 合唱
  445. self.speedView.hidden = YES;
  446. self.speedViewHeight.constant = 1;
  447. self.stillModeLabel.hidden = YES;
  448. self.progressView.minimumTimeTipHidden = YES;
  449. self.btnRatioMenu.hidden = YES;
  450. for (UIButton *button in @[self.btnRatio11, self.btnRatio43, self.btnRatio169]) {
  451. [button removeFromSuperview];
  452. }
  453. [self.btnMusic removeFromSuperview];
  454. [self.btnAudioMix removeFromSuperview];
  455. [self.btnBeauty mas_remakeConstraints:^(MASConstraintMaker *make) {
  456. make.top.equalTo(self.btnNext.mas_bottom).offset(38);
  457. make.right.equalTo(self.view).offset(-16);
  458. }];
  459. [self.btnCountDown mas_remakeConstraints:^(MASConstraintMaker *make) {
  460. make.centerX.equalTo(self.btnBeauty);
  461. make.top.equalTo(self.btnBeauty.mas_bottom).offset(30);
  462. }];
  463. _videoPlayView = [[UIView alloc] initWithFrame:CGRectZero];
  464. _videoPlayView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  465. [self.view insertSubview:_videoPlayView atIndex:0];
  466. [_videoPlayView mas_remakeConstraints:^(MASConstraintMaker *make) {
  467. make.width.equalTo(self.view).dividedBy(2);
  468. make.height.equalTo(self.view).dividedBy(2);
  469. make.right.equalTo(self.view.mas_right);
  470. make.bottom.equalTo(self.bottomMask.mas_top).offset(30);
  471. }];
  472. [_videoRecordView mas_remakeConstraints:^(MASConstraintMaker *make) {
  473. make.width.equalTo(self.view).dividedBy(2);
  474. make.height.equalTo(self.view).dividedBy(2);
  475. make.left.equalTo(self.view.mas_left);
  476. make.bottom.equalTo(self.bottomMask.mas_top).offset(30);
  477. }];
  478. _videoRecordView.translatesAutoresizingMaskIntoConstraints = NO;
  479. _videoPlayView.translatesAutoresizingMaskIntoConstraints = NO;
  480. TXVideoInfo *info = [TXVideoInfoReader getVideoInfo:_videoPath];
  481. CGFloat duration = info.duration;
  482. _fps = (int)(info.fps + 0.5);
  483. if (info.audioSampleRate == 8000) {
  484. _sampleRate = AUDIO_SAMPLERATE_8000;
  485. }else if (info.audioSampleRate == 16000){
  486. _sampleRate = AUDIO_SAMPLERATE_16000;
  487. }else if (info.audioSampleRate == 32000){
  488. _sampleRate = AUDIO_SAMPLERATE_32000;
  489. }else if (info.audioSampleRate == 44100){
  490. _sampleRate = AUDIO_SAMPLERATE_44100;
  491. }else if (info.audioSampleRate == 48000){
  492. _sampleRate = AUDIO_SAMPLERATE_48000;
  493. }
  494. _size = CGSizeMake(info.width, info.height);
  495. _recordType = RecordType_Chorus;
  496. MAX_RECORD_TIME = duration;
  497. MIN_RECORD_TIME = duration;
  498. TXPreviewParam *param = [TXPreviewParam new];
  499. param.videoView = _videoPlayView;
  500. param.renderMode = PREVIEW_RENDER_MODE_FILL_EDGE;
  501. //用于模仿视频播放
  502. _videoEditer = [[TXVideoEditer alloc] initWithPreview:param];
  503. [_videoEditer setVideoPath:_videoPath];
  504. //用于模仿视频和录制视频的合成
  505. _videoJoiner = [[TXVideoJoiner alloc] initWithPreview:nil];
  506. _videoJoiner.joinerDelegate = self;
  507. [self.view layoutIfNeeded];
  508. }else{
  509. self.btnCountDown.hidden = YES;
  510. _videoRecordView.frame = self.view.bounds;
  511. MAX_RECORD_TIME = 16;
  512. MIN_RECORD_TIME = 2;
  513. _recordType = RecordType_Normal;
  514. }
  515. UIPinchGestureRecognizer* pinchGensture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
  516. [_videoRecordView addGestureRecognizer:pinchGensture];
  517. _btnNext.enabled = NO;
  518. _btnRatio169.tag = VIDEO_ASPECT_RATIO_9_16;
  519. _btnRatio11.tag = VIDEO_ASPECT_RATIO_1_1;
  520. _btnRatio43.tag = VIDEO_ASPECT_RATIO_3_4;
  521. _btnRatio169.enabled = _recordType == RecordType_Normal;
  522. _musicView = [[TCVideoRecordMusicView alloc] initWithFrame:CGRectMake(0, self.view.bottom - 330 , self.view.width, 330 ) needEffect:YES];
  523. _musicView.delegate = self;
  524. _musicView.hidden = YES;
  525. [self.view addSubview:_musicView];
  526. _musicView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  527. _soundMixView = [SoundMixView instantiateFromNib];
  528. _soundMixView.delegate = self;
  529. _soundMixView.backgroundColor = kWhiteColor;
  530. _soundMixView.width = self.view.width;
  531. _soundMixView.top = self.view.height - _soundMixView.height;
  532. // _soundMixView = [[SoundMixView alloc] initWithFrame:_musicView.frame];
  533. [self.view addSubview:_soundMixView];
  534. _soundMixView.hidden = YES;
  535. _soundMixView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  536. if (_cameraFront) {
  537. _btnFlash.enabled = NO;
  538. }else{
  539. _btnFlash.enabled = YES;
  540. }
  541. [_recordTimeLabel setText:@"00:00"];
  542. [self configSpeedView];
  543. UIPanGestureRecognizer* panGensture = [[UIPanGestureRecognizer alloc] initWithTarget:self action: @selector (handlePanSlide:)];
  544. [self.view addGestureRecognizer:panGensture];
  545. // switch (_videoRatio) {
  546. // case VIDEO_ASPECT_RATIO_3_4:
  547. // [self onBtnRatioClicked:_btnRatio43];
  548. // break;
  549. // case VIDEO_ASPECT_RATIO_1_1:
  550. // [self onBtnRatioClicked:_btnRatio11];
  551. // break;
  552. // case VIDEO_ASPECT_RATIO_9_16:
  553. // [self onBtnRatioClicked:_btnRatio169];
  554. // break;
  555. //
  556. // default:
  557. // break;
  558. // }
  559. }
  560. #pragma mark ---- Video Beauty UI ----
  561. -(void)initBeautyUI
  562. {
  563. NSUInteger controlHeight = [BeautySettingPanel getHeight];
  564. CGFloat offset = 0;
  565. if (@available(iOS 11, *)) {
  566. offset = [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom;
  567. }
  568. _vBeauty = [[BeautySettingPanel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - controlHeight - offset, self.view.frame.size.width, controlHeight)];
  569. _vBeauty.hidden = YES;
  570. _vBeauty.delegate = self;
  571. _vBeauty.pituDelegate = self;
  572. _vBeauty.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  573. [self.view addSubview:_vBeauty];
  574. }
  575. //加速录制
  576. -(void)configSpeedView
  577. {
  578. _speedBtnList = [NSMutableArray array];
  579. _speedView.layer.cornerRadius = _speedView.size.height / 2;
  580. _speedView.layer.masksToBounds = YES;
  581. _speedView.backgroundColor = [UIColor blackColor];
  582. _speedView.alpha = 0.5;
  583. if (_recordType != RecordType_Chorus) {
  584. //合唱暂不支持变速录制
  585. _speedChangeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  586. _speedChangeBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
  587. _speedChangeBtn.titleLabel.minimumScaleFactor = 0.5;
  588. [_speedChangeBtn setTitle:[self getSpeedText:2] forState:UIControlStateNormal];
  589. [_speedChangeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  590. [_speedChangeBtn setBackgroundImage:[UIImage imageNamed:@"speedChange_center"] forState:UIControlStateNormal];
  591. CGFloat btnSpace = 0;
  592. CGFloat padding = 16 ;
  593. CGFloat btnWidth = (_speedView.width - 2 * padding - btnSpace * 4 ) / 5;
  594. for(int i = 0 ; i < BUTTON_SPEED_COUNT ; i ++)
  595. {
  596. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  597. btn.titleLabel.minimumScaleFactor = 0.5;
  598. btn.frame = CGRectMake(padding + (btnSpace + btnWidth) * i, 0, btnWidth, _speedView.height);
  599. [btn setTitle:[self getSpeedText:(SpeedMode)i] forState:UIControlStateNormal];
  600. [btn setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
  601. [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
  602. btn.titleLabel.minimumScaleFactor = 0.5;
  603. btn.titleLabel.adjustsFontSizeToFitWidth = YES;
  604. [btn addTarget:self action:@selector(onBtnSpeedClicked:) forControlEvents:UIControlEventTouchUpInside];
  605. btn.tag = i;
  606. [_speedView addSubview:btn];
  607. [_speedBtnList addObject:btn];
  608. }
  609. [self setSelectedSpeed:SpeedMode_Standard];
  610. [self.bottomMask addSubview:_speedChangeBtn];
  611. }
  612. }
  613. - (void)setSelectedSpeed:(SpeedMode)tag
  614. {
  615. if (tag >= _speedBtnList.count) {
  616. return;
  617. }
  618. const float padding = 16 ;
  619. UIButton *btn = _speedBtnList[(NSInteger)tag];
  620. // UIButton *btn = [self.speedView viewWithTag:(NSInteger)tag];
  621. CGRect rect = CGRectIntegral([_speedView convertRect:btn.frame toView:self.bottomMask]);
  622. CGRect frame = rect;
  623. frame.origin.y -= (BUTTON_SPEED_CHANGE_HEIGHT - rect.size.height) * 0.5;
  624. frame.size.height = BUTTON_SPEED_CHANGE_HEIGHT;
  625. NSString *bgName = @"speedChange_center";
  626. if (tag == 0) {
  627. frame.origin.x -= padding;
  628. frame.size.width += padding;
  629. bgName = @"speedChange_left";
  630. } else if (tag == 4) {
  631. frame.size.width += padding;
  632. bgName = @"speedChange_right";
  633. }
  634. [_speedChangeBtn setBackgroundImage:[UIImage imageNamed:bgName] forState:UIControlStateNormal];
  635. _speedChangeBtn.frame = frame;
  636. [_speedChangeBtn setTitle:[self getSpeedText:(SpeedMode)tag] forState:UIControlStateNormal];
  637. _speedMode = tag;
  638. }
  639. - (void)viewDidLayoutSubviews
  640. {
  641. CGFloat btnSpace = 0;
  642. CGFloat padding = 16 ;
  643. CGFloat btnWidth = (_speedView.width - 2 * padding - btnSpace * 4 ) / 5;
  644. [_speedBtnList enumerateObjectsUsingBlock:^(UIButton *btn, NSUInteger i, BOOL * _Nonnull stop) {
  645. btn.frame = CGRectMake(padding + (btnSpace + btnWidth) * i, 0, btnWidth, _speedView.height);
  646. }];
  647. [self setSelectedSpeed:_speedMode];
  648. }
  649. -(void)setSpeedBtnHidden:(BOOL)hidden{
  650. if (_videoPath != nil) hidden = YES;
  651. _speedView.hidden = hidden;
  652. _speedChangeBtn.hidden = hidden;
  653. }
  654. -(NSString *)getSpeedText:(SpeedMode)speedMode
  655. {
  656. NSString *text = nil;
  657. switch (speedMode) {
  658. case SpeedMode_VerySlow:
  659. text = NSLocalizedString(@"TCVideoRecordView.SpeedSlow0", nil);
  660. break;
  661. case SpeedMode_Slow:
  662. text = NSLocalizedString(@"TCVideoRecordView.SpeedSlow", nil);
  663. break;
  664. case SpeedMode_Standard:
  665. text = NSLocalizedString(@"TCVideoRecordView.SpeedStandard", nil);
  666. break;
  667. case SpeedMode_Quick:
  668. text = NSLocalizedString(@"TCVideoRecordView.SpeedFast", nil);
  669. break;
  670. case SpeedMode_VeryQuick:
  671. text = NSLocalizedString(@"TCVideoRecordView.SpeedFast0", nil);
  672. break;
  673. default:
  674. break;
  675. }
  676. return text;
  677. }
  678. - (void)switchButton:(UIButton *)button0 withAnother:(UIButton *)button1
  679. {
  680. NSInteger tmp = button0.tag;
  681. button0.tag = button1.tag;
  682. button1.tag = tmp;
  683. UIImage *tmpImage = [button0 imageForState:UIControlStateNormal];
  684. UIImage *tmpImage0 = [button0 imageForState:UIControlStateHighlighted];
  685. NSString *tmpTitle = [button0 titleForState:UIControlStateNormal];
  686. [button0 setImage:[button1 imageForState:UIControlStateNormal] forState:UIControlStateNormal];
  687. [button0 setImage:[button1 imageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
  688. [button0 setTitle:[button1 titleForState:UIControlStateNormal] forState:UIControlStateNormal];
  689. [button1 setImage:tmpImage forState:UIControlStateNormal];
  690. [button1 setImage:tmpImage0 forState:UIControlStateHighlighted];
  691. [button1 setTitle:tmpTitle forState:UIControlStateNormal];
  692. }
  693. - (void)setAspectRatio:(TXVideoAspectRatio)aspectRatio
  694. {
  695. _aspectRatio = aspectRatio;
  696. [[TXUGCRecord shareInstance] setAspectRatio:_aspectRatio];
  697. CGFloat height = 0;
  698. switch (_aspectRatio) {
  699. case VIDEO_ASPECT_RATIO_9_16:
  700. height = _videoRecordView.frame.size.width * 16 / 9;
  701. break;
  702. case VIDEO_ASPECT_RATIO_3_4:
  703. height = _videoRecordView.frame.size.width * 4 / 3;
  704. break;
  705. case VIDEO_ASPECT_RATIO_1_1:
  706. height = _videoRecordView.frame.size.width * 1 / 1;
  707. break;
  708. default:
  709. break;
  710. }
  711. [UIView animateWithDuration:0.2 animations:^{
  712. _videoRecordView.frame = CGRectMake(0, (self.view.height - height) / 2.0, _videoRecordView.frame.size.width, height);;
  713. }];
  714. }
  715. -(BOOL)ratioIsClosure
  716. {
  717. if (CGRectEqualToRect(_btnRatio43.frame, _btnRatio11.frame)) {
  718. return YES;
  719. }
  720. return NO;
  721. }
  722. #pragma mark - Actions
  723. - (void)takePhoto {
  724. [[TXUGCRecord shareInstance] snapshot:^(UIImage *image) {
  725. dispatch_async(dispatch_get_main_queue(), ^{
  726. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  727. UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void*)imageView);
  728. imageView.contentMode = UIViewContentModeScaleAspectFit;
  729. imageView.frame = self.view.bounds;
  730. [self.view insertSubview:imageView belowSubview:self.bottomMask];
  731. CGAffineTransform t = CGAffineTransformMakeScale(0.33, 0.33);
  732. [UIView animateWithDuration:0.3 animations:^{
  733. imageView.transform = t;
  734. } completion:^(BOOL finished) {
  735. }];
  736. });
  737. }];
  738. }
  739. -(void)startVideoRecord
  740. {
  741. self.btnCountDown.enabled = NO;
  742. [self startCameraPreview];
  743. [self setSpeedRate];
  744. int result = [[TXUGCRecord shareInstance] startRecord];
  745. [TCUtil report:xiaoshipin_startrecord userName:nil code:result msg:result == 0 ? ASLocalizedString(@"启动录制成功"): ASLocalizedString(@"启动录制失败")];
  746. if(0 != result)
  747. {
  748. if(-3 == result) [self alert:NSLocalizedString(@"TCVideoRecordView.HintLaunchRecordFailed", nil) msg:NSLocalizedString(@"TCVideoRecordView.ErrorCamera", nil)];
  749. if(-4 == result) [self alert:NSLocalizedString(@"TCVideoRecordView.HintLaunchRecordFailed", nil) msg:NSLocalizedString(@"TCVideoRecordView.ErrorMIC", nil)];
  750. if(-5 == result) [self alert:NSLocalizedString(@"TCVideoRecordView.HintLaunchRecordFailed", nil) msg:NSLocalizedString(@"TCVideoRecordView.ErrorLicense", nil)];
  751. }else{
  752. //如果设置了BGM,播放BGM
  753. [self playBGM:_bgmBeginTime];
  754. //初始化录制状态
  755. _bgmRecording = YES;
  756. _videoRecording = YES;
  757. _isPaused = NO;
  758. //录制过程中不能切换分辨率,不能切换拍照模式
  759. _btnRatio169.enabled = NO;
  760. _btnRatio43.enabled = NO;
  761. _btnRatio11.enabled = NO;
  762. self.captureModeView.userInteractionEnabled = NO;
  763. [self setSpeedBtnHidden:YES];
  764. [_btnStartRecord setImage:[UIImage imageNamed:@"pause_record"] forState:UIControlStateNormal];
  765. [_btnStartRecord setBackgroundImage:[UIImage imageNamed:@"pause_ring"] forState:UIControlStateNormal];
  766. _btnStartRecord.bounds = CGRectMake(0, 0, BUTTON_RECORD_SIZE * 0.85, BUTTON_RECORD_SIZE * 0.85);
  767. if (_recordType == RecordType_Chorus) {
  768. [_videoEditer startPlayFromTime:_recordTime toTime:[[GlobalVariables sharedInstance].appModel.sts_video_limit floatValue]];
  769. }
  770. }
  771. }
  772. - (void)startCountDown {
  773. if (_countDownTimer) {
  774. return;
  775. }
  776. if (_countDownView == nil) {
  777. UIVisualEffectView *view = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle: UIBlurEffectStyleExtraLight]];
  778. view.layer.cornerRadius = 20;
  779. view.clipsToBounds = YES;
  780. view.translatesAutoresizingMaskIntoConstraints = NO;
  781. UILabel *countDownLabel = [[UILabel alloc] init];
  782. countDownLabel.translatesAutoresizingMaskIntoConstraints = NO;
  783. countDownLabel.textColor = [UIColor colorWithWhite:0.33 alpha:1];
  784. countDownLabel.font = [UIFont systemFontOfSize:100];
  785. [view.contentView addSubview:countDownLabel];
  786. [view.contentView addConstraint:[NSLayoutConstraint constraintWithItem:view.contentView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:countDownLabel attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
  787. [view.contentView addConstraint:[NSLayoutConstraint constraintWithItem:view.contentView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:countDownLabel attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
  788. [view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:150]];
  789. [view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:150]];
  790. [self.view addSubview:view];
  791. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
  792. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
  793. _countDownView = view;
  794. _countDownLabel = countDownLabel;
  795. }
  796. _countDownView.hidden = NO;
  797. _countDownLabel.text = @"3";
  798. _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onCountDownTimer:) userInfo:nil repeats:YES];
  799. _countDownView.hidden = NO;
  800. }
  801. - (void)onCountDownTimer:(NSTimer *)timer {
  802. int count = _countDownLabel.text.intValue - 1;
  803. _countDownLabel.text = @(count).stringValue;
  804. if (count == 0) {
  805. [_countDownTimer invalidate];
  806. _countDownTimer = nil;
  807. _countDownView.hidden = YES;
  808. [self changeCaptureModeUI:CaptureModeTap];
  809. self.captureMode = CaptureModeTap;
  810. [self onBtnRecordStartClicked];
  811. [self hideBottomView:NO];
  812. }
  813. }
  814. #pragma mark - Properties
  815. -(void)setSpeedRate{
  816. switch (_speedMode) {
  817. case SpeedMode_VerySlow:
  818. [[TXUGCRecord shareInstance] setRecordSpeed:VIDEO_RECORD_SPEED_SLOWEST];
  819. break;
  820. case SpeedMode_Slow:
  821. [[TXUGCRecord shareInstance] setRecordSpeed:VIDEO_RECORD_SPEED_SLOW];
  822. break;
  823. case SpeedMode_Standard:
  824. [[TXUGCRecord shareInstance] setRecordSpeed:VIDEO_RECORD_SPEED_NOMAL];
  825. break;
  826. case SpeedMode_Quick:
  827. [[TXUGCRecord shareInstance] setRecordSpeed:VIDEO_RECORD_SPEED_FAST];
  828. break;
  829. case SpeedMode_VeryQuick:
  830. [[TXUGCRecord shareInstance] setRecordSpeed:VIDEO_RECORD_SPEED_FASTEST];
  831. break;
  832. default:
  833. break;
  834. }
  835. }
  836. #pragma mark - Left Side Button Event Handler
  837. -(IBAction)onBtnPopClicked:(id)sender
  838. {
  839. NSArray *videoPaths = [[TXUGCRecord shareInstance].partsManager getVideoPathList];
  840. if (videoPaths.count > 0) {
  841. UIAlertView *alert = [UIAlertView bk_showAlertViewWithTitle:NSLocalizedString(@"TCVideoRecordView.AbandonRecord", nil) message:nil cancelButtonTitle:NSLocalizedString(@"Common.Cancel", nil) otherButtonTitles:@[NSLocalizedString(@"Common.OK", nil)] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
  842. if (buttonIndex == 1) {
  843. [[NSUserDefaults standardUserDefaults] setObject:nil forKey:CACHE_PATH_LIST];
  844. // if (_recordType == RecordType_Normal) {
  845. //
  846. // }else{
  847. //
  848. // }
  849. [self dismissViewControllerAnimated:YES completion:nil];
  850. [self.navigationController popViewControllerAnimated:YES];
  851. }else{
  852. return;
  853. }
  854. }];
  855. [alert show];
  856. }else{
  857. [[NSUserDefaults standardUserDefaults] setObject:nil forKey:CACHE_PATH_LIST];
  858. // if (_recordType == RecordType_Normal) {
  859. // [self dismissViewControllerAnimated:YES completion:nil];
  860. // }else{
  861. // [self.navigationController popViewControllerAnimated:YES];
  862. // }
  863. [self dismissViewControllerAnimated:YES completion:nil];
  864. [self.navigationController popViewControllerAnimated:YES];
  865. }
  866. }
  867. #pragma mark - Right Side Button Event Handler
  868. - (IBAction)onBtnDoneClicked:(id)sender
  869. {
  870. // if (!_videoRecording)
  871. // return;
  872. self.btnNext.userInteractionEnabled = NO;
  873. [self stopVideoRecord];
  874. }
  875. - (IBAction)onBtnMusicClicked:(id)sender
  876. {
  877. _vBeauty.hidden = YES;
  878. if (_BGMPath) {
  879. _musicView.hidden = !_musicView.hidden;
  880. [self hideBottomView:!_musicView.hidden];
  881. }else{
  882. // UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:_bgmListVC];
  883. // [nv.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
  884. // nv.navigationBar.barTintColor = RGB(25, 29, 38);
  885. // [self presentViewController:nv animated:YES completion:nil];
  886. // [_bgmListVC loadBGMList];
  887. [self presetTonew_bgmList];
  888. }
  889. }
  890. - (void)presetTonew_bgmList
  891. {
  892. __block typeof(self)blockself =self;
  893. [[TXUGCRecord shareInstance] pauseBGM];
  894. new_BGMListController *bgmvc =[new_BGMListController new];
  895. new_bgmNavController *nv = [[new_bgmNavController alloc] initWithRootViewController:bgmvc];
  896. bgmvc.nav =nv;
  897. nv.useMusicBlock = ^(music_obj *model) {
  898. if (model)
  899. {
  900. blockself.model =model;
  901. blockself.musicPatch =model.music_url;
  902. [blockself onBGMControllerPlay:model.music_url];
  903. }
  904. };
  905. nv.modalPresentationStyle = UIModalPresentationFullScreen;
  906. [self presentViewController:nv animated:YES completion:nil];
  907. }
  908. - (IBAction)onBtnRatioClicked:(UIButton *)btn
  909. {
  910. TXVideoAspectRatio targetRatio = btn.tag;
  911. if (btn == self.btnRatioMenu) {
  912. BOOL menuIsClosed = NO;
  913. if (self.btnRatio169 == self.btnRatioMenu) {
  914. menuIsClosed = self.btnRatio11.hidden;
  915. } else {
  916. menuIsClosed = self.btnRatio169.hidden;
  917. }
  918. BOOL shouldHidden = !menuIsClosed;
  919. self.btnRatio169.hidden = shouldHidden;
  920. self.btnRatio11.hidden = shouldHidden;
  921. self.btnRatio43.hidden = shouldHidden;
  922. self.btnRatioMenu.hidden = NO;
  923. } else {
  924. [self switchButton:self.btnRatioMenu withAnother:btn];
  925. __weak UIButton *tmp = *_ratioMenuButtonIvarPtr;
  926. *_ratioMenuButtonIvarPtr = btn;
  927. switch (targetRatio) {
  928. case VIDEO_ASPECT_RATIO_9_16:
  929. self.btnRatio169 = tmp;
  930. _ratioMenuButtonIvarPtr = &_btnRatio169;
  931. break;
  932. case VIDEO_ASPECT_RATIO_3_4:
  933. self.btnRatio43 = tmp;
  934. _ratioMenuButtonIvarPtr = &_btnRatio43;
  935. break;
  936. case VIDEO_ASPECT_RATIO_1_1:
  937. self.btnRatio11 = tmp;
  938. _ratioMenuButtonIvarPtr = &_btnRatio11;
  939. break;
  940. default:
  941. break;
  942. }
  943. [self setAspectRatio:targetRatio];
  944. self.btnRatio169.hidden = YES;
  945. self.btnRatio11.hidden = YES;
  946. self.btnRatio43.hidden = YES;
  947. self.btnRatioMenu.hidden = NO;
  948. }
  949. }
  950. -(IBAction)onBtnBeautyClicked:(id)sender
  951. {
  952. _vBeautyShow = !_vBeautyShow;
  953. _musicView.hidden = YES;
  954. _vBeauty.hidden = !_vBeautyShow;
  955. [self hideBottomView:_vBeautyShow];
  956. }
  957. - (IBAction)onBtnAudioMix:(id)sender {
  958. [self hideBottomView:YES];
  959. _soundMixView.hidden = NO;
  960. _vBeauty.hidden = YES;
  961. _musicView.hidden = YES;
  962. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  963. button.frame = CGRectMake(0, 0, self.view.width, self.view.height - _soundMixView.height);
  964. [self.view addSubview:button];
  965. [button addTarget:self action:@selector(onHideSoundMix:) forControlEvents:UIControlEventTouchUpInside];
  966. CATransition *animation = [CATransition animation];
  967. animation.type = kCATransitionFade;
  968. [_soundMixView.layer addAnimation:animation forKey:nil];
  969. }
  970. - (IBAction)onCountDown:(id)sender {
  971. [self hideBottomView:YES];
  972. [self startCountDown];
  973. }
  974. #pragma mark * Bottom Control Tap Handler
  975. -(void)onBtnSpeedClicked:(UIButton *)btn
  976. {
  977. [UIView animateWithDuration:0.3 animations:^{
  978. _speedMode = btn.tag;
  979. [self setSelectedSpeed:_speedMode];
  980. }];
  981. }
  982. -(IBAction)onBtnFlashClicked
  983. {
  984. if (_isFlash) {
  985. [_btnFlash setImage:[UIImage imageNamed:@"closeFlash"] forState:UIControlStateNormal];
  986. [_btnFlash setImage:[UIImage imageNamed:@"closeFlash_hover"] forState:UIControlStateHighlighted];
  987. }else{
  988. [_btnFlash setImage:[UIImage imageNamed:@"openFlash"] forState:UIControlStateNormal];
  989. [_btnFlash setImage:[UIImage imageNamed:@"openFlash_hover"] forState:UIControlStateHighlighted];
  990. }
  991. _isFlash = !_isFlash;
  992. [[TXUGCRecord shareInstance] toggleTorch:_isFlash];
  993. }
  994. -(IBAction)onBtnDeleteClicked
  995. {
  996. if (_videoRecording && !_isPaused) {
  997. [self onBtnRecordStartClicked];
  998. }
  999. if (0 == _deleteCount) {
  1000. [_progressView prepareDeletePart];
  1001. }else{
  1002. [_progressView comfirmDeletePart];
  1003. [[TXUGCRecord shareInstance].partsManager deleteLastPart];
  1004. _isBackDelete = YES;
  1005. }
  1006. if (2 == ++ _deleteCount) {
  1007. _deleteCount = 0;
  1008. }
  1009. }
  1010. -(IBAction)onBtnStartRecord // touch down
  1011. {
  1012. if (self.captureMode == CaptureModePress) {
  1013. [self onBtnRecordStartClicked];
  1014. }
  1015. }
  1016. -(IBAction)onBtnStopRecord // touch up
  1017. {
  1018. switch (self.captureMode) {
  1019. case CaptureModePress:
  1020. [self onBtnRecordStartClicked];
  1021. break;
  1022. case CaptureModeTap:
  1023. [self onBtnRecordStartClicked];
  1024. break;
  1025. default:
  1026. break;
  1027. }
  1028. }
  1029. - (IBAction)onRecordTouchUpInside:(id)sender
  1030. {
  1031. if (self.captureMode == CaptureModeStill) {
  1032. [self takePhoto];
  1033. }
  1034. }
  1035. -(IBAction)onBtnCameraClicked
  1036. {
  1037. _cameraFront = !_cameraFront;
  1038. [[TXUGCRecord shareInstance] switchCamera:_cameraFront];
  1039. if (_cameraFront) {
  1040. [_btnFlash setImage:[UIImage imageNamed:@"openFlash_disable"] forState:UIControlStateNormal];
  1041. _btnFlash.enabled = NO;
  1042. }else{
  1043. if (_isFlash) {
  1044. [_btnFlash setImage:[UIImage imageNamed:@"openFlash"] forState:UIControlStateNormal];
  1045. [_btnFlash setImage:[UIImage imageNamed:@"openFlash_hover"] forState:UIControlStateHighlighted];
  1046. }else{
  1047. [_btnFlash setImage:[UIImage imageNamed:@"closeFlash"] forState:UIControlStateNormal];
  1048. [_btnFlash setImage:[UIImage imageNamed:@"closeFlash_hover"] forState:UIControlStateHighlighted];
  1049. }
  1050. _btnFlash.enabled = YES;
  1051. }
  1052. [[TXUGCRecord shareInstance] toggleTorch:_isFlash];
  1053. }
  1054. - (IBAction)onTapCaptureMode:(UITapGestureRecognizer *)gesture
  1055. {
  1056. CGPoint point = [gesture locationInView:gesture.view];
  1057. CGFloat itemWidth = CGRectGetWidth(gesture.view.frame) / 3;
  1058. CaptureMode mode = floor(point.x / itemWidth);
  1059. [self changeCaptureModeUI:mode];
  1060. self.captureMode = mode;
  1061. }
  1062. - (void)changeCaptureModeUI:(CaptureMode)captureMode
  1063. {
  1064. NSInteger tag = captureMode + 1;
  1065. if (tag == 1) {
  1066. NSLog(@"点击了相册");
  1067. return;
  1068. }
  1069. if (_videoPath) {
  1070. // 合唱
  1071. if (tag - 1 == CaptureModeStill) {
  1072. return;
  1073. }
  1074. }
  1075. UIView *targetView = [self.captureModeView viewWithTag:tag];
  1076. CGFloat offset = CGRectGetMidX(self.captureModeView.bounds) - targetView.center.x;
  1077. self.captureMode = tag - 1;
  1078. NSString *name = self.captureMode == CaptureModeStill ? @"start_record_white" : @"start_record";
  1079. [UIView animateWithDuration:0.1 animations:^{
  1080. [self.btnStartRecord setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
  1081. self.centerConstraint.constant = offset;
  1082. [self.captureModeView layoutIfNeeded];
  1083. }];
  1084. }
  1085. #pragma mark -
  1086. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  1087. {
  1088. UIImageView *imageView = (__bridge UIImageView *)contextInfo;
  1089. [UIView animateWithDuration:0.3
  1090. delay:0
  1091. options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState
  1092. animations:^{
  1093. CGAffineTransform t = CGAffineTransformTranslate(imageView.transform, 0, self.view.height);
  1094. imageView.transform = CGAffineTransformScale(t, 0.5, 0.5);
  1095. } completion:^(BOOL finished) {
  1096. [imageView removeFromSuperview];
  1097. }];
  1098. }
  1099. -(void)onBtnRecordStartClicked
  1100. {
  1101. if (!_videoRecording)
  1102. {
  1103. [self startVideoRecord];
  1104. }
  1105. else
  1106. {
  1107. if (_isPaused) {
  1108. self.btnCountDown.enabled = NO;
  1109. self.captureModeView.userInteractionEnabled = NO;
  1110. [self setSpeedRate];
  1111. if (_bgmRecording) {
  1112. [self resumeBGM];
  1113. }else{
  1114. [self playBGM:_bgmBeginTime];
  1115. _bgmRecording = YES;
  1116. }
  1117. [[TXUGCRecord shareInstance] resumeRecord];
  1118. [_btnStartRecord setImage:[UIImage imageNamed:@"pause_record"] forState:UIControlStateNormal];
  1119. [_btnStartRecord setBackgroundImage:[UIImage imageNamed:@"pause_ring"] forState:UIControlStateNormal];
  1120. _btnStartRecord.bounds = CGRectMake(0, 0, BUTTON_RECORD_SIZE * 0.85, BUTTON_RECORD_SIZE * 0.85);
  1121. if (_deleteCount == 1) {
  1122. [_progressView cancelDelete];
  1123. _deleteCount = 0;
  1124. }
  1125. [self setSpeedBtnHidden:YES];
  1126. _isPaused = NO;
  1127. [_videoEditer startPlayFromTime:_recordTime toTime:MAX_RECORD_TIME];
  1128. }
  1129. else {
  1130. self.captureModeView.userInteractionEnabled = YES;
  1131. self.btnCountDown.enabled = YES;
  1132. __weak __typeof(self) weakSelf = self;
  1133. [[TXUGCRecord shareInstance] pauseRecord:^{
  1134. [weakSelf cacheVideoPathList];
  1135. }];
  1136. [self pauseBGM];
  1137. [_btnStartRecord setImage:[UIImage imageNamed:@"start_record"] forState:UIControlStateNormal];
  1138. [_btnStartRecord setBackgroundImage:[UIImage imageNamed:@"start_ring"] forState:UIControlStateNormal];
  1139. _btnStartRecord.bounds = CGRectMake(0, 0, BUTTON_RECORD_SIZE, BUTTON_RECORD_SIZE);
  1140. [_progressView pause];
  1141. [self setSpeedBtnHidden:NO];
  1142. _isPaused = YES;
  1143. [_videoEditer stopPlay];
  1144. }
  1145. }
  1146. }
  1147. - (void)onHideSoundMix:(UIButton *)sender
  1148. {
  1149. [sender removeFromSuperview];
  1150. _soundMixView.hidden = YES;
  1151. self.bottomMask.hidden = NO;
  1152. [self hideBottomView:NO];
  1153. CATransition *animation = [CATransition animation];
  1154. animation.duration = 0.1;
  1155. animation.type = kCATransitionFade;
  1156. [_soundMixView.superview.layer addAnimation:animation forKey:nil];
  1157. }
  1158. -(void)startCameraPreview
  1159. {
  1160. if (_cameraPreviewing == NO)
  1161. {
  1162. //简单设置
  1163. // TXUGCSimpleConfig * param = [[TXUGCSimpleConfig alloc] init];
  1164. // param.videoQuality = VIDEO_QUALITY_MEDIUM;
  1165. // [[TXUGCRecord shareInstance] startCameraSimple:param preview:_videoRecordView];
  1166. //自定义设置
  1167. TXUGCCustomConfig * param = [[TXUGCCustomConfig alloc] init];
  1168. param.videoResolution = VIDEO_RESOLUTION_720_1280;
  1169. param.videoFPS = 30;
  1170. param.videoBitratePIN = 9600;
  1171. param.GOP = 3;
  1172. param.audioSampleRate = AUDIO_SAMPLERATE_48000;
  1173. param.minDuration = MIN_RECORD_TIME;
  1174. param.maxDuration = [[GlobalVariables sharedInstance].appModel.sts_video_limit floatValue];
  1175. // MAX_RECORD_TIME;
  1176. [[TXUGCRecord shareInstance] startCameraCustom:param preview:_videoRecordView];
  1177. [[TXUGCRecord shareInstance] setBeautyStyle:_beautyStyle beautyLevel:_beautyDepth whitenessLevel:_whitenDepth ruddinessLevel:_ruddinessDepth];
  1178. [[TXUGCRecord shareInstance] setVideoRenderMode:VIDEO_RENDER_MODE_ADJUST_RESOLUTION];
  1179. if (_greenIndex >=0 || _greenIndex < _greenArray.count) {
  1180. V8LabelNode *v = [_greenArray objectAtIndex:_greenIndex];
  1181. [[TXUGCRecord shareInstance] setGreenScreenFile:v.file];
  1182. }
  1183. [[TXUGCRecord shareInstance] setEyeScaleLevel:_eye_level];
  1184. [[TXUGCRecord shareInstance] setFaceScaleLevel:_face_level];
  1185. UIImage *watermark = [UIImage imageNamed:@"watermark.png"];
  1186. CGRect watermarkFrame = (CGRect){0.01, 0.01, 0.3 , 0};
  1187. [[TXUGCRecord shareInstance] setWaterMark:watermark normalizationFrame:watermarkFrame];
  1188. #if POD_PITU
  1189. [self motionTmplSelected:_materialID];
  1190. #endif
  1191. //内存里面没有视频数据,重置美颜状态
  1192. if ([TXUGCRecord shareInstance].partsManager.getVideoPathList.count == 0) {
  1193. [_vBeauty resetValues];
  1194. }
  1195. //加载本地视频 -> 内存
  1196. if (_preloadingVideos) {
  1197. NSArray *cachePathList = [[NSUserDefaults standardUserDefaults] objectForKey:CACHE_PATH_LIST];
  1198. NSString *cacheFolder = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"TXUGC"] stringByAppendingPathComponent:@"TXUGCParts"];
  1199. //预加载视频 -> SDK
  1200. TXVideoInfo *videoInfo = nil;
  1201. for (NSInteger i = cachePathList.count - 1; i >= 0; i --) {
  1202. NSString *videoPath = [cacheFolder stringByAppendingPathComponent:cachePathList[i]];
  1203. [[TXUGCRecord shareInstance].partsManager insertPart:videoPath atIndex:0];
  1204. if (i == 0) {
  1205. videoInfo = [TXVideoInfoReader getVideoInfo:videoPath];
  1206. }
  1207. }
  1208. //录制分辨比例初始化
  1209. if (videoInfo.width == 720 && videoInfo.height == 1280 && self.btnRatio169 != self.btnRatioMenu) {
  1210. [self onBtnRatioClicked:self.btnRatio169];
  1211. }else if(videoInfo.width == 720 && videoInfo.height == 960 && self.btnRatio43 != self.btnRatioMenu){
  1212. [self onBtnRatioClicked:self.btnRatio43];
  1213. }else if(videoInfo.width == 720 && videoInfo.height == 720 && self.btnRatio11 != self.btnRatioMenu){
  1214. [self onBtnRatioClicked:self.btnRatio11];
  1215. }
  1216. //进度条初始化
  1217. CGFloat time = 0;
  1218. for (NSInteger i = 0; i < cachePathList.count; i ++) {
  1219. NSString *videoPath = [cacheFolder stringByAppendingPathComponent:cachePathList[i]];
  1220. time = time + [TXVideoInfoReader getVideoInfo:videoPath].duration;
  1221. [_progressView pauseAtTime:time];
  1222. }
  1223. _preloadingVideos = NO;
  1224. }
  1225. _cameraPreviewing = YES;
  1226. }
  1227. }
  1228. -(void)stopCameraPreview
  1229. {
  1230. if (_cameraPreviewing == YES)
  1231. {
  1232. [[TXUGCRecord shareInstance] stopCameraPreview];
  1233. _cameraPreviewing = NO;
  1234. }
  1235. }
  1236. -(void)alert:(NSString *)title msg:(NSString *)msg
  1237. {
  1238. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Common.OK", nil) otherButtonTitles:nil, nil];
  1239. [alert show];
  1240. }
  1241. -(void)stopVideoRecord
  1242. {
  1243. self.btnCountDown.enabled = YES;
  1244. [_btnStartRecord setImage:[UIImage imageNamed:@"start_record"] forState:UIControlStateNormal];
  1245. [_btnStartRecord setBackgroundImage:[UIImage imageNamed:@"start_ring"] forState:UIControlStateNormal];
  1246. _btnStartRecord.bounds = CGRectMake(0, 0, BUTTON_RECORD_SIZE, BUTTON_RECORD_SIZE);
  1247. [self setSpeedBtnHidden:NO];
  1248. [_videoEditer stopPlay];
  1249. //调用partsManager快速合成视频,不破坏录制状态,下次返回后可以接着录制(注意需要先暂停视频录制)
  1250. __weak __typeof(self) weakSelf = self;
  1251. [[TXUGCRecord shareInstance] pauseRecord:^{
  1252. [weakSelf cacheVideoPathList];
  1253. }];
  1254. NSLog(@"_recordVideoPath%@",_recordVideoPath);
  1255. [[TXUGCRecord shareInstance].partsManager joinAllParts:_recordVideoPath complete:^(int result) {
  1256. [weakSelf joinAllPartsResult:result];
  1257. }];
  1258. // NSURL *avUrl = [NSURL fileURLWithPath:_recordVideoPath];
  1259. // AVAsset *videoAsset = [AVAsset assetWithURL:avUrl];
  1260. // NSData *data = [NSData dataWithContentsOfURL:avUrl];
  1261. }
  1262. - (void)cacheVideoPathList
  1263. {
  1264. NSMutableArray *cachePathList = [NSMutableArray array];
  1265. for (NSString *videoPath in [TXUGCRecord shareInstance].partsManager.getVideoPathList) {
  1266. [cachePathList addObject:[[videoPath pathComponents] lastObject]];
  1267. }
  1268. [[NSUserDefaults standardUserDefaults] setObject:cachePathList forKey:CACHE_PATH_LIST];
  1269. }
  1270. -(void)joinAllPartsResult:(int)result
  1271. {
  1272. if(0 == result){
  1273. if (_recordType == RecordType_Normal) {
  1274. [self stopCameraPreview];
  1275. // TCVideoEditViewController *vc = [[TCVideoEditViewController alloc] init];
  1276. // vc.videoPath = _recordVideoPath;
  1277. // vc.element =_model;
  1278. // [[AppDelegate sharedAppDelegate]pushViewController:vc];
  1279. // [self.navigationController pushViewController:vc animated:YES];
  1280. }else{
  1281. CGFloat width = 720;
  1282. CGFloat height = 1280;
  1283. CGRect recordScreen = CGRectMake(0, 0, width, height);
  1284. //播放视频所占画布的大小这里要计算下,防止视频拉伸
  1285. CGRect playScreen = CGRectZero;
  1286. if (_size.height / _size.width >= height / width) {
  1287. CGFloat playScreen_w = height * _size.width / _size.height;
  1288. playScreen = CGRectMake(width + (width - playScreen_w) / 2.0, 0, playScreen_w, height);
  1289. }else{
  1290. CGFloat playScreen_h = width * _size.height / _size.width;
  1291. playScreen = CGRectMake(width, (height - playScreen_h) / 2.0, width, playScreen_h);
  1292. }
  1293. if (_recordVideoPath
  1294. && _videoPath
  1295. && [[NSFileManager defaultManager] fileExistsAtPath:_recordVideoPath]
  1296. && [[NSFileManager defaultManager] fileExistsAtPath:_videoPath]) {
  1297. if (0 == [_videoJoiner setVideoPathList:@[_recordVideoPath,_videoPath]]) {
  1298. [_videoJoiner setSplitScreenList:@[[NSValue valueWithCGRect:recordScreen],[NSValue valueWithCGRect:playScreen]] canvasWidth:720 * 2 canvasHeight:1280];
  1299. [_videoJoiner splitJoinVideo:VIDEO_COMPRESSED_720P videoOutputPath:_joinVideoPath];
  1300. _hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  1301. _hub.mode = MBProgressHUDModeText;
  1302. _hub.label.text = NSLocalizedString(@"TCVideoEditPrevView.VideoSynthesizing", nil);
  1303. }else{
  1304. UIAlertView *alert = [UIAlertView bk_showAlertViewWithTitle:NSLocalizedString(@"TCVideoEditPrevView.HintVideoSynthesizeFailed", nil) message:NSLocalizedString(@"TCVideoEditPrevView.VideoChorusNotSupported",nil) cancelButtonTitle:NSLocalizedString(@"Common.OK", nil) otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
  1305. }];
  1306. [alert show];
  1307. }
  1308. }else{
  1309. [self alert:NSLocalizedString(@"TCVideoEditPrevView.HintVideoSynthesizeFailed",nil) msg:NSLocalizedString(@"TCVideoEditPrevView.TryAgain",nil)];
  1310. }
  1311. }
  1312. [TCUtil report:xiaoshipin_videorecord userName:nil code:0 msg:ASLocalizedString(@"视频录制成功")];
  1313. }else{
  1314. [self alert:NSLocalizedString(@"TCVideoEditPrevView.HintVideoSynthesizeFailed",nil) msg:NSLocalizedString(@"TCVideoEditPrevView.TryAgain",nil)];
  1315. [TCUtil report:xiaoshipin_videorecord userName:nil code:-1 msg:ASLocalizedString(@"视频录制失败")];
  1316. }
  1317. }
  1318. -(void)resetVideoUI
  1319. {
  1320. [_progressView deleteAllPart];
  1321. [_btnStartRecord setImage:[UIImage imageNamed:@"start_record"] forState:UIControlStateNormal];
  1322. [_btnStartRecord setBackgroundImage:[UIImage imageNamed:@"start_ring"] forState:UIControlStateNormal];
  1323. _btnStartRecord.bounds = CGRectMake(0, 0, BUTTON_RECORD_SIZE, BUTTON_RECORD_SIZE);
  1324. [self resetSpeedBtn];
  1325. [_musicView resetVolume];
  1326. //合唱逻辑UI暂不适配
  1327. if (_recordType == RecordType_Normal) {
  1328. _btnRatio169.enabled = YES;
  1329. _btnRatio43.enabled = YES;
  1330. _btnRatio11.enabled = YES;
  1331. _btnMusic.enabled = YES;
  1332. }
  1333. _btnNext.enabled = NO;
  1334. _isPaused = NO;
  1335. _videoRecording = NO;
  1336. }
  1337. -(void)resetSpeedBtn{
  1338. [self setSpeedBtnHidden:NO];
  1339. for(UIButton *btn in _speedBtnList){
  1340. if (btn.tag == 2) {
  1341. [self onBtnSpeedClicked:btn];
  1342. }
  1343. }
  1344. }
  1345. -(void)onBtnLampClicked
  1346. {
  1347. _lampOpened = !_lampOpened;
  1348. BOOL result = [[TXUGCRecord shareInstance] toggleTorch:_lampOpened];
  1349. if (result == NO)
  1350. {
  1351. _lampOpened = !_lampOpened;
  1352. [self toastTip:NSLocalizedString(@"TCVideoRecordView.ErrorFlash", nil)];
  1353. }
  1354. if (_lampOpened)
  1355. {
  1356. [_btnTorch setImage:[UIImage imageNamed:@"lamp_press"] forState:UIControlStateNormal];
  1357. }else
  1358. {
  1359. [_btnTorch setImage:[UIImage imageNamed:@"lamp"] forState:UIControlStateNormal];
  1360. }
  1361. }
  1362. /// 选拍照模式
  1363. - (void)setCaptureMode:(CaptureMode)captureMode
  1364. {
  1365. _captureMode = captureMode;
  1366. BOOL isStillMode = captureMode == CaptureModeStill;
  1367. self.speedView.hidden = isStillMode;
  1368. self.progressView.hidden = isStillMode;
  1369. self.recordTimeLabel.hidden = isStillMode;
  1370. self.btnDelete.hidden = isStillMode;
  1371. _speedChangeBtn.hidden = isStillMode;
  1372. }
  1373. - (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  1374. {
  1375. if (_vBeautyShow)
  1376. {
  1377. UITouch *touch = [[event allTouches] anyObject];
  1378. CGPoint _touchPoint = [touch locationInView:self.view];
  1379. if (NO == CGRectContainsPoint(_vBeauty.frame, _touchPoint))
  1380. {
  1381. [self onBtnBeautyClicked:nil];
  1382. }
  1383. }
  1384. if (!_musicView.hidden) {
  1385. CGPoint _touchPoint = [[[event allTouches] anyObject] locationInView:self.view];
  1386. if (NO == CGRectContainsPoint(_musicView.frame, _touchPoint)){
  1387. _musicView.hidden = !_musicView.hidden;
  1388. [self hideBottomView:!_musicView.hidden];
  1389. }
  1390. }
  1391. }
  1392. - (void)hideBottomView:(BOOL)bHide
  1393. {
  1394. _recordTimeLabel.hidden = bHide;
  1395. _bottomMask.hidden = bHide;
  1396. }
  1397. -(void)refreshRecordTime:(CGFloat)second
  1398. {
  1399. _currentRecordTime = second;
  1400. [_progressView update:_currentRecordTime / [[GlobalVariables sharedInstance].appModel.sts_video_limit floatValue]];
  1401. long min = (int)_currentRecordTime / 60;
  1402. long sec = (int)_currentRecordTime % 60;
  1403. [_recordTimeLabel setText:[NSString stringWithFormat:@"%02ld:%02ld", min, sec]];
  1404. }
  1405. #pragma mark TXUGCRecordListener
  1406. -(void) onRecordProgress:(NSInteger)milliSecond;
  1407. {
  1408. _recordTime = milliSecond / 1000.0;
  1409. [self refreshRecordTime: _recordTime];
  1410. BOOL isEmpty = milliSecond == 0;
  1411. //录制过程中不能切换BGM, 不能改变声音效果
  1412. _btnMusic.enabled = isEmpty;
  1413. _btnNext.enabled = milliSecond / 1000.0 >= MIN_RECORD_TIME;
  1414. _btnAudioMix.enabled = _btnMusic.enabled;
  1415. //回删之后被模仿视频进度回退
  1416. if (_isBackDelete && _recordType == RecordType_Chorus) {
  1417. [_videoEditer previewAtTime:_recordTime];
  1418. _isBackDelete = NO;
  1419. }
  1420. }
  1421. -(void) onRecordComplete:(TXUGCRecordResult*)result;
  1422. {
  1423. if (_appForeground)
  1424. {
  1425. if (_currentRecordTime >= MIN_RECORD_TIME)
  1426. {
  1427. if (result.retCode != UGC_RECORD_RESULT_FAILED) {
  1428. [self stopVideoRecord];
  1429. }else{
  1430. [self toastTip:NSLocalizedString(@"TCVideoRecordView.ErrorREC", nil)];
  1431. }
  1432. } else {
  1433. [self toastTip:NSLocalizedString(@"TCVideoRecordView.ErrorTime", nil)];
  1434. }
  1435. }
  1436. }
  1437. #pragma mark TXVideoJoinerListener
  1438. -(void) onJoinProgress:(float)progress
  1439. {
  1440. _hub.label.text = [NSString stringWithFormat:@"%@%d%%",NSLocalizedString(@"TCVideoEditPrevView.VideoSynthesizing",nil), (int)(progress * 100)];
  1441. }
  1442. -(void) onJoinComplete:(TXJoinerResult *)result
  1443. {
  1444. [_hub hideAnimated:YES];
  1445. if (_appForeground && result.retCode == RECORD_RESULT_OK) {
  1446. [self stopCameraPreview];
  1447. // TCVideoEditViewController *vc = [[TCVideoEditViewController alloc] init];
  1448. // vc.videoPath = _joinVideoPath;
  1449. // vc.isFromChorus = YES;
  1450. // [[AppDelegate sharedAppDelegate]pushViewController:vc];
  1451. // [self.navigationController pushViewController:vc animated:YES];
  1452. }else{
  1453. UIAlertView *alert = [UIAlertView bk_showAlertViewWithTitle:NSLocalizedString(@"TCVideoRecordView.VideoJoinerFailed", nil) message:result.descMsg cancelButtonTitle:NSLocalizedString(@"Common.OK", nil) otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
  1454. }];
  1455. [alert show];
  1456. }
  1457. [TCUtil report:xiaoshipin_videojoiner userName:nil code:result.retCode msg:result.descMsg];
  1458. }
  1459. #if POD_PITU
  1460. - (void)motionTmplSelected:(NSString *)materialID {
  1461. if (materialID == nil) {
  1462. [MCTip hideText];
  1463. }
  1464. _materialID = materialID;
  1465. if ([MaterialManager isOnlinePackage:materialID]) {
  1466. [[TXUGCRecord shareInstance] selectMotionTmpl:materialID inDir:[MaterialManager packageDownloadDir]];
  1467. } else {
  1468. NSString *localPackageDir = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Resource"];
  1469. [[TXUGCRecord shareInstance] selectMotionTmpl:materialID inDir:localPackageDir];
  1470. }
  1471. }
  1472. #endif
  1473. #pragma mark - HorizontalPickerView DataSource
  1474. - (NSInteger)numberOfElementsInHorizontalPickerView:(V8HorizontalPickerView *)picker {
  1475. if (picker == _greenPickerView) {
  1476. return [_greenArray count];
  1477. } else if(picker == _filterPickerView) {
  1478. return [_filterArray count];
  1479. }
  1480. return 0;
  1481. }
  1482. #pragma mark - BeautyLoadPituDelegate
  1483. - (void)onLoadPituStart
  1484. {
  1485. dispatch_async(dispatch_get_main_queue(), ^{
  1486. _hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  1487. _hub.removeFromSuperViewOnHide = YES;
  1488. _hub.mode = MBProgressHUDModeText;
  1489. _hub.label.text = NSLocalizedString(@"TCVideoRecordView.ResourceLoadBegin", nil);
  1490. });
  1491. }
  1492. - (void)onLoadPituProgress:(CGFloat)progress
  1493. {
  1494. dispatch_async(dispatch_get_main_queue(), ^{
  1495. _hub.label.text = [NSString stringWithFormat:NSLocalizedString(@"TCVideoRecordView.ResourceLoading", nil),(int)(progress * 100)];
  1496. });
  1497. }
  1498. - (void)onLoadPituFinished
  1499. {
  1500. dispatch_async(dispatch_get_main_queue(), ^{
  1501. _hub.label.text = NSLocalizedString(@"TCVideoRecordView.ResourceLoadSucceeded", nil);
  1502. [_hub hideAnimated:YES afterDelay:1];
  1503. });
  1504. }
  1505. - (void)onLoadPituFailed
  1506. {
  1507. dispatch_async(dispatch_get_main_queue(), ^{
  1508. _hub.label.text = NSLocalizedString(@"TCVideoRecordView.ResourceLoadFailed", nil);
  1509. [_hub hideAnimated:YES afterDelay:1];
  1510. });
  1511. }
  1512. #pragma mark - BeautySettingPanelDelegate
  1513. - (void)onSetBeautyStyle:(TXVideoBeautyStyle)beautyStyle beautyLevel:(float)beautyLevel whitenessLevel:(float)whitenessLevel ruddinessLevel:(float)ruddinessLevel{
  1514. _beautyStyle = beautyStyle;
  1515. _beautyDepth = beautyLevel;
  1516. _whitenDepth = whitenessLevel;
  1517. _ruddinessDepth = ruddinessLevel;
  1518. [[TXUGCRecord shareInstance] setBeautyStyle:_beautyStyle beautyLevel:_beautyDepth whitenessLevel:_whitenDepth ruddinessLevel:_ruddinessDepth];
  1519. }
  1520. - (void)onSetEyeScaleLevel:(float)eyeScaleLevel
  1521. {
  1522. _eye_level = eyeScaleLevel;
  1523. [[TXUGCRecord shareInstance] setEyeScaleLevel:_eye_level];
  1524. }
  1525. - (void)onSetFaceScaleLevel:(float)faceScaleLevel
  1526. {
  1527. _face_level = faceScaleLevel;
  1528. [[TXUGCRecord shareInstance] setFaceScaleLevel:_face_level];
  1529. }
  1530. - (void)onSetFilter:(UIImage*)filterImage
  1531. {
  1532. [[TXUGCRecord shareInstance] setFilter:filterImage];
  1533. }
  1534. - (void)onSetGreenScreenFile:(NSURL *)file
  1535. {
  1536. [[TXUGCRecord shareInstance] setGreenScreenFile:file];
  1537. }
  1538. - (void)onSelectMotionTmpl:(NSString *)tmplName inDir:(NSString *)tmplDir
  1539. {
  1540. [[TXUGCRecord shareInstance] selectMotionTmpl:tmplName inDir:tmplDir];
  1541. }
  1542. - (void)onSetFaceVLevel:(float)faceVLevel{
  1543. [[TXUGCRecord shareInstance] setFaceVLevel:faceVLevel];
  1544. }
  1545. - (void)onSetChinLevel:(float)chinLevel{
  1546. [[TXUGCRecord shareInstance] setChinLevel:chinLevel];
  1547. }
  1548. - (void)onSetNoseSlimLevel:(float)slimLevel{
  1549. [[TXUGCRecord shareInstance] setNoseSlimLevel:slimLevel];
  1550. }
  1551. - (void)onSetFaceShortLevel:(float)faceShortlevel{
  1552. [[TXUGCRecord shareInstance] setFaceShortLevel:faceShortlevel];
  1553. }
  1554. - (void)onSetMixLevel:(float)mixLevel{
  1555. [[TXUGCRecord shareInstance] setSpecialRatio:mixLevel / 10.0];
  1556. }
  1557. - (void)onSetFaceBeautyLevel:(float)beautyLevel {
  1558. // None
  1559. }
  1560. #pragma mark TCBGMControllerListener
  1561. -(void) onBGMControllerPlay:(NSObject*) path{
  1562. if(path == nil) return;
  1563. [self onSetBGM:path];
  1564. //试听音乐这里要把RecordSpeed 设置为VIDEO_RECORD_SPEED_NOMAL,否则音乐可能会出现加速或则慢速播现象
  1565. [[TXUGCRecord shareInstance] setRecordSpeed:VIDEO_RECORD_SPEED_NOMAL];
  1566. [self playBGM:0];
  1567. dispatch_async(dispatch_get_main_queue(), ^(){
  1568. [_musicView resetCutView];
  1569. if(_musicView.hidden){
  1570. _musicView.hidden = !_musicView.hidden;
  1571. [self hideBottomView:!_musicView.hidden];
  1572. }
  1573. });
  1574. }
  1575. #pragma mark - SoundMixView
  1576. #pragma mark * SoundMixViewDelegate
  1577. - (void)soundMixView:(SoundMixView *)view didSelectMixIndex:(NSInteger)index
  1578. {
  1579. _soundMixChangeType = index;
  1580. [[TXUGCRecord shareInstance] setReverbType:index];
  1581. }
  1582. - (void)soundMixView:(SoundMixView *)view didSelectVoiceChangeIndex:(NSInteger)index
  1583. {
  1584. if (index >= VIDOE_VOICECHANGER_TYPE_5) {
  1585. // 去掉感冒
  1586. index ++;
  1587. }
  1588. _voiceChangeType = index;
  1589. [[TXUGCRecord shareInstance] setVoiceChangerType:index];
  1590. }
  1591. #pragma mark - VideoRecordMusicViewDelegate
  1592. -(void)selectAudioEffect:(NSInteger)index
  1593. {
  1594. [[TXUGCRecord shareInstance] setReverbType:index];
  1595. }
  1596. -(void)selectAudioEffect2:(NSInteger)index
  1597. {
  1598. [[TXUGCRecord shareInstance] setVoiceChangerType:index];
  1599. }
  1600. -(void)onBtnMusicSelected
  1601. {
  1602. // UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:_bgmListVC];
  1603. //
  1604. // [nv.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
  1605. // nv.navigationBar.barTintColor = RGB(25, 29, 38);
  1606. // [self presentViewController:nv animated:YES completion:nil];
  1607. // [_bgmListVC loadBGMList];
  1608. [self presetTonew_bgmList];
  1609. }
  1610. -(void)onBtnMusicStoped
  1611. {
  1612. _BGMPath = nil;
  1613. _bgmRecording = NO;
  1614. [[TXUGCRecord shareInstance] stopBGM];
  1615. if (!_musicView.hidden) {
  1616. _musicView.hidden = !_musicView.hidden;
  1617. [self hideBottomView:!_musicView.hidden];
  1618. }
  1619. }
  1620. -(void)onBGMValueChange:(CGFloat)value
  1621. {
  1622. [[TXUGCRecord shareInstance] setBGMVolume:value];
  1623. }
  1624. -(void)onVoiceValueChange:(CGFloat)value
  1625. {
  1626. [[TXUGCRecord shareInstance] setMicVolume:value];
  1627. }
  1628. -(void)onBGMRangeChange:(CGFloat)startPercent endPercent:(CGFloat)endPercent
  1629. {
  1630. //切换bgm 范围的时候,bgm录制状态置NO
  1631. _bgmRecording = NO;
  1632. //试听音乐这里要把RecordSpeed 设置为VIDEO_RECORD_SPEED_NOMAL,否则音乐可能会出现加速或则慢速播现象
  1633. [[TXUGCRecord shareInstance] setRecordSpeed:VIDEO_RECORD_SPEED_NOMAL];
  1634. [self playBGM:_BGMDuration * startPercent toTime:_BGMDuration * endPercent];
  1635. }
  1636. -(void)onSetBGM:(NSObject *)path
  1637. {
  1638. _BGMPath = path;
  1639. if([_BGMPath isKindOfClass:[NSString class]]){
  1640. _BGMDuration = [[TXUGCRecord shareInstance] setBGM:(NSString *)_BGMPath];
  1641. }else{
  1642. _BGMDuration = [[TXUGCRecord shareInstance] setBGMAsset:(AVAsset *)_BGMPath];
  1643. }
  1644. _bgmRecording = NO;
  1645. dispatch_async(dispatch_get_main_queue(), ^{
  1646. [MBProgressHUD hideHUDForView:self.view animated:YES];
  1647. });
  1648. }
  1649. -(void)playBGM:(CGFloat)beginTime{
  1650. if (_BGMPath != nil) {
  1651. [[TXUGCRecord shareInstance] playBGMFromTime:beginTime toTime:_BGMDuration withBeginNotify:^(NSInteger errCode) {
  1652. NSLog(@"%ld",errCode);
  1653. } withProgressNotify:^(NSInteger progressMS, NSInteger durationMS) {
  1654. NSLog(@"%ld",durationMS);
  1655. } andCompleteNotify:^(NSInteger errCode) {
  1656. NSLog(@"%ld",errCode);
  1657. }];
  1658. _bgmBeginTime = beginTime;
  1659. }
  1660. }
  1661. -(void)playBGM:(CGFloat)beginTime toTime:(CGFloat)endTime
  1662. {
  1663. if (_BGMPath != nil) {
  1664. [[TXUGCRecord shareInstance] playBGMFromTime:beginTime toTime:endTime withBeginNotify:^(NSInteger errCode) {
  1665. } withProgressNotify:^(NSInteger progressMS, NSInteger durationMS) {
  1666. } andCompleteNotify:^(NSInteger errCode) {
  1667. }];
  1668. _bgmBeginTime = beginTime;
  1669. }
  1670. }
  1671. -(void)pauseBGM{
  1672. if (_BGMPath != nil) {
  1673. [[TXUGCRecord shareInstance] pauseBGM];
  1674. }
  1675. }
  1676. - (void)resumeBGM
  1677. {
  1678. if (_BGMPath != nil) {
  1679. [[TXUGCRecord shareInstance] resumeBGM];
  1680. }
  1681. }
  1682. #pragma mark - HorizontalPickerView Delegate Methods
  1683. - (UIView *)horizontalPickerView:(V8HorizontalPickerView *)picker viewForElementAtIndex:(NSInteger)index {
  1684. if (picker == _greenPickerView) {
  1685. V8LabelNode *v = [_greenArray objectAtIndex:index];
  1686. return [[UIImageView alloc] initWithImage:v.face];
  1687. } else if(picker == _filterPickerView) {
  1688. V8LabelNode *v = [_filterArray objectAtIndex:index];
  1689. return [[UIImageView alloc] initWithImage:v.face];
  1690. }
  1691. return nil;
  1692. }
  1693. - (NSInteger) horizontalPickerView:(V8HorizontalPickerView *)picker widthForElementAtIndex:(NSInteger)index {
  1694. if (picker == _greenPickerView) {
  1695. return 70;
  1696. }
  1697. return 90;
  1698. }
  1699. - (void)horizontalPickerView:(V8HorizontalPickerView *)picker didSelectElementAtIndex:(NSInteger)index
  1700. {
  1701. if (picker == _greenPickerView) {
  1702. _greenIndex = index;
  1703. V8LabelNode *v = [_greenArray objectAtIndex:index];
  1704. [[TXUGCRecord shareInstance] setGreenScreenFile:v.file];
  1705. return;
  1706. }
  1707. if (picker == _filterPickerView) {
  1708. _filterIndex = index;
  1709. [self setFilter:_filterIndex];
  1710. }
  1711. }
  1712. - (void)setFilter:(NSInteger)index
  1713. {
  1714. NSString* lookupFileName = @"";
  1715. switch (index) {
  1716. case FilterType_None:
  1717. break;
  1718. case FilterType_biaozhun:
  1719. lookupFileName = @"filter_biaozhun";
  1720. break;
  1721. case FilterType_yinghong:
  1722. lookupFileName = @"filter_yinghong";
  1723. break;
  1724. case FilterType_yunshang:
  1725. lookupFileName = @"filter_yunshang";
  1726. break;
  1727. case FilterType_chunzhen:
  1728. lookupFileName = @"filter_chunzhen";
  1729. break;
  1730. case FilterType_bailan:
  1731. lookupFileName = @"filter_bailan";
  1732. break;
  1733. case FilterType_yuanqi:
  1734. lookupFileName = @"filter_yuanqi";
  1735. break;
  1736. case FilterType_chaotuo:
  1737. lookupFileName = @"filter_chaotuo";
  1738. break;
  1739. case FilterType_xiangfen:
  1740. lookupFileName = @"filter_xiangfen";
  1741. break;
  1742. case FilterType_white:
  1743. lookupFileName = @"filter_white";
  1744. break;
  1745. case FilterType_langman:
  1746. lookupFileName = @"filter_langman";
  1747. break;
  1748. case FilterType_qingxin:
  1749. lookupFileName = @"filter_qingxin";
  1750. break;
  1751. case FilterType_weimei:
  1752. lookupFileName = @"filter_weimei";
  1753. break;
  1754. case FilterType_fennen:
  1755. lookupFileName = @"filter_fennen";
  1756. break;
  1757. case FilterType_huaijiu:
  1758. lookupFileName = @"filter_huaijiu";
  1759. break;
  1760. case FilterType_landiao:
  1761. lookupFileName = @"filter_landiao";
  1762. break;
  1763. case FilterType_qingliang:
  1764. lookupFileName = @"filter_qingliang";
  1765. break;
  1766. case FilterType_rixi:
  1767. lookupFileName = @"filter_rixi";
  1768. break;
  1769. default:
  1770. break;
  1771. }
  1772. NSString * path = [[NSBundle mainBundle] pathForResource:lookupFileName ofType:@"png"];
  1773. if (path != nil && index != FilterType_None)
  1774. {
  1775. [[TXUGCRecord shareInstance] setFilter:[UIImage imageWithContentsOfFile:path]];
  1776. }
  1777. else
  1778. {
  1779. [[TXUGCRecord shareInstance] setFilter:nil];
  1780. }
  1781. }
  1782. #pragma mark - Misc Methods
  1783. - (float) heightForString:(UITextView *)textView andWidth:(float)width{
  1784. CGSize sizeToFit = [textView sizeThatFits:CGSizeMake(width, MAXFLOAT)];
  1785. return sizeToFit.height;
  1786. }
  1787. - (void) toastTip:(NSString*)toastInfo
  1788. {
  1789. CGRect frameRC = [[UIScreen mainScreen] bounds];
  1790. frameRC.origin.y = frameRC.size.height - 100;
  1791. frameRC.size.height -= 100;
  1792. __block UITextView * toastView = [[UITextView alloc] init];
  1793. toastView.editable = NO;
  1794. toastView.selectable = NO;
  1795. frameRC.size.height = [self heightForString:toastView andWidth:frameRC.size.width];
  1796. toastView.frame = frameRC;
  1797. toastView.text = toastInfo;
  1798. toastView.backgroundColor = [UIColor whiteColor];
  1799. toastView.alpha = 0.5;
  1800. [self.view addSubview:toastView];
  1801. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
  1802. dispatch_after(popTime, dispatch_get_main_queue(), ^(){
  1803. [toastView removeFromSuperview];
  1804. toastView = nil;
  1805. });
  1806. }
  1807. #pragma mark - gesture handler
  1808. - (void)handlePanSlide:(UIPanGestureRecognizer*)recognizer
  1809. {
  1810. CGPoint translation = [recognizer translationInView:self.view.superview];
  1811. [recognizer velocityInView:self.view];
  1812. CGPoint speed = [recognizer velocityInView:self.view];
  1813. NSLog(@"pan center:(%.2f)", translation.x);
  1814. NSLog(@"pan speed:(%.2f)", speed.x);
  1815. float ratio = translation.x / self.view.frame.size.width;
  1816. float leftRatio = ratio;
  1817. NSInteger index = [_vBeauty currentFilterIndex];
  1818. UIImage* curFilterImage = [_vBeauty filterImageByIndex:index];
  1819. UIImage* filterImage1 = nil;
  1820. UIImage* filterImage2 = nil;
  1821. CGFloat filter1Level = 0.f;
  1822. CGFloat filter2Level = 0.f;
  1823. if (leftRatio > 0) {
  1824. filterImage1 = [_vBeauty filterImageByIndex:index - 1];
  1825. filter1Level = [_vBeauty filterMixLevelByIndex:index - 1] / 10;
  1826. filterImage2 = curFilterImage;
  1827. filter2Level = [_vBeauty filterMixLevelByIndex:index] / 10;
  1828. }
  1829. else {
  1830. filterImage1 = curFilterImage;
  1831. filter1Level = [_vBeauty filterMixLevelByIndex:index] / 10;
  1832. filterImage2 = [_vBeauty filterImageByIndex:index + 1];
  1833. filter2Level = [_vBeauty filterMixLevelByIndex:index + 1] / 10;
  1834. leftRatio = 1 + leftRatio;
  1835. }
  1836. if (recognizer.state == UIGestureRecognizerStateChanged) {
  1837. [[TXUGCRecord shareInstance] setFilter:filterImage1 leftIntensity:filter1Level rightFilter:filterImage2 rightIntensity:filter2Level leftRatio:leftRatio];
  1838. }
  1839. else if (recognizer.state == UIGestureRecognizerStateEnded) {
  1840. BOOL isDependRadio = fabs(speed.x) < 500; //x方向的速度
  1841. [self animateFromFilter1:filterImage1 filter2:filterImage2 filter1MixLevel:filter1Level filter2MixLevel:filter2Level leftRadio:leftRatio speed:speed.x completion:^{
  1842. if (!isDependRadio) {
  1843. if (speed.x < 0) {
  1844. _vBeauty.currentFilterIndex = index + 1;
  1845. }
  1846. else {
  1847. _vBeauty.currentFilterIndex = index - 1;
  1848. }
  1849. }
  1850. else {
  1851. if (ratio > 0.5) { //过半或者速度>500就切换
  1852. _vBeauty.currentFilterIndex = index - 1;
  1853. }
  1854. else if (ratio < -0.5) {
  1855. _vBeauty.currentFilterIndex = index + 1;
  1856. }
  1857. }
  1858. UILabel* filterTipLabel = [UILabel new];
  1859. filterTipLabel.text = [_vBeauty currentFilterName];
  1860. filterTipLabel.font = [UIFont systemFontOfSize:30];
  1861. filterTipLabel.textColor = UIColor.whiteColor;
  1862. filterTipLabel.alpha = 0.1;
  1863. [filterTipLabel sizeToFit];
  1864. filterTipLabel.center = CGPointMake(self.view.size.width / 2, self.view.size.height / 3);
  1865. [self.view addSubview:filterTipLabel];
  1866. [UIView animateWithDuration:0.25 animations:^{
  1867. filterTipLabel.alpha = 1;
  1868. } completion:^(BOOL finished) {
  1869. [UIView animateWithDuration:0.25 delay:0.25 options:UIViewAnimationOptionCurveLinear animations:^{
  1870. filterTipLabel.alpha = 0.1;
  1871. } completion:^(BOOL finished) {
  1872. [filterTipLabel removeFromSuperview];
  1873. }];
  1874. }];
  1875. }];
  1876. }
  1877. }
  1878. - (void)animateFromFilter1:(UIImage*)filter1Image filter2:(UIImage*)filter2Image filter1MixLevel:(CGFloat)filter1MixLevel filter2MixLevel:(CGFloat)filter2MixLevel leftRadio:(CGFloat)leftRadio speed:(CGFloat)speed completion:(void(^)(void))completion
  1879. {
  1880. if (leftRadio <= 0 || leftRadio >= 1) {
  1881. completion();
  1882. return;
  1883. }
  1884. static float delta = 1.f / 12;
  1885. BOOL isDependRadio = fabs(speed) < 500;
  1886. if (isDependRadio) {
  1887. if (leftRadio < 0.5) {
  1888. leftRadio -= delta;
  1889. }
  1890. else {
  1891. leftRadio += delta;
  1892. }
  1893. }
  1894. else {
  1895. if (speed > 0) {
  1896. leftRadio += delta;
  1897. }
  1898. else
  1899. leftRadio -= delta;
  1900. }
  1901. [[TXUGCRecord shareInstance] setFilter:filter1Image leftIntensity:filter1MixLevel rightFilter:filter2Image rightIntensity:filter2MixLevel leftRatio:leftRadio];
  1902. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f / 30 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1903. [self animateFromFilter1:filter1Image filter2:filter2Image filter1MixLevel:filter1MixLevel filter2MixLevel:filter2MixLevel leftRadio:leftRadio speed:speed completion:completion];
  1904. });
  1905. }
  1906. - (void)uinit{
  1907. [[TXUGCRecord shareInstance] stopRecord];
  1908. [[TXUGCRecord shareInstance] stopCameraPreview];
  1909. [[TXUGCRecord shareInstance].partsManager deleteAllParts];
  1910. if (!_savePath) {
  1911. [TCUtil removeCacheFile:_videoPath];
  1912. }
  1913. [TCUtil removeCacheFile:_recordVideoPath];
  1914. [TCUtil removeCacheFile:_joinVideoPath];
  1915. }
  1916. - (void)viewDidDisappear:(BOOL)animated
  1917. {
  1918. [super viewDidDisappear:animated];
  1919. [[TXUGCRecord shareInstance] pauseBGM];
  1920. }
  1921. - (void)dealloc
  1922. {
  1923. [self uinit];
  1924. [[NSNotificationCenter defaultCenter] removeObserver:self];
  1925. }
  1926. @end