recordVideoViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #import "recordVideoViewController.h"
  2. #import "JXConvertMedia.h"
  3. #import "JXCaptureMedia.h"
  4. #import "JXImageView.h"
  5. @implementation recordVideoViewController
  6. @synthesize delegate;
  7. @synthesize didRecord;
  8. @synthesize timeLen;
  9. @synthesize outputFileName;
  10. @synthesize recorder=_capture;
  11. - (id)init
  12. {
  13. self = [super init];
  14. _pSelf = self;
  15. [UIApplication sharedApplication].statusBarHidden = YES;
  16. self.view.backgroundColor = HEXCOLOR(0x2e2f2f);
  17. preview = [[UIView alloc] initWithFrame:g_window.bounds];
  18. [self.view addSubview:preview];
  19. // [preview release];
  20. _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_HEIGHT-(JX_SCREEN_HEIGHT-JX_SCREEN_WIDTH)/2, JX_SCREEN_WIDTH, (JX_SCREEN_HEIGHT-JX_SCREEN_WIDTH)/2)];
  21. _bottomView.backgroundColor = [UIColor clearColor];
  22. [self.view addSubview:_bottomView];
  23. // [_bottomView release];
  24. CGFloat btnW = _bottomView.frame.size.height/1.8;
  25. _recrod = [UIButton buttonWithType:UIButtonTypeCustom];
  26. _recrod.frame = CGRectMake((JX_SCREEN_WIDTH-btnW)/2, 10, btnW, btnW);
  27. // _recrod.center = CGPointMake(_recrod.center.x, _recrod.);
  28. [_recrod setBackgroundImage:[UIImage imageNamed:@"recordvideo_normal"] forState:UIControlStateNormal];
  29. [_recrod setBackgroundImage:[UIImage imageNamed:@"play_press"] forState:UIControlStateSelected];
  30. [_recrod addTarget:self action:@selector(record:) forControlEvents:UIControlEventTouchUpInside];
  31. [_bottomView addSubview:_recrod];
  32. _recordLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 17)];
  33. _recordLabel.center = CGPointMake(_recrod.center.x, _recrod.center.y+(CGRectGetHeight(_recrod.frame)+25)/2);
  34. _recordLabel.text = Localized(@"JX_Recorder");
  35. _recordLabel.textColor = [UIColor whiteColor];
  36. _recordLabel.font = SYSFONT(12);
  37. _recordLabel.backgroundColor = [UIColor clearColor];
  38. _recordLabel.textAlignment = NSTextAlignmentCenter;
  39. [_bottomView addSubview:_recordLabel];
  40. // [_recordLabel release];
  41. _close = [[JXImageView alloc]initWithFrame:CGRectMake(52, _recrod.frame.origin.y, 31, 31)];
  42. _close.center = CGPointMake(_close.center.x, _recrod.center.y);
  43. _close.image = [UIImage imageNamed:@"fork"];
  44. _close.userInteractionEnabled = YES;
  45. _close.delegate = self;
  46. _close.didTouch = @selector(onQuit);
  47. [_bottomView addSubview:_close];
  48. // [_close release];
  49. _save = [[JXImageView alloc]initWithFrame:CGRectMake(JX_SCREEN_WIDTH-_close.frame.origin.x-_close.frame.size.width, _close.frame.origin.y, 31, 31)];
  50. _save.image = [UIImage imageNamed:@"tick"];
  51. _save.userInteractionEnabled = YES;
  52. _save.delegate = self;
  53. _save.didTouch = @selector(onSave);
  54. [_bottomView addSubview:_save];
  55. // [_save release];
  56. _flash = [[JXImageView alloc]initWithFrame:CGRectMake(16, 10, 30, 30)];
  57. _flash.image = [UIImage imageNamed:@"automatic"];
  58. _flash.userInteractionEnabled = YES;
  59. _flash.delegate = self;
  60. _flash.didTouch = @selector(flash);
  61. [self.view addSubview:_flash];
  62. // [_flash release];
  63. _flashOn = [[JXImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_flash.frame)+20, CGRectGetMinY(_flash.frame), 30, 30)];
  64. _flashOn.image = [UIImage imageNamed:@"flash_on"];
  65. _flashOn.userInteractionEnabled = YES;
  66. _flashOn.delegate = self;
  67. _flashOn.didTouch = @selector(flashOn);
  68. _flashOn.hidden = YES;
  69. [self.view addSubview:_flashOn];
  70. // [_flashOn release];
  71. _flashOff = [[JXImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_flashOn.frame)+20, CGRectGetMinY(_flash.frame), 30, 30)];
  72. _flashOff.image = [UIImage imageNamed:@"flash_off"];
  73. _flashOff.userInteractionEnabled = YES;
  74. _flashOff.delegate = self;
  75. _flashOff.didTouch = @selector(flashOff);
  76. _flashOff.hidden = YES;
  77. [self.view addSubview:_flashOff];
  78. // [_flashOff release];
  79. _cammer = [[JXImageView alloc]initWithFrame:CGRectMake(JX_SCREEN_WIDTH-16-30, CGRectGetMinY(_flash.frame), 30, 27)];
  80. _cammer.image = [UIImage imageNamed:@"switch_cammer"];
  81. _cammer.userInteractionEnabled = YES;
  82. _cammer.delegate = self;
  83. _cammer.didTouch = @selector(toggle);
  84. [self.view addSubview:_cammer];
  85. // [_cammer release];
  86. //时间
  87. _timeBGView = [[UIImageView alloc] initWithFrame:CGRectMake((JX_SCREEN_WIDTH-210)/2, (JX_SCREEN_HEIGHT-JX_SCREEN_WIDTH)/2-35, 210, 2)];
  88. _timeBGView.image = [UIImage imageNamed:@"time_axis"];
  89. _timeBGView.hidden =YES;
  90. [self.view addSubview:_timeBGView];
  91. _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 55, 30)];
  92. _timeLabel.center = _timeBGView.center;
  93. _timeLabel.text = @"";
  94. _timeLabel.textAlignment = NSTextAlignmentCenter;
  95. _timeLabel.textColor = [UIColor whiteColor];
  96. _timeLabel.shadowColor = [UIColor blackColor];
  97. _timeLabel.shadowOffset = CGSizeMake(1, 1);
  98. _timeLabel.backgroundColor = [UIColor clearColor];
  99. [self.view addSubview:_timeLabel];
  100. // [_timeLabel release];
  101. // [_timeBGView release];
  102. _noticeLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, JX_SCREEN_WIDTH-45*2, 45)];
  103. _noticeLabel.center = self.view.center;
  104. _noticeLabel.textColor = [UIColor whiteColor];
  105. _noticeLabel.font = SYSFONT(15);
  106. _noticeLabel.numberOfLines = 2;
  107. _noticeLabel.backgroundColor = [UIColor clearColor];
  108. _noticeLabel.textAlignment = NSTextAlignmentCenter;
  109. [self.view addSubview:_noticeLabel];
  110. // [_noticeLabel release];
  111. [self noticeLabelHidden:NO textType:1];
  112. [self initCapture];
  113. return self;
  114. }
  115. - (void)initCapture{
  116. _capture = [[JXCaptureMedia alloc]init];
  117. // _capture.logoImage = [UIImage imageNamed:@"logo"];
  118. _capture.logoRect = CGRectMake(0, JX_SCREEN_WIDTH-100, 100, 100);
  119. _capture.saveVideoToImage = 1;
  120. _capture.maxTime = _maxTime;
  121. _capture.isOnlySaveFirstImage = !_isShowSaveImage;
  122. _capture.labelTime = _timeLabel;
  123. _capture.isReciprocal = _isReciprocal;
  124. _capture.isRecordAudio = YES;
  125. _capture.isEditVideo = YES;
  126. _capture.videoWidth = JX_SCREEN_HEIGHT;
  127. _capture.videoHeight = JX_SCREEN_WIDTH;
  128. _capture.outputFileName = [FileInfo getUUIDFileName:@"mp4"];
  129. if(![_capture createPreview:preview]){
  130. [self performSelector:@selector(onQuit) withObject:nil afterDelay:1];
  131. return;
  132. }
  133. [g_notify addObserver:self selector:@selector(recordAutoEnd:) name:kVideoRecordEndNotifaction object:nil];//开始录音
  134. }
  135. - (void)dealloc {
  136. NSLog(@"recordVideoViewController.dealloc");
  137. [UIApplication sharedApplication].statusBarHidden = NO;
  138. // [super dealloc];
  139. }
  140. - (void)onSave{
  141. if(_capture.isRecording){
  142. if(_capture.timeLen < _minTime && _minTime > 0){
  143. [self noticeLabelHidden:NO textType:2];
  144. [self performSelector:@selector(hiddenNoticeLabel) withObject:nil afterDelay:3];
  145. return;
  146. }
  147. [_capture stop];
  148. }
  149. if(_isShowSaveImage && [_capture.outputImageFiles count]){
  150. ImageSelectorViewController * imageSelectVC = [[ImageSelectorViewController alloc] init];
  151. imageSelectVC.imageFileNameArray = _capture.outputImageFiles;
  152. imageSelectVC.imgDelegete = self;
  153. imageSelectVC.title = Localized(@"ImageSelectorVC_SelImage");
  154. // [g_window addSubview:imageSelectVC.view];
  155. [g_navigation pushViewController:imageSelectVC animated:YES];
  156. }else{
  157. [self notifyWithImage:nil];
  158. [self showPreview];
  159. // [self doQuit];
  160. }
  161. }
  162. -(void)notifyWithImage:(NSString *)imagePath{
  163. if (imagePath) {
  164. self.outputImage = imagePath;
  165. }else{
  166. if (_capture.outputImageFiles.count > 0)
  167. self.outputImage = _capture.outputImageFiles[0];
  168. }
  169. self.outputFileName = _capture.outputFileName;
  170. self.timeLen = (int)_capture.timeLen;
  171. if(_capture.timeLen>0)
  172. if (delegate && [delegate respondsToSelector:didRecord])
  173. // [delegate performSelector:didRecord withObject:self];
  174. [delegate performSelectorOnMainThread:didRecord withObject:self waitUntilDone:NO];
  175. }
  176. -(void)imageSelectorDidiSelectImage:(NSString *)imagePath{
  177. NSLog(@"imageSelectorDidiSelectImage:%@",imagePath);
  178. [self notifyWithImage:imagePath];
  179. [self doQuit];
  180. }
  181. - (void)toggle{
  182. [_capture toggleCamera];
  183. if(_capture.isFrontFace)
  184. [self flashOff];
  185. }
  186. - (void)start{
  187. [_capture start];
  188. }
  189. - (void)stop{
  190. [_capture stop];
  191. }
  192. - (IBAction)record:(UIButton*)sender{
  193. _recrod.selected = !_recrod.selected;
  194. if(_capture.isRecording){
  195. if(_capture.timeLen < _minTime && _minTime > 0){
  196. [self noticeLabelHidden:NO textType:2];
  197. [self performSelector:@selector(hiddenNoticeLabel) withObject:nil afterDelay:3];
  198. return;
  199. }
  200. _timeBGView.hidden = YES;
  201. _timeLabel.hidden = YES;
  202. _recordLabel.hidden = NO;
  203. // [self noticeLabelHidden:NO textType:1];
  204. [_capture stop];
  205. [self onSave];
  206. // if(self.didRecord)
  207. // [self onSave];
  208. // else
  209. // [_capture stop];
  210. }else{
  211. _timeBGView.hidden = NO;
  212. _timeLabel.hidden = NO;
  213. _recordLabel.hidden = YES;
  214. [self noticeLabelHidden:YES textType:1];
  215. [_capture clearTempFile];
  216. [_capture start];
  217. }
  218. // _recrod.selected = _capture.isRecording;
  219. }
  220. - (void)showPreview {
  221. _playerView = [[UIView alloc] initWithFrame:self.view.bounds];
  222. [self.view addSubview:_playerView];
  223. _player= [JXVideoPlayer alloc];
  224. _player.type = JXVideoTypePreview;
  225. _player.isShowHide = YES; //播放中点击播放器便销毁播放器
  226. _player.didSendBtn = @selector(didSendBtn:);
  227. _player.isStartFullScreenPlay = YES; //全屏播放
  228. _player.isPreview = YES; // 这是预览
  229. _player.delegate = self;
  230. _player = [_player initWithParent:_playerView];
  231. _player.parent = _playerView;
  232. _player.videoFile = self.outputFileName;
  233. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  234. [_player switch];
  235. });
  236. }
  237. -(void)flashOn{
  238. _flashOn.hidden = YES;
  239. _flashOff.hidden = YES;
  240. _flash.image = [UIImage imageNamed:@"flash_on"];
  241. _capture.curFlashMode = AVCaptureFlashModeOn;
  242. }
  243. -(void)flashOff{
  244. _flashOn.hidden = YES;
  245. _flashOff.hidden = YES;
  246. _flash.image = [UIImage imageNamed:@"flash_off"];
  247. _capture.curFlashMode = AVCaptureFlashModeOff;
  248. }
  249. - (void)flash{
  250. if(_flashOn.hidden){
  251. if(_capture.isFrontFace)
  252. return;
  253. CGContextRef context = UIGraphicsGetCurrentContext();
  254. [UIView beginAnimations:nil context:context];
  255. [UIView setAnimationCurve:UIViewAnimationCurveLinear];
  256. [UIView setAnimationDuration:1];
  257. _flashOn.hidden = NO;
  258. _flashOff.hidden = NO;
  259. _flash.image = [UIImage imageNamed:@"automatic"];
  260. [UIView commitAnimations];
  261. return;
  262. }
  263. _flashOn.hidden = YES;
  264. _flashOff.hidden = YES;
  265. _flash.image = [UIImage imageNamed:@"automatic"];
  266. _capture.curFlashMode = AVCaptureFlashModeAuto;
  267. }
  268. -(void)recordAutoEnd:(NSNotification*)notification{
  269. NSLog(@"sssss");
  270. dispatch_async(dispatch_get_main_queue(), ^{
  271. [self onSave];
  272. });
  273. }
  274. -(void)noticeLabelHidden:(BOOL)hide textType:(int)type{
  275. _noticeLabel.hidden = hide;
  276. NSString * showStr = nil;
  277. switch (type) {
  278. case 1:
  279. showStr = [NSString stringWithFormat:@"%@%d%@",Localized(@"recordVideoVC_Show1"),_maxTime,Localized(@"recordVideoVC_Show2")];
  280. break;
  281. case 2:
  282. showStr = [NSString stringWithFormat:@"%@%ds",Localized(@"recordVideoViewController_LessThan"),_minTime];
  283. break;
  284. default:
  285. break;
  286. }
  287. _noticeLabel.text = showStr;
  288. }
  289. -(void)hiddenNoticeLabel{
  290. [self noticeLabelHidden:YES textType:1];
  291. }
  292. -(void)doQuit{
  293. // [_capture release];
  294. _capture = nil;
  295. [g_notify removeObserver:self name:kVideoRecordEndNotifaction object:nil];
  296. // [self.view removeFromSuperview];
  297. [g_navigation dismissViewController:self animated:YES];
  298. // [self release];
  299. _pSelf = nil;
  300. }
  301. -(void)onQuit{
  302. if(_capture.isRecording){
  303. [_capture stop];
  304. [_capture.captureSession startRunning];
  305. _recrod.selected = NO;
  306. _timeLabel.hidden = NO;
  307. _recordLabel.hidden = NO;
  308. _timeBGView.hidden = YES;
  309. _timeLabel.text = @"00:00";
  310. _timeLabel.hidden = YES;
  311. }else{
  312. [_capture.captureSession stopRunning];
  313. [self doQuit];
  314. }
  315. }
  316. @end