接下来是录制部分,只要响应用户点击按钮调用SDK方法就可以了,为了方便起见,这里复用了这个按钮来显示当前状态。另外加上在进度条上显示进度的逻辑。
- (IBAction)onTapButton:(UIButton *)sender { [_editor startPlayFromTime:0 toTime:_videoInfo.duration]; if ([_recorder startRecord:_recordPath coverPath:[_recordPath stringByAppendingString:@".png"]] != 0) { NSLog(@"相机启动失败"); } [sender setTitle:@"录像中" forState:UIControlStateNormal]; sender.enabled = NO; } #pragma mark TXVideoPreviewListener -(void) onPreviewProgress:(CGFloat)time { self.progressView.progress = time / _videoInfo.duration; }录制好后开始完成拼接部分, 这里需要指定两个视频在结果中的位置,这里设置一左一右。
-(void)onRecordComplete:(TXUGCRecordResult*)result; { NSLog(@"录制完成,开始合成"); [self.recordButton setTitle:@"合成中..." forState:UIControlStateNormal]; //获取录制视频的宽高 TXVideoInfo *videoInfo = [TXVideoInfoReader getVideoInfo:_recordPath]; CGFloat width = videoInfo.width; CGFloat height = videoInfo.height; //录制视频和原视频左右排列 CGRect recordScreen = CGRectMake(0, 0, width, height); CGRect playScreen = CGRectMake(width, 0, width, height); [_joiner setSplitScreenList:@[[NSValue valueWithCGRect:recordScreen],[NSValue valueWithCGRect:playScreen]] canvasWidth:width * 2 canvasHeight:height]; [_joiner splitJoinVideo:VIDEO_COMPRESSED_720P videoOutputPath:_resultPath]; }监听合成进度,让子弹飞一会-(void) onJoinProgress:(float)progress { NSLog(@"视频合成中%d%%",(int)(progress * 100)); self.progressView.progress = progress; }
大工告成#pragma mark TXVideoJoinerListener -(void) onJoinComplete:(TXJoinerResult )result { NSLog(@"视频合成完毕"); VideoPreviewController controller = [[VideoPreviewController alloc] initWithVideoPath:_resultPath]; [self.navigationController pushViewController:controller animated:YES]; }
至此就制作完成了,上面提到了一个视频预览的ViewController,代码也很简单
@import TXLiteAVSDK_UGC; @interface VideoPreviewController () <TXVideoPreviewListener> { TXVideoEditer *_editor; } @property (strong, nonatomic) NSString *videoPath; @end @implementation VideoPreviewController - (instancetype)initWithVideoPath:(NSString *)path { if (self = [super initWithNibName:nil bundle:nil]) { self.videoPath = path; } return self; } - (void)viewDidLoad { [super viewDidLoad]; TXPreviewParam *param = [[TXPreviewParam alloc] init]; param.videoView = self.view; param.renderMode = RENDER_MODE_FILL_EDGE; _editor = [[TXVideoEditer alloc] initWithPreview:param]; _editor.previewDelegate = self; [_editor setVideoPath:self.videoPath]; [_editor startPlayFromTime:0 toTime:[TXVideoInfoReader getVideoInfo:self.videoPath].duration]; } -(void) onPreviewFinished { [_editor startPlayFromTime:0 toTime:[TXVideoInfoReader getVideoInfo:self.videoPath].duration]; } @end以上既是所有的代码,这里回顾一下前面的完整流程: 1.新建与配置工程 2.添加录像、播放与状态显示的视图 3. 响应用户事件来调用SDK相关方法 4. 响应异步操作进度的回调。一共只有百十来行代码,简直是唾手可得,再把界面修饰下明天就可以和老板报告了。老板肯定没有想到我能这么完成这个任务,这对他来说一定是一个惊喜。
问答短视频都需要CDN的支持吗?
相关阅读心随手动,驱动短视频热潮的引擎
教你1天搭建自己的“微视”
MySQL 8.0 版本功能变更介绍
此文已由作者授权腾讯云+社区发布,原文链接:https://cloud.tencent.com/developer/article/1158911?fromSource=waitui
欢迎大家前往腾讯云+社区或关注云加社区微信公众号(QcloudCommunity),第一时间获取更多海量技术实践干货哦~
海量技术实践经验,尽在云加社区!