foreword

Short video templates are a way to quickly create short videos. They are generally made by professional designers or template creators. Users only need to replace some of the materials in the video template to generate a creative video that is the same as the template. This time-saving and labor-saving video creation method of "set template" that does not require "brain-burning" to conceive ideas is very popular among users.

Application scenarios

Short video templates are widely used in short video apps, video editing tools, shooting and beautification tools, travel, e-commerce, news and other vertical fields. E.g:

In the vertical domain of video editing tools and shooting and beautification tools, the short video template function can reduce the threshold for video editing, stimulate user creation, and then increase the user activity of the application;

In the travel application, users can record the customs and customs during the journey with videos by means of "set templates";

In e-commerce applications, merchants can quickly make product display videos by applying product display templates;

In the news information application, users can apply news templates to quickly publish news information.

Huawei HMS Core Video Editing Service Template Capability

The HMS Core video editing service has recently opened the template function, and preset some video templates for different scenarios for developers to test.

Easy access, time-saving and labor-saving

HMS Core video editing service provides detailed development interfaces, interface descriptions and access steps covering template column list, template details, template engineering, import and export, etc. The code is simple and clear, saving time and effort for development.

Cloud management, efficient operation

The HMS Core video editing service provides developers with a background for arranging materials in the AGC background, and operators can complete template column arrangement, tag management, country/region settings, template content on and off shelves, and quick retrieval and query information on the web side.

integration code

1. Development preparation

For detailed preparation steps, please refer to the official website of Huawei Developer Alliance

2. Edit project integration

2.1 Set the authentication information of the application

Application authentication information can be set through api_key or Access Token.

The Access Token is set through the setAccessToken method, which can be initialized and set once when the application starts, and there is no need to set it multiple times.

 MediaApplication.getInstance().setAccessToken("your access token");

The api_key is set by the setApiKey method, which can be initialized and set once when the application starts, and there is no need to set it multiple times.

 MediaApplication.getInstance().setApiKey("your ApiKey");

2.2 Set the unique identification ID, that is, the License ID.

The license ID is a valid certificate for management and control. You must ensure that the license ID is unique.

 MediaApplication.getInstance().setLicenseId("License ID");

2.2.1 Initialize the Editor runtime environment

To create an editing project, you need to first create an Editor object and initialize its runtime environment. When leaving the editing project, the Editor instance should be released.

(1) Create Editor object

 HuaweiVideoEditor editor = HuaweiVideoEditor.create(getApplicationContext());

(2) Specify the layout position of the preview window

The preview window is responsible for the rendering of video images, which is realized by creating SurfaceView inside the video editing atomic capability SDK. Before creating the window, you need to specify the layout position of the preview window in your app.

 <LinearLayout    
    android:id="@+id/video_content_layout"    
    android:layout_width="0dp"    
    android:layout_height="0dp"    
    android:background="@color/video_edit_main_bg_color"    
    android:gravity="center"    
    android:orientation="vertical" />
// 指定预览窗口 
LinearLayout mSdkPreviewContainer = view.findViewById(R.id.video_content_layout);

// 设置预览窗口承载的布局 
editor.setDisplay(mSdkPreviewContainer);

(3) Initialize the running environment. If the license authentication fails, a LicenseException will be thrown.

After the Editor object is created, the actual system resources are not occupied at this time, and the timing of its environment initialization needs to be manually selected. At this time, the necessary threads and timers will be created inside the Video Editing Atomic Capability SDK.

 try {
        editor.initEnvironment();
   } catch (LicenseException error) { 
        SmartLog.e(TAG, "initEnvironment failed: " + error.getErrorMsg());    
        finish();
        return;
   }

3. Template capability integration

 // 获取模板栏目列表
final HVEColumnInfo[] column = new HVEColumnInfo[1];
HVETemplateManager.getInstance().getColumnInfos(new HVETemplateManager.HVETemplateColumnsCallback() {
        @Override
        public void onSuccess(List<HVEColumnInfo> result) {
           // 获取模板栏目列表成功
           column[0] = result.get(0);
        }

        @Override
        public void onFail(int error) {
           // 获取模板栏目列表失败
        }
});

// 获取模板详情
final String[] templateIds = new String[1];
// size为需要请求数据的数量 > 0,offset为需要请求数据的偏移量 ≥ 0,true代表强制获取网络数据
HVETemplateManager.getInstance().getTemplateInfos(column[0].getColumnId(), size, offset, true, new HVETemplateManager.HVETemplateInfosCallback() {
        @Override
        public void onSuccess(List<HVETemplateInfo> result, boolean hasMore) {
           // 获取模板详情成功
           HVETemplateInfo templateInfo = result.get(0);
           // 获取模板ID
           templateIds[0] = templateInfo.getId();
        }

        @Override
        public void onFail(int errorCode) {
           // 获取模板详情失败
        }
});

// 待获取模板详情成功后,获取模板ID
String templateId = templateIds[0];

// 获取模板工程
final List<HVETemplateElement>[] editableElementList = new ArrayList[1];;
HVETemplateManager.getInstance().getTemplateProject(templateId, new HVETemplateManager.HVETemplateProjectCallback() {
        @Override
        public void onSuccess(List<HVETemplateElement> editableElements) {
           // 获取成功后跳转到选择资源界面,把选中的本地资源路径更新到editableElements
           editableElementList[0] = editableElements;
        }

        @Override
        public void onProgress(int progress) {
           // 获取进度
        }

        @Override
        public void onFail(int errorCode) {
           // 获取失败
        }
});

// 准备模板工程
HVETemplateManager.getInstance().prepareTemplateProject(templateId, new HVETemplateManager.HVETemplateProjectPrepareCallback() {
        @Override
        public void onSuccess() {
            // 准备模板工程成功,可以生成HuaweiVideoEditor实例进行播放、预览、导出等操作         
        }
        @Override
        public void onProgress(int progress) {
            // 准备进度
        }

        @Override
        public void onFail(int errorCode) {
            // 准备失败
        }
});

// 在模板准备成功后,生成HuaweiVideoEditor实例
// 拥有editor之后就可以进行播放或者导出操作,详情见播放时间线和导出
HuaweiVideoEditor editor = HuaweiVideoEditor.create(templateId, editableElementList[0]);
try {
      editor.initEnvironment();
} catch (LicenseException e) {
      SmartLog.e(TAG, "editor initEnvironment ERROR.");
}

Demo

Learn more details>>

Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee

Follow us to know the latest technical information of HMS Core for the first time~


HarmonyOS_SDK
596 声望11.7k 粉丝

HarmonyOS SDK通过将HarmonyOS系统级能力对外开放,支撑开发者高效打造更纯净、更智能、更精致、更易用的鸿蒙原生应用,和开发者共同成长。