短视频系统开发商,demo中拍同款功能是如何实现的

短视频系统开发商为了能让客户在购买系统之前进行产品体验,所以提供了demo(演示)供客户下载体验。那么短视频系统demo中的拍同款功能又是如何实现的呢,我们本文就针对该问题来简单分享一下。
1.首先下载原视频添加的音乐到本地

//文件下载
public void download(String tag, String fileDir, String fileName, String url, final Callback callback) {
    File file = new File(fileDir);
    if (!file.exists()) {
        file.mkdirs();
    }
    File fileF = new File(fileDir, fileName); 
    if (fileF.exists()) {
        if (callback!=null){
            callback.onSuccess(fileF);
        }
    }else {
        OkGo.<File>get(url).tag(tag).execute(new FileCallback(fileDir, fileName) {
            @Override
            public void onSuccess(Response<File> response) {
                //下载成功结束后的回调
                if (callback != null) {
                    callback.onSuccess(response.body());
                }
            }
 @Override
            public void downloadProgress(Progress progress) {
                //下载进度-progress
                if (callback != null) {
                    int val = (int) (progress.currentSize * 100 / progress.totalSize);
                    callback.onProgress(val);
                }
            }
 @Override
            public void onError(Response<File> response) {
                super.onError(response);
                //下载失败
                Throwable e = response.getException();
                if (callback != null) {
                    callback.onError(e);
                }
            }
        });
    }
}

2.获取音乐时长

//用来获取本地和网络media相关文件的信息
private MediaMetadataRetriever mMetadataRetriever;

long bgmDuration = 0;
if (mMetadataRetriever == null) {
    mMetadataRetriever = new MediaMetadataRetriever();
}
try {
    mMetadataRetriever.setDataSource(path);
    String duration = mMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    bgmDuration = Long.parseLong(duration);
} catch (Exception e) {
    e.printStackTrace();
}

3.录制视频文件,并添加下载到本地的原视频背景音乐文件为背景音乐

private String mVideoPath;//视频的保存路径
private TXUGCRecord mRecorder;//腾讯sdk-录制器
mVideoPath = StringUtil.generateVideoOutputPath();//视频保存的路径
//初始化录制器
mRecorder = TXUGCRecord.getInstance(CommonAppContext.sInstance);
mRecorder.setVideoRecordListener(this);
mRecorder.setHomeOrientation(TXLiveConstants.VIDEO_ANGLE_HOME_DOWN);
mRecorder.setRenderRotation(TXLiveConstants.RENDER_ROTATION_PORTRAIT);
mRecordSpeed = TXRecordCommon.RECORD_SPEED_NORMAL;
mRecorder.setRecordSpeed(mRecordSpeed);
mRecorder.setAspectRatio(TXRecordCommon.VIDEO_ASPECT_RATIO_9_16);
//开始预览 mCustomConfig 自定义录制参数 mVideoView预览控件
mRecorder.startCameraCustomPreview(mCustomConfig, mVideoView);
//开始录制
int result = mRecorder.startRecord(mVideoPath, CommonAppConfig.VIDEO_RECORD_TEMP_PATH, null);//为空表示不需要生成视频封面
mRecorder.setBGM(mMusicBean.getLocalPath());//设置背景音乐
mRecorder.playBGMFromTime(0, bgmDuration);// bgmDuration时长
mRecorder.setBGMVolume(1);//背景音为1最大
mRecorder.setMicVolume(0);//原声音为0

4.录制成功后,查看mVideoPath路径下的文件,便是与原视频背景音乐相同的视频
以上就是短视频系统开发商所提供的demo中拍同款功能的大概实现流程,而提供demo的初衷也是为客户带来更好的售前体验。
声明:本文由作者原创,转载请注明出处及原文链接。

发布了150 篇原创文章 · 获赞 65 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/yb1314111/article/details/103856685
今日推荐