HarmonyOS鸿蒙Next中Flutter转换文件格式
HarmonyOS鸿蒙Next中Flutter转换文件格式
介绍
本示例通过Flutter调用原生ffmpeg库进行文件格式转换,将录制的.mp4格式文件转换为.aac格式。
效果预览
使用说明
进入应用,打开.mp4文件并播放。点击按钮将.mp4文件转换为.aac音频文件并保存.aac音频文件到手机目录。切换Tab打开.aac音频文件显示并播放。点击删除按钮可以将.aac音频文件从app页面列表删除。
实现思路
flutter通过channel的方式调用FlutterFfmpegPlugin插件方法,该方法通过ffmpeg命令将.mp4格式文件转成.aac音频格式。
onMethodCall(call: MethodCall, result: MethodResult): void {
switch (call.method) {
case 'executeFFmpegWithPath': {
let sourcePath: string = call.argument("sourcePath");
let destPath: string = call.argument("destPath");
let output = destPath;
let callBack: ICallBack = {
callBackResult(code: number) {
if (code === 0) {
let re: HashMap<String, String> = new HashMap<String, String>();
re.set('rc', '0')
re.set('msg', 'covert type success')
re.set('data', output)
result.success(re)
} else {
result.error('-1', 'cmd failed', 'ffmpeg cmd failed')
}
}
}
if(destPath.endsWith(".aac")){
MP4Parser.ffmpegCmd(` ffmpeg -y -i ${sourcePath} -vn -c:a aac -strict experimental -b:a 128k ${output}`, callBack);
}
break;
}
}
}
更多关于HarmonyOS鸿蒙Next中Flutter转换文件格式的实战教程也可以访问 https://www.itying.com/category-92-b0.html
2 回复
在HarmonyOS Next中,Flutter转换文件格式需使用ArkUI适配方案。Flutter的Dart代码无法直接运行,需通过FFI调用Native能力或转为ArkTS组件。具体步骤:
- 用方舟编译器转换Dart逻辑为ArkTS;
- 对Platform层代码使用Native API重写;
- 通过NAPI封装系统能力供ArkTS调用。
UI部分需重构为ArkUI的声明式语法,布局文件需转为ets格式。目前没有自动化转换工具,需手动迁移。
更多关于HarmonyOS鸿蒙Next中Flutter转换文件格式的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html