TCVideoEditPrevViewController.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // TCVideoEditPrevController
  3. // TCLVBIMDemo
  4. //
  5. // Created by annidyfeng on 2017/4/19.
  6. // Copyright © 2017年 tencent. All rights reserved.
  7. //
  8. #import "TCVideoEditPrevViewController.h"
  9. #import "TCVideoPublishController.h"
  10. #import "SDKHeader.h"
  11. #import <MBProgressHUD/MBProgressHUD.h>
  12. typedef NS_ENUM(NSInteger,ActionType)
  13. {
  14. ActionType_Save,
  15. ActionType_Publish,
  16. ActionType_Save_Publish,
  17. };
  18. @interface TCVideoEditPrevViewController ()<TXVideoGenerateListener,TXVideoJoinerListener,VideoPreviewDelegate>
  19. @end
  20. @implementation TCVideoEditPrevViewController {
  21. VideoPreview *_videoPreview;
  22. TXVideoJoiner *_ugcJoin;
  23. TXVideoEditer *_ugcEdit;
  24. CGFloat _currentPos;
  25. BOOL _saveToLocal;
  26. NSString *_outFilePath;
  27. ActionType _actionType;
  28. UILabel* _generationTitleLabel;
  29. UIView* _generationView;
  30. UIProgressView* _generateProgressView;
  31. UIButton* _generateCannelBtn;
  32. }
  33. //- (id)init
  34. //{
  35. // self = [super initWithNibName:@"Myview" bundle:nil];
  36. // if (self != nil)
  37. // {
  38. // // Further initialization if needed
  39. // }
  40. // return self;
  41. //}
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. // Do any additional setup after loading the view from its nib.
  45. #if 0
  46. _outFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"];
  47. #else
  48. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  49. NSString *documentsDirectory = [paths objectAtIndex:0];
  50. _outFilePath = [documentsDirectory stringByAppendingPathComponent:@"output.mp4"];
  51. #endif
  52. UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Common.Back", nil)
  53. style:UIBarButtonItemStylePlain
  54. target:self
  55. action:@selector(goBack)];
  56. self.navigationItem.leftBarButtonItem = customBackButton;
  57. customBackButton.tintColor = UIColorFromRGB(0x0accac);
  58. self.navigationItem.title = NSLocalizedString(@"TCVideoEditPrevView.TitleVideoPreview", nil);
  59. _actionType = -1;
  60. }
  61. - (UIView*)generatingView
  62. {
  63. /*用作生成时的提示浮层*/
  64. if (!_generationView) {
  65. _generationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height + 64)];
  66. _generationView.backgroundColor = UIColor.blackColor;
  67. _generationView.alpha = 0.9f;
  68. _generateProgressView = [UIProgressView new];
  69. _generateProgressView.center = CGPointMake(_generationView.width / 2, _generationView.height / 2);
  70. _generateProgressView.bounds = CGRectMake(0, 0, 225, 20);
  71. _generateProgressView.progressTintColor = UIColorFromRGB(0x0accac);
  72. [_generateProgressView setTrackImage:[UIImage imageNamed:@"slide_bar_small"]];
  73. _generationTitleLabel = [UILabel new];
  74. _generationTitleLabel.font = [UIFont systemFontOfSize:14];
  75. _generationTitleLabel.text = NSLocalizedString(@"TCVideoEditPrevView.VideoSynthesizing", nil);
  76. _generationTitleLabel.textColor = UIColor.whiteColor;
  77. _generationTitleLabel.textAlignment = NSTextAlignmentCenter;
  78. _generationTitleLabel.frame = CGRectMake(0, _generateProgressView.y - 34, _generationView.width, 14);
  79. _generateCannelBtn = [UIButton new];
  80. [_generateCannelBtn setImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
  81. _generateCannelBtn.frame = CGRectMake(_generateProgressView.right + 15, _generationTitleLabel.bottom + 10, 20, 20);
  82. [_generateCannelBtn addTarget:self action:@selector(onCancelBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  83. [_generationView addSubview:_generationTitleLabel];
  84. [_generationView addSubview:_generateProgressView];
  85. [_generationView addSubview:_generateCannelBtn];
  86. }
  87. _generateProgressView.progress = 0.f;
  88. [[[UIApplication sharedApplication] delegate].window addSubview:_generationView];
  89. return _generationView;
  90. }
  91. - (void)onCancelBtnClicked:(UIButton*)sender
  92. {
  93. _generationView.hidden = YES;
  94. [_ugcJoin cancelJoin];
  95. }
  96. - (void)goBack
  97. {
  98. [self onVideoPause];
  99. [self.navigationController popViewControllerAnimated:YES];
  100. }
  101. - (void)viewDidAppear:(BOOL)animated{
  102. [super viewDidAppear:animated];
  103. _videoPreview = [[VideoPreview alloc] initWithFrame:CGRectMake(0, 0, self.prevPlaceHolder.width, self.prevPlaceHolder.height) coverImage:[TXVideoInfoReader getVideoInfoWithAsset:_composeArray.firstObject].coverImage];
  104. _videoPreview.delegate = self;
  105. [self.prevPlaceHolder addSubview:_videoPreview];
  106. TXPreviewParam *param = [[TXPreviewParam alloc] init];
  107. param.videoView = _videoPreview.renderView;
  108. param.renderMode = PREVIEW_RENDER_MODE_FILL_EDGE;
  109. _ugcJoin = [[TXVideoJoiner alloc] initWithPreview:param];
  110. [_ugcJoin setVideoAssetList:_composeArray];
  111. _ugcJoin.previewDelegate = _videoPreview;
  112. _ugcJoin.joinerDelegate = self;
  113. [self play];
  114. }
  115. - (void)viewDidDisappear:(BOOL)animated
  116. {
  117. [_ugcJoin pausePlay];
  118. }
  119. - (void)play{
  120. [_ugcJoin startPlay];
  121. [_videoPreview setPlayBtn:YES];
  122. }
  123. #pragma mark VideoPreviewDelegate
  124. - (void)onVideoPlay
  125. {
  126. if (_ugcJoin) {
  127. [_ugcJoin startPlay];
  128. }else{
  129. }
  130. }
  131. - (void)onVideoPause
  132. {
  133. if (_ugcJoin) {
  134. [_ugcJoin pausePlay];
  135. }else{
  136. }
  137. }
  138. - (void)onVideoResume
  139. {
  140. if (_ugcJoin) {
  141. [_ugcJoin resumePlay];
  142. } else {
  143. }
  144. }
  145. - (void)onVideoPlayProgress:(CGFloat)time
  146. {
  147. _currentPos = time;
  148. }
  149. - (void)onVideoPlayFinished
  150. {
  151. [_ugcJoin startPlay];
  152. }
  153. - (void)onVideoEnterBackground
  154. {
  155. [MBProgressHUD hideHUDForView:self.view animated:YES];
  156. [self onVideoPause];
  157. if (_generationView && !_generationView.hidden) {
  158. _generationView.hidden = YES;
  159. [_ugcJoin cancelJoin];
  160. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"TCVideoEditPrevView.HintVideoSynthesizeFailed", nil)
  161. message:NSLocalizedString(@"TCVideoEditPrevView.ErrorSwitchBackend", nil)
  162. delegate:self
  163. cancelButtonTitle:NSLocalizedString(@"Common.GotIt", nil)
  164. otherButtonTitles:nil, nil];
  165. [alertView show];
  166. }
  167. }
  168. -(void) onJoinComplete:(TXJoinerResult *)result
  169. {
  170. _generationView.hidden = YES;
  171. if(result.retCode == 0)
  172. {
  173. if (_actionType == ActionType_Save || _actionType == ActionType_Save_Publish) {
  174. UISaveVideoAtPathToSavedPhotosAlbum(_outFilePath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
  175. }else{
  176. [self performSelector:@selector(video:didFinishSavingWithError:contextInfo:) withObject:nil];
  177. }
  178. }else{
  179. [MBProgressHUD hideHUDForView:self.view animated:YES];
  180. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"TCVideoEditPrevView.HintVideoSynthesizeFailed", nil)
  181. message:[NSString stringWithFormat:NSLocalizedString(@"Common.HintErrorCodeMessage", nil),(long)result.retCode,result.descMsg]
  182. delegate:self
  183. cancelButtonTitle:NSLocalizedString(@"Common.GotIt", nil)
  184. otherButtonTitles:nil, nil];
  185. [alertView show];
  186. }
  187. }
  188. -(void) onJoinProgress:(float)progress {
  189. // [MBProgressHUD HUDForView:self.view].progress = progress;
  190. _generateProgressView.progress = progress;
  191. }
  192. // ------
  193. - (IBAction)saveToLocal:(id)sender {
  194. _actionType = ActionType_Save;
  195. [self process];
  196. }
  197. - (IBAction)publish:(id)sender {
  198. _actionType = ActionType_Publish;
  199. [self process];
  200. }
  201. - (IBAction)saveAndPublish:(id)sender {
  202. _actionType = ActionType_Save_Publish;
  203. [self process];
  204. }
  205. - (void)process {
  206. [_videoPreview setPlayBtn:NO];
  207. [self onVideoPause];
  208. // MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  209. // hud.labelText = @"视频生成中...";
  210. if (_ugcJoin) {
  211. [_ugcJoin joinVideo:VIDEO_COMPRESSED_540P videoOutputPath:_outFilePath];
  212. }
  213. // Set the bar determinate mode to show task progress.
  214. // hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
  215. _generationView = [self generatingView];
  216. _generationView.hidden = NO;
  217. }
  218. - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
  219. [MBProgressHUD hideHUDForView:self.view animated:YES];
  220. _generationView.hidden = YES;
  221. if (_actionType == ActionType_Save) {
  222. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  223. return;
  224. }
  225. [self publish];
  226. }
  227. - (void)publish {
  228. TCVideoPublishController *vc = [[TCVideoPublishController alloc] initWithPath:_outFilePath
  229. videoMsg:[TXVideoInfoReader getVideoInfo:_outFilePath]];
  230. [self.navigationController pushViewController:vc animated:YES];
  231. }
  232. @end