扫一扫咨询方案
更新时间 : 2026-03-23 16:37:39
SXTemplate类的静态函数createWithFolder,并传入模板路径和SXTemplate.TemplateUsage.kForPreview作为参数构建一个SXTemplate实例对象。SXTemplate实例的setReplaceableJson函数,并传入需要替换素材的Json字符串作为参数来实现高级素材替换。SXTemplate实例的setDrawTextCacheDir函数,并传入缓存目录作为参数。SXTemplate实例的enableSourcePrepare函数。SXTemplate实例的commit函数创建渲染对象。SXTemplatePlayer的构造函数传入SXTemplate的实例对象作为参数,构建一个SXTemplatePlayer实例对象。SXTemplatePlayer实例对象的setXComponent方法传入鸿蒙XComponent组件作为播放容器。SXTemplatePlayer实例对象的setPlayerCallback方法监听播放进度。SXTemplatePlayer实例对象的play方法开启预览。//鸿蒙XComponent组件示例
xcomponentController: XComponentController = new XComponentController()
XComponent({
id: 'templateXComponent',
type: 'surface',
controller: this.xcomponentController
})
let path: string = "";//模板根目录
let json: string = "";//替换资源json
let mTemplate: SXTemplate = SXTemplate.createWithFolder(path, SXTemplate.TemplateUsage.kForPreview);
mTemplate.setReplaceableJson(json);
mTemplate.commit();
let player: SXTemplatePlayer = new SXTemplatePlayer(this.template, null);
player.setPlayerCallback((frame: number) => {
//frame为第几帧
hilog.info(0x0000, 'SXVideoEngine', 'frameIndex %{public}d', frame);
}, () => {
hilog.info(0x0000, 'SXVideoEngine', 'finish');
})
//传入XComponent surface id
player.setXComponent(this.xcomponentController.getXComponentSurfaceId());
mPlayer.start(); //开始预览

SXTemplate类的静态函数createWithFolder,并传入模板路径和SXTemplate.TemplateUsage.kForPreview作为参数构建一个SXTemplate实例对象。SXTemplate实例的setReplaceableJson函数,并传入需要替换素材的Json字符串作为参数来实现高级素材替换。SXTemplate实例的setDrawTextCacheDir函数,并传入缓存目录作为参数。SXTemplate实例的enableSourcePrepare函数。SXTemplate实例的commit函数创建渲染对象。SXTemplatePlayer的构造函数传入SXTemplate的实例对象作为参数,构建一个SXTemplatePlayer实例对象。SXTemplatePlayer实例对象的setXComponent方法传入鸿蒙XComponent组件作为播放容器。SXTemplatePlayer实例对象的setPlayerCallback方法监听播放进度。SXTemplatePlayer实例对象的play方法开启预览。//鸿蒙XComponent组件示例
xcomponentController: XComponentController = new XComponentController()
XComponent({
id: 'templateXComponent',
type: 'surface',
controller: this.xcomponentController
})
let path: string = "";//模板根目录
let json: string = "";//替换资源json
let mTemplate: SXTemplate = SXTemplate.createWithFolder(path, SXTemplate.TemplateUsage.kForPreview);
mTemplate.setReplaceableJson(json);
mTemplate.commit();
let player: SXTemplatePlayer = new SXTemplatePlayer(this.template, null);
player.setPlayerCallback((frame: number) => {
//frame为第几帧
hilog.info(0x0000, 'SXVideoEngine', 'frameIndex %{public}d', frame);
}, () => {
hilog.info(0x0000, 'SXVideoEngine', 'finish');
})
//传入XComponent surface id
player.setXComponent(this.xcomponentController.getXComponentSurfaceId());
mPlayer.start(); //开始预览