阿里云短视频Android-SDK创建设置字幕

创建字幕控制器

  • 创建字幕控制器,使用字幕相关功能,需要创建字幕轨并添加到媒体轨道中心,并创建一个字幕片段,配合字幕UI逻辑使用,详见智能小视频源码,涉及组件BDHKVlogSubtitlesView(字幕位置),BDMVSubtitleInputAccessoryView(字幕输入框),BDMVInputEventBottomBar(字幕确认框)
  • 代码示例如下:

//创建字幕控制器 mUgcSubtitleEditController = new UgcSubtitleEditController(this, mUgcPreViewFrameLayout, mVideoView); mUgcSubtitleEditController.setUgcVideoPreviewActivity(this); mUgcSubtitleEditController.setSubtitleEditControllerListener( new UgcSubtitleEditController.SubtitleEditControllerListener() {@Overridepublic List<MultiMediaData> getDataList() {return mDataSourceList;}@Overridepublic void updateSubtitleList(List<SubTitleUnit> subTitleUnits) {if (mVlogEditManager != null) {mVlogEditManager.setSubtitle(subTitleUnits);}if (mAEffectProcessor != null) {mAEffectProcessor.changeEffect(mVlogEditManager.getShaderConfigMap(), mVlogEditManager.getUpdateMediaTracks());}if (mUgcSubtitleEditController != null) {mUgcSubtitleEditController.changeSubtitleIcon(!ListUtils.isEmpty(subTitleUnits));}}@Overridepublic String getThemeId() {return "";}@Overridepublic void updateSubtitleConfig(SubTitleConfig subTitleConfig) {if (mVlogEditManager != null) {mVlogEditManager.setSubtitleConfig(subTitleConfig);}VideoDraftBean videoDraftBeanDb = getVideoDraftBeanDb();if (videoDraftBeanDb != null) {videoDraftBeanDb.setVideoSubtitleConfig(SubTitleConfig.beanToJson(subTitleConfig));CurrentVideoBeanManager.updateVideoDraftBean(videoDraftBeanDb);}}@Overridepublic void setIsNeedPauseWhenEdit(boolean isNeedPauseWhenEdit) {mIsNeedPauseWhenEdit = isNeedPauseWhenEdit;} @Overridepublic void saveSubtitleDraft(List<SubTitleUnit> subTitleUnits) {VideoDraftBean videoDraftBeanDb = getVideoDraftBeanDb();if (videoDraftBeanDb != null) {videoDraftBeanDb.setVideoSubtitleData(SubTitleUnit.arrayToJson(subTitleUnits));CurrentVideoBeanManager.updateVideoDraftBean(videoDraftBeanDb);}}@Overridepublic void updateIsPauseByUser(boolean isPauseByUser) {mIsPausePlayByUser = isPauseByUser;if (mIsPausePlayByUser) {if (mVlogEditManager != null) { mVlogEditManager.pause();}if (mPreviewMusicPlayer != null) { mPreviewMusicPlayer.onPause();}}} });
设置字幕画面位置

  • 设置字幕画面位置,可以调整字幕轨道上某个字幕的显示位置
  • 代码示例如下:

private void inflateDragSubtitleLayout() { if (mDragSubtitleViewStub == null) { return; } mDragSubtitleLayout = (DragSubtitleLayout) mDragSubtitleViewStub.inflate(); mDragSubtitleLayout.setOnDragItemClickListener(this); mDragSubtitleViewStub = null; if (mSubTitleConfig != null) { mDragSubtitleLayout.setSubtitleConfig(mSubTitleConfig); } mDragSubtitleLayout.setOnDragItemMovedListener(new DragSubtitleLayout.OnDragItemMovedListener() { @Override public void onDragItemMoved() { mIsChangeSubtile = true; } }); mDragSubtitleLayout.setOnCancelFocusManuallyListener(new DragSubtitleLayout.OnCancelFocusManuallyListener() { @Override public void onCancelFocusManually(SubTitleUnit subTitleUnit) { if (mSubtitleEditLayout != null && mSubtitleEditLayout.getVideoShaft() != null&& mIPlayerDataSource != null) {mSubtitleEditLayout.getVideoShaft().clearSelectState(); } } });