jm_baidu_stt_plugin
Flutter bindings for the latest Baidu Speech ASR & wake-up SDKs.
- Android: bdasr_V3_20250507_b610f20.jar + native libs (v3.4.5).
- iOS: ASR_iOS_v3.0.12.0.1285b57 static library & resources.
The plugin exposes a simple Dart API that mirrors the reference native demo so you can initialize the SDK, create recognizer/wake-up engines, and handle streaming status callbacks from Dart.
Install
-
Add the dependency to your
pubspec.yaml:jm_baidu_stt_plugin: ^0.0.9 -
Install packages:
flutter pub get -
For iOS, run CocoaPods from your host app’s
ios/folder:pod install
iOS SDK delivery (recommended)
Baidu iOS SDK binaries/models are large. For publishing (Git / pub.dev), it is recommended not to commit them into the repository.
This plugin uses CocoaPods prepare_command to download missing artifacts during pod install.
Usage
- Provide download URLs via environment variables.
Option A (recommended): one zip containing both files
JM_BAIDU_STT_IOS_SDK_ZIP_URL(the zip must containlibBaiduSpeechSDK.aandbds_easr_input_model.datsomewhere inside)
Option B: two direct file URLs
JM_BAIDU_STT_IOS_LIB_URLJM_BAIDU_STT_IOS_MODEL_URL
- Run CocoaPods:
pod install
If your network requires a proxy, set it before running CocoaPods:
export https_proxy=http://127.0.0.1:7890export http_proxy=http://127.0.0.1:7890
Notes
- If the files already exist in
ios/Libs/...andios/Assets/..., download is skipped (offline friendly). - To force re-download:
export JM_BAIDU_STT_IOS_FORCE_DOWNLOAD=1 - To disable download and fail fast if missing:
export JM_BAIDU_STT_IOS_SKIP_DOWNLOAD=1
Tip: set URLs in ios/Podfile (CI/team friendly)
If you don’t want every developer/CI to export env vars manually, you can set them in your host app’s ios/Podfile:
ENV['JM_BAIDU_STT_IOS_SDK_ZIP_URL'] ||= '<YOUR_ZIP_URL>'
Or (two direct URLs):
ENV['JM_BAIDU_STT_IOS_LIB_URL'] ||= '<YOUR_LIB_URL>'ENV['JM_BAIDU_STT_IOS_MODEL_URL'] ||= '<YOUR_MODEL_URL>'
Quick start
-
Initialize and subscribe to events:
JmBaiduSttPlugin.initSDK(...)and provide anonEventcallback. -
Create the ASR engine:
await JmBaiduSttPlugin.create(type: BaiduSpeechBuildType.asr) -
Start recognition:
await JmBaiduSttPlugin.startRecognition()
Optional (some offline modes): await JmBaiduSttPlugin.startRecognition(wakeUpWord: '...')
-
Stop recognition:
await JmBaiduSttPlugin.stopRecognition() -
Wake-up:
- Create:
await JmBaiduSttPlugin.create(type: BaiduSpeechBuildType.wakeUp) - Start:
await JmBaiduSttPlugin.startMonitorWakeUp() - Stop:
await JmBaiduSttPlugin.stopMonitorWakeUp()
Event status values
| status | meaning |
|---|---|
flushData |
partial recognition result |
statusFinish |
final recognition result |
volumeChanged |
audio level update (0–100) |
statusError |
recognition/wake-up failure |
statusTriggered |
wake-up keyword detected |
type in the payload is 0 for ASR and 1 for wake-up.
iOS notes (important)
- Simulator: the Baidu static library in this project is
arm64only (device slice). It usually cannot run on iOS Simulator. - Resource lookup: iOS native code will try to locate
.datfiles frommainBundle/bundleForClass/ common resource bundles, to be resilient under CocoaPods.
iOS ASR defaults
The iOS implementation follows Baidu’s official sample defaults:
- Default
productId:1537 - Default
language:EVoiceRecognitionLanguageChinese - Default
strategy:EVR_STRATEGY_ONLINE
If you need parallel mode (online + offline), pass strategy = EVR_STRATEGY_BOTH and ensure the offline model/license files are available.
ASR stability
iOS native layer includes guards for common issues:
- Rapid
stopRecognition()→startRecognition()may causeengine is busy: the plugin will auto retry with backoff. - STOP-induced HTTP timeout will be silently ignored when stopping.
NoSpeech/Shortwill be treated asstatusFinishwith empty string.
Advanced iOS parameters (optional)
The iOS implementation supports extra parameters on create (defaults are applied if not provided):
productId: defaults to1537language: defaults toEVoiceRecognitionLanguageChinesestrategy: defaults toEVR_STRATEGY_ONLINEenableModelVAD: defaults tofalseofflineEngineType: defaults toEVR_OFFLINE_ENGINE_INPUT(only used whenstrategy == EVR_STRATEGY_BOTH)offlineDatPath: defaults to bundledbds_easr_input_model.dat(only used whenstrategy == EVR_STRATEGY_BOTH)serverUrl: defaults tohttps://vop.baidu.com/server_api
Current Dart API does not expose these options yet; if you need them you can extend the plugin or invoke MethodChannel('jm_baidu_stt_plugin').invokeMethod('create', ...) yourself.
permission_handler (microphone)
If you use permission_handler on iOS, make sure the microphone permission macro is enabled for the permission_handler_apple target in your host app’s ios/Podfile post_install.
Example lines to add (as recommended by the plugin):
if target.name == 'permission_handler_apple'target.build_configurations.each do |config|config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'PERMISSION_MICROPHONE=1']endend
Android integration notes
minSdkVersionis set to 24 to align with the new Baidu SDK artifacts.- The plugin bundles the latest
bdasr_V3_20250507_b610f20.jar,WakeUp.bin, and all native.sofiles (armeabi, armeabi-v7a, arm64-v8a, x86, x86_64). - Required permissions are already declared in the embedded
AndroidManifest.xml, but your host app must still requestRECORD_AUDIOat runtime. - If you shrink/obfuscate, keep the Baidu classes (e.g. add
-keep class com.baidu.speech.** { *; }toproguard-rules.pro).
iOS integration notes
- The plugin ships Baidu’s static library (
libBaiduSpeechSDK.a), headers, wake-word models, and resource bundles from ASR_iOS_v3.0.12.0. - Add the following to your app’s
Info.plist:NSMicrophoneUsageDescription- (Optional)
UIBackgroundModes→audioif you record in background.
- Run
pod installinside the Flutter app’sios/folder after adding the plugin so CocoaPods pulls in the updated podspec. - The plugin includes a Privacy Manifest (
Resources/PrivacyInfo.xcprivacy). Update it if your host app has additional data uses.
Example app
example/lib/main.dart contains a basic control panel showing how to:
- Initialize the SDK with an event listener.
- Create ASR and wake-up engines.
- Start/stop recognition and wake-up monitoring while printing native callback payloads.
Replace the placeholder credentials before running on a device.
License
See LICENSE. The Baidu SDK binaries remain subject to Baidu’s own license terms; ensure you have permission to use them in production.