TCVideoJoinViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // TCVideoJoinController.m
  3. // TCLVBIMDemo
  4. //
  5. // Created by annidyfeng on 2017/4/19.
  6. // Copyright © 2017年 tencent. All rights reserved.
  7. //
  8. #import "TCVideoJoinViewController.h"
  9. #import "TCVideoEditPrevViewController.h"
  10. #import "TCVideoEditViewController.h"
  11. #import "TCVideoCutViewController.h"
  12. #import "SDKHeader.h"
  13. #import "TCVideoJoinCell.h"
  14. #import "SDKHeader.h"
  15. static NSString *indetifer = @"TCVideoJoinCell";
  16. @interface TCVideoJoinViewController ()<UITableViewDelegate, UITableViewDataSource , TXVideoJoinerListener>
  17. @property (weak) IBOutlet UITableView *tableView;
  18. @property (weak, nonatomic) IBOutlet UIButton *confirmButton;
  19. @end
  20. @implementation TCVideoJoinViewController
  21. {
  22. UIView * _generationView;
  23. UIProgressView * _generateProgressView;
  24. NSString * _videoOutputPath;
  25. BOOL _appInbackground;
  26. TXVideoJoiner * _videoJoiner;
  27. }
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. [self.confirmButton setTitle:NSLocalizedString(@"Common.OK", nil) forState:UIControlStateNormal];
  31. // 视频列表
  32. [_tableView registerNib:[UINib nibWithNibName:@"TCVideoJoinCell" bundle:nil] forCellReuseIdentifier:indetifer];
  33. _tableView.dataSource = self;
  34. _tableView.delegate = self;
  35. [_tableView setEditing:YES animated:YES];
  36. //视频合成相关逻辑
  37. _reorderVideoList = [NSMutableArray new];
  38. for (AVAsset *asset in self.videoAssertList) {
  39. TCVideoJoinCellModel *model = [TCVideoJoinCellModel new];
  40. model.videoAsset = asset;
  41. TXVideoInfo *info = [TXVideoInfoReader getVideoInfoWithAsset:asset];
  42. model.cover = info.coverImage;
  43. model.duration = info.duration;
  44. model.width = info.width;
  45. model.height = info.height;
  46. [_reorderVideoList addObject:model];
  47. }
  48. _videoOutputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"outputJoin.mp4"];
  49. TXPreviewParam *param = [[TXPreviewParam alloc] init];
  50. param.videoView = [UIView new];
  51. _videoJoiner = [[TXVideoJoiner alloc] initWithPreview:param];
  52. _videoJoiner.joinerDelegate = self;
  53. UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Common.Cancel", nil)
  54. style:UIBarButtonItemStylePlain
  55. target:self
  56. action:@selector(goBack)];
  57. customBackButton.tintColor = RGB(238, 100, 85);
  58. self.navigationItem.leftBarButtonItem = customBackButton;
  59. self.navigationItem.title = NSLocalizedString(@"TCVideoJoinView.StitchVideo", nil);
  60. //监听后台事件
  61. [[NSNotificationCenter defaultCenter] addObserver:self
  62. selector:@selector(applicationWillEnterForeground:)
  63. name:UIApplicationWillEnterForegroundNotification
  64. object:nil];
  65. [[NSNotificationCenter defaultCenter] addObserver:self
  66. selector:@selector(applicationDidEnterBackground:)
  67. name:UIApplicationDidEnterBackgroundNotification
  68. object:nil];
  69. [[NSNotificationCenter defaultCenter] addObserver:self
  70. selector:@selector(applicationDidBecomeActive:)
  71. name:UIApplicationDidBecomeActiveNotification
  72. object:nil];
  73. [[NSNotificationCenter defaultCenter] addObserver:self
  74. selector:@selector(applicationWillResignActive:)
  75. name:UIApplicationWillResignActiveNotification
  76. object:nil];
  77. [[NSNotificationCenter defaultCenter] addObserver:self
  78. selector:@selector(onAudioSessionEvent:)
  79. name:AVAudioSessionInterruptionNotification
  80. object:nil];
  81. }
  82. - (void)viewDidLayoutSubviews
  83. {
  84. /*用作生成时的提示浮层*/
  85. _generationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height + 64)];
  86. _generationView.backgroundColor = UIColor.blackColor;
  87. _generationView.alpha = 0.9f;
  88. _generateProgressView = [UIProgressView new];
  89. _generateProgressView.center = CGPointMake(_generationView.width / 2, _generationView.height / 2);
  90. _generateProgressView.bounds = CGRectMake(0, 0, 225, 20);
  91. _generateProgressView.progressTintColor = RGB(238, 100, 85);
  92. [_generateProgressView setTrackImage:[UIImage imageNamed:@"slide_bar_small"]];
  93. //_generateProgressView.trackTintColor = UIColor.whiteColor;
  94. //_generateProgressView.transform = CGAffineTransformMakeScale(1.0, 2.0);
  95. UILabel *generationTitleLabel = [UILabel new];
  96. generationTitleLabel.font = [UIFont systemFontOfSize:14];
  97. generationTitleLabel.text = NSLocalizedString(@"TCVideoEditPrevView.VideoSynthesizing", nil);
  98. generationTitleLabel.textColor = UIColor.whiteColor;
  99. generationTitleLabel.textAlignment = NSTextAlignmentCenter;
  100. generationTitleLabel.frame = CGRectMake(0, _generateProgressView.y - 34, _generationView.width, 14);
  101. UIButton *generateCannelBtn = [UIButton new];
  102. [generateCannelBtn setImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
  103. generateCannelBtn.frame = CGRectMake(_generateProgressView.right + 15, generationTitleLabel.bottom + 10, 20, 20);
  104. [generateCannelBtn addTarget:self action:@selector(onCancelBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  105. [_generationView addSubview:generationTitleLabel];
  106. [_generationView addSubview:_generateProgressView];
  107. [_generationView addSubview:generateCannelBtn];
  108. _generateProgressView.progress = 0.f;
  109. _generationView.hidden = YES;
  110. [self.view addSubview:_generationView];
  111. [self.view bringSubviewToFront:_generationView];
  112. }
  113. - (void)goBack
  114. {
  115. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  116. }
  117. - (void)didReceiveMemoryWarning {
  118. [super didReceiveMemoryWarning];
  119. // Dispose of any resources that can be recreated.
  120. }
  121. - (void)applicationWillEnterForeground:(NSNotification *)noti
  122. {
  123. if (_appInbackground){
  124. _appInbackground = NO;
  125. }
  126. }
  127. - (void)applicationDidEnterBackground:(NSNotification *)noti
  128. {
  129. if (!_appInbackground) {
  130. [self onVideoEnterBackground];
  131. _appInbackground = YES;
  132. }
  133. }
  134. - (void)applicationDidBecomeActive:(NSNotification *)noti
  135. {
  136. if (_appInbackground){
  137. _appInbackground = NO;
  138. }
  139. }
  140. - (void)applicationWillResignActive:(NSNotification *)noti
  141. {
  142. if (!_appInbackground) {
  143. [self onVideoEnterBackground];
  144. _appInbackground = YES;
  145. }
  146. }
  147. - (void) onAudioSessionEvent: (NSNotification *) notification
  148. {
  149. NSDictionary *info = notification.userInfo;
  150. AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
  151. if (type == AVAudioSessionInterruptionTypeBegan) {
  152. if (!_appInbackground) {
  153. [self onVideoEnterBackground];
  154. _appInbackground = YES;
  155. }
  156. }
  157. }
  158. - (void)onVideoEnterBackground
  159. {
  160. [_videoJoiner pausePlay];
  161. if (_generationView && !_generationView.hidden) {
  162. _generateProgressView.progress = 0.f;
  163. _generationView.hidden = YES;
  164. [_videoJoiner cancelJoin];
  165. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"TCVideoEditPrevView.HintVideoSynthesizeFailed", nil)
  166. message:NSLocalizedString(@"TCVideoEditPrevView.ErrorSwitchBackend", nil)
  167. delegate:self
  168. cancelButtonTitle:NSLocalizedString(@"Common.GotIt", nil)
  169. otherButtonTitles:nil, nil];
  170. [alertView show];
  171. }
  172. }
  173. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  174. return NSLocalizedString(@"TCVideoJoinView.TitleVideoOrder", nil);
  175. }
  176. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  177. {
  178. return 75;
  179. }
  180. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  181. return self.reorderVideoList.count;
  182. }
  183. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  184. TCVideoJoinCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifer];
  185. cell.model = self.reorderVideoList[indexPath.row];
  186. return cell;
  187. }
  188. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  189. return YES;
  190. }
  191. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  192. return YES;
  193. }
  194. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
  195. {
  196. NSString *toMove = [self.reorderVideoList objectAtIndex:sourceIndexPath.row];
  197. [self.reorderVideoList removeObjectAtIndex:sourceIndexPath.row];
  198. [self.reorderVideoList insertObject:toMove atIndex:destinationIndexPath.row];
  199. }
  200. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  201. if (editingStyle == UITableViewCellEditingStyleDelete) {
  202. [self.reorderVideoList removeObjectAtIndex:indexPath.row];
  203. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
  204. }
  205. }
  206. - (IBAction)preview:(id)sender {
  207. if (self.reorderVideoList.count < 1)
  208. return;
  209. _generationView.hidden= NO;
  210. NSMutableArray *videoAssetList = [NSMutableArray array];
  211. for (TCVideoJoinCellModel *model in _reorderVideoList) {
  212. [videoAssetList addObject:model.videoAsset];
  213. }
  214. [_videoJoiner setVideoAssetList:videoAssetList];
  215. [_videoJoiner joinVideo:VIDEO_COMPRESSED_720P videoOutputPath:_videoOutputPath];
  216. }
  217. -(void)onCancelBtnClicked:(UIButton *)button
  218. {
  219. [_videoJoiner cancelJoin];
  220. _generateProgressView.progress = 0.f;
  221. _generationView.hidden = YES;
  222. }
  223. #pragma mark TXVideoJoinerListener
  224. -(void) onJoinProgress:(float)progress
  225. {
  226. _generateProgressView.progress = progress;
  227. }
  228. -(void) onJoinComplete:(TXJoinerResult *)result
  229. {
  230. if (result.retCode == JOINER_RESULT_OK) {
  231. TCVideoCutViewController *vc = [[TCVideoCutViewController alloc] init];
  232. vc.videoAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:_videoOutputPath]];
  233. [self.navigationController pushViewController:vc animated:YES];
  234. _generationView.hidden = YES;
  235. }
  236. [TCUtil report:xiaoshipin_videojoiner userName:nil code:result.retCode msg:result.descMsg];
  237. }
  238. @end