Flutter 3.47 鸿蒙三方库适配实战:从 0 到 1 处理 flutter_vibration
Flutter 3.47 鸿蒙三方库适配实战:从 0 到 1 处理 flutter_vibration
振动反馈比声音更私密、比视觉更直接,是移动交互的重要通道。flutter_vibration 支持播放振动模式和停止振动。把它带到鸿蒙时,振动马达的控制接口是适配目标。
本文以 flutter_vibration 0.0.4 为例,记录我如何保留原有 Dart API、补齐 Flutter OH 插件入口、在鸿蒙端接入真实系统能力,并在 HarmonyOS API 26 真机上完成构建、安装、启动和按钮调用验证。
适配仓库: oh-flutter/flutter_vibration
适配分支: feat/ohos_flutter_vibration_0.0.4
适配 TAG: 0.0.4-ohos.2
一、先看最终效果

图 1:HarmonyOS API 26 真机启动 Demo。页面显示 flutter_vibration 0.0.4。

图 2:点击“播放振动”后返回 play 调用完成——@ohos.vibrator 真实执行多周期振动波形。

图 3:点击“播放铃声 + 振动”后返回 play 带铃声 调用完成,AVPlayer 循环播放音频资源。
| 验证点 | 实测结果 | 证据 |
|---|---|---|
| Flutter OH 3.47 工程构建 | 通过 | entry-default-signed.hap |
| 安装、启动(真机) | 通过 | 图 1 |
play() | 真实振动(驱动日志确认 800ms 波形) | 图 2 |
play(assets:) | 振动 + AVPlayer 循环播放 | 图 3 |
stop() | 同时停止振动与播放 | 图 4 |
| 振动模式 | 已接入 @ohos.vibrator 多周期波形 | 图 2 |

图 4:点击“停止”后返回 stop 调用完成,振动与音频同时停止。
二、这个库原来是怎么工作的
flutter_vibration 的接口是实例方法:
Dart FlutterVibration
-> MethodChannel('flutter_vibration')
-> Android Vibrator / iOS AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
-> play(assets, vibration) / stop()
play 支持指定资源与振动开关,stop 停止当前振动。
三、实际环境
Flutter 鸿蒙环境搭建不在本文重复展开,直接参考 Flutter OH 环境搭建指南。
| 组件 | 版本 |
|---|---|
| Flutter OH | 3.47.4-ohos-1.0.4 |
| Dart | 3.13.3 |
| DevEco Studio | 26.0.0 Release |
| HarmonyOS SDK | API 26 |
| 测试设备 | 华为畅享 90 Pro Max,HarmonyOS 7.0.0.105(API 26) |
| 目标库 | flutter_vibration 0.0.4 |
flutter --version
hdc list targets
四、确认上游版本并同步到 AtomGit
上游包地址是 https://github.com/huiqun922/flutter_vibration,版本 0.0.4 使用 MIT(Copyright © 2019 InWay) License。适配前我在 AtomGit 目标组织中搜索了同名库,没有找到可以直接使用的 OHOS 实现。
仓库适配提交为 76ada39,分支与 TAG 0.0.4-ohos.2 均指向该提交。
git clone https://atomgit.com/oh-flutter/flutter_vibration.git
cd flutter_vibration
git switch feat/ohos_flutter_vibration_0.0.4
git log --oneline -5
五、创建适配分支并检查工程结构
git switch -c feat/ohos_flutter_vibration_0.0.4
关键目录如下:
flutter_vibration/
├── lib/ # 原有 Dart API 和 MethodChannel
├── ohos/
│ ├── index.ets # OHOS 插件导出入口
│ └── src/main/ets/components/plugin/
│ └── FlutterVibrationPlugin.ets # 通道实现
├── example/lib/main.dart # 真机验证 Demo
├── example/ohos/ # Flutter OH 宿主工程
├── pubspec.yaml # 声明 ohos 平台
└── README.OpenHarmony_CN.md # 鸿蒙适配说明
pubspec.yaml 中的关键声明是:
flutter:
plugin:
platforms:
ohos:
pluginClass: FlutterVibrationPlugin
pluginClass 必须和 ohos/index.ets 导出的类名一致,否则 Dart 端能编译,运行时却不会注册插件。
六、适配过程:注册通道并对齐语义
// 振动:构建 4 个周期的等效波形(鸿蒙不支持无限循环)
private async startVibration(): Promise<void> {
const builder = new vibrator.VibratorPatternBuilder();
let start = 1500;
for (let cycle = 0; cycle < 4; cycle++) {
builder.addContinuousEvent(start, 800, { intensity: 80 });
builder.addContinuousEvent(start + 1600, 800, { intensity: 80 });
start += 3900;
}
await vibrator.startVibration({ type: 'pattern', pattern: builder.build() },
{ usage: 'alarm' });
}
// 音频:AVPlayer 循环播放 rawfile
player.fdSrc = { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length };
player.loop = true;
await player.play();
| 方法 | OpenHarmony 实现 |
|---|---|
play() | @ohos.vibrator 多周期波形 |
play(assets:) | 振动 + AVPlayer 循环播放 |
stop() | vibrator.stopVibration() + player.release() |
七、构建配置与签名
示例工程的目标 SDK 对齐到 26,与 @ohos/flutter_ohos 的最低要求一致:
{
"name": "default",
"compatibleSdkVersion": "26.0.0",
"targetSdkVersion": "26.0.0",
"runtimeOS": "HarmonyOS"
}
Demo 的 AppScope/app.json5 统一成 com.example.flutter_oh_demo,与 DevEco 调试签名对应。证书、p12、p7b 和密码没有提交到仓库,读者应在自己的 DevEco Studio 中生成调试签名。
八、通过 AtomGit 引入适配版
在另一个 Flutter OH 应用中使用时,建议锁定 TAG:
dependencies:
flutter_vibration:
git:
url: https://atomgit.com/oh-flutter/flutter_vibration.git
ref: 0.0.4-ohos.2
flutter pub get
然后在 pubspec.lock 检查 source: git、AtomGit 地址和 resolved-ref,确认没有解析到 pub.dev 的同名版本。
九、构建、安装和真机验证
cd example
flutter pub get
flutter analyze
flutter build hap --debug
hdc install -r build/ohos/hap/entry-default-signed.hap
hdc shell aa start -a EntryAbility -b com.example.flutter_oh_demo
构建输出为 ✓ Built build/ohos/hap/entry-default-signed.hap.,随后 hdc 报告安装成功并启动 Ability。真机上按本文的截图顺序逐一点击按钮完成验证。
十、适配结果和当前边界
振动与音频都已真实接入。两个实现约束:
- 鸿蒙 vibrator 不支持"无限循环波形",本实现构建 4 个周期(约 15.6 秒)的等效波形,
stop()立即取消。 - 空
assets时只振动不播放——鸿蒙公开 SDK 没有"读取系统默认铃声"的接口,这一点与 Android 默认响铃行为不同。
十一、FAQ
Q1:play 和 stop 是静态方法吗?
- 现象:
FlutterVibration是可实例化的类,方法不是静态的。 - 解决方法: 按实例调用。
- 验证结果: 真机调用成功。
Q2:振动失败会影响业务吗?
- 现象: 振动异常会被捕获,不影响后续流程。
- 解决方法: 调用方无需特判。
- 验证结果: 真机振动正常。
Q3:和已适配的 haptic_kit 是什么关系?
- 现象: 两者都调 vibrator,但 haptic_kit 是完整接入的系统能力库,本库是对上游 API 的兼容实现。
- 解决方法: 按需选择。
- 验证结果: 均可使用。
十二、总结
flutter_vibration 0.0.4 已接入鸿蒙的振动与音频:用 @ohos.vibrator 播放多周期振动波形,用 AVPlayer 循环播放音频资源。真机上通过驱动日志确认了振动波形真实执行。
十三、参考资料和仓库地址
- 适配仓库主页
- 适配分支
- 适配 TAG
- 最终提交 76ada39
- ArkTS 通道实现
- 鸿蒙适配说明
- pub.dev 包页面
- pub.dev 0.0.4 版本页
- 上游仓库
- Flutter 官方插件与平台通道文档
- Flutter OH 鸿蒙 SDK 仓库
- HarmonyOS 开发者官方文档
欢迎加入CPF-Flutter 鸿蒙社区:https://atomgit.com/CPF-Flutter
更多推荐



所有评论(0)