build.gradle 中添加 UIKit 依赖。implementation 'com.yunxiao.tavmagic:tavmagicuikit:x.x.x'
<com.yunxiao.tavmagic.uikit.TAVEditorUIKitandroid:id="@+id/editor_ui"android:layout_width="match_parent"android:layout_height="match_parent" />
TAVEditorUIKit editor = findViewById(R.id.editor_ui);// 初始化方式1:使用视频路径列表List<String> videoPaths = Arrays.asList("path1.mp4", "path2.mp4");editor.initWithClipPath(videoPaths);// 初始化方式2:使用草稿TAVDraft draft = loadDraft(); // 加载草稿editor.initWithDraft(draft);
@Overrideprotected void onResume() {super.onResume();editor.onResume(true); // true表示恢复播放}@Overrideprotected void onPause() {super.onPause();editor.onPause();}@Overrideprotected void onDestroy() {super.onDestroy();editor.onDestroy();}@Overridepublic void onBackPressed() {if (!editor.onBackPressed()) {super.onBackPressed();}}
// 设置权限提供器editor.setPermissionProvider(new ITAVPermissionProvider() {@Overridepublic boolean checkPermission(String permission) {return ContextCompat.checkSelfPermission(MainActivity.this, permission)== PackageManager.PERMISSION_GRANTED;}@Overridepublic void requestPermission(String permission, TAVConsumer<Boolean> callback) {ActivityCompat.requestPermissions(MainActivity.this,new String[]{permission}, REQUEST_CODE);}});
TAVMagicAdaptActivity 即可。如果业务侧已存在自定义的 BaseActivity,可将以下代码拷贝至 UIKit 所在的 Activity 中使用。public class TAVMagicAdaptActivity extends AppCompatActivity {private static final String TAG = "TAVMagicAdaptActivity";private static final float TARGET_WIDTH_DP = 375;private Context mCustomCtx;@Overrideprotected void attachBaseContext(Context newBase) {if (mCustomCtx == null) {mCustomCtx = createCustomContext(newBase);}super.attachBaseContext(mCustomCtx);}private Context createCustomContext(Context context) {final DisplayMetrics systemDM = Resources.getSystem().getDisplayMetrics();final DisplayMetrics activityDM = context.getResources().getDisplayMetrics();float targetDensity = activityDM.widthPixels / TARGET_WIDTH_DP;float targetScaledDensity = targetDensity * (systemDM.scaledDensity / systemDM.density);Configuration newConfig = new Configuration(context.getResources().getConfiguration());Context adaptedContext = context.createConfigurationContext(newConfig);DisplayMetrics newMetrics = adaptedContext.getResources().getDisplayMetrics();newMetrics.density = targetDensity;newMetrics.scaledDensity = targetScaledDensity;return adaptedContext;}@Overridepublic Resources getResources() {return (mCustomCtx != null && mCustomCtx.getResources() != null)? mCustomCtx.getResources() : super.getResources();}}
字段名 | 类型 | 说明 |
editorKey | String | 对应编辑器实例的唯一标识 Key,用于从当前运行中的编辑器中生成封面。 |
draft | TAVDraft | 草稿对象,当基于草稿离线生成封面时使用。 |
position | long | 打开封面选择器期望展示的视频时间点,单位:微秒(us)。 |
coverPlaceHolder | String | 封面占位图路径(可选),用于正式封面生成前展示的占位图片。 |
maxDuration | long | 当前封面请求对应的视频最大时长,单位:微秒(us)。用于裁剪封面选择器时长。 |
renderSizeLevel | int | 渲染分辨率等级,默认值为 TAVEditorConstants.VIDEO_LEVEL_720P。 |
字段名 | 类型 | 说明 |
position | long | 封面对应的视频时间戳,单位:微秒(us)。 |
thumbPath | String | 封面图在本地的缓存文件路径。 |
String editorKey = mViewBinding.uiKit.forkOffscreenEditor();
TAVDraft 对象。TAVDraft draft = TAVDraft.createFromJsonPath(draftDir + File.separator + "draft.json");
coverLauncher = TAVCoverSelector.registerForResult(this, result -> {if (result != null) {//此处处理封面选择器返回的选择结果TAVCoverResult}});
// 通过草稿打开// TAVCoverSelector.TAVCoverRequest request = TAVCoverSelector.TAVCoverRequest.fromDraft(draft);// 通过编辑打开TAVCoverSelector.TAVCoverRequest request = TAVCoverSelector.TAVCoverRequest.fromKey(editorKey);request.coverPlaceHolder = mThumbPath;request.renderSizeLevel = TAVEditorConstants.VIDEO_LEVEL_720P;request.maxDuration = 20_000_000;request.position = 10_000_000;coverLauncher.launch(request);
文档反馈