2025 iThome鐵人賽
「 Flutter :30天打造念佛App,跨平台從Mobile到VR,讓極樂世界在眼前實現 ! 」
Day 10
「 Flutter 語音辨識 實戰入門篇 — 生活在地球的勇者啊,你聽過阿彌陀佛嗎(1) 」
《佛說阿彌陀經》:
「 若有善男子善女人,聞說阿彌陀佛,執持名號,若一日、若二日,若三日,若四日,若五日,若六日,若七日,一心不亂,其人臨命終時,阿彌陀佛,與諸聖眾,現在其前。是人終時,心不顛倒,即得往生阿彌陀佛極樂國土。 」
白話:
如果有善男子、善女人,聽到阿彌陀佛四個字,能一心專一持續念佛,過了一天,過了二天,過了三天,過了四天,過了五天,過了六天,過了七天,若能始終保持一心專一念佛,此人臨命終時,阿彌陀佛和菩薩聖人們,會出現在此人眼前。此人生命盡時,一心清清楚楚專一念佛,就可以轉生阿彌陀佛極樂國土。
前幾天我們已經擁有了Conding世界的神裝 Git 和專屬練級副本 Flavor ,
今天我們就可以出發探索Coding世界,找尋實踐「自動計算佛號數量」的合適方法。
今天我們來嘗試看看「語音辨識」的方式,
讓使用者只需一如往常誦念佛號,
念佛App就會藉由語音辨識自動計算佛號數量,
使用者就不需要額外分心計算數量。
語音辨識在flutter實踐時,我們該選擇哪一個套件?
不同套件的差異在哪? 就讓我們一起來探究吧!
今天我們先來初步認識「speech_to_text」!
Day10 文章目錄:
一、語音辨識
二、speech_to_text
三、安裝與取得用戶權限
1.簡介
語音辨識(ASR, Automatic Speech Recognition),也被稱為自動語音辨識、
語音轉文字識別。
是一種將「語音」即時轉成「文字」的技術,常見應用於會議紀錄、語音指令、
通話轉錄、字幕顯示。
2.常見實作方向
(1)
雲端串流辨識:裝置把音訊持續上傳到雲端模型,回傳即時文字。
優點:準確度高、語言支援廣、可長時間
缺點:需網路與雲端費用
(2)
裝置端(離線)辨識:模型放在本地,不須網路,隱私佳。
優點:低延遲、離線可用
缺點:模型較大、耗電與效能要求較高
1.簡介
This plugin contains a set of classes that make it easy to use the speech recognition capabilities of the underlying platform in Flutter. It supports Android, iOS, MacOS and web.
The target use cases for this library are commands and short phrases, not continuous spoken conversion or always on listening.
speech_to_text 插件是使用裝置內建的語音辨識功能,
適用於命令或短句子,不適用持續性的對話與聆聽。
支援Android、iOS、MacOS、Web、Windows
2.可能遇到的狀況
Speech recognition stops after a brief pause on Android
Android speech recognition has a very short timeout when the speaker pauses. The duration seems to vary by device and version of the Android OS. In the devices I've used none have had a pause longer than 5 seconds. Unfortunately there appears to be no way to change that behaviour.
Android 停頓超過 5 秒可能會結束語音辨識
iOS recognition guidelines
Plan for a one-minute limit on audio duration. Speech recognition places a relatively high burden on battery life and network usage. To minimize this burden, the framework stops speech recognition tasks that last longer than one minute.
iOS 有語音辨識的時長與次數限制,可能超過1分鐘就會停止
1.安裝speech_to_text
(1)打開 pubspec.yaml 添加
dependencies:
speech_to_text: ^7.3.0
(2)終端機執行
flutter pub get
2.取得iOS用戶權限
(1)打開Info.plist,iOS/Runner/Info.plist
(2)添加權限設置
<key>NSSpeechRecognitionUsageDescription</key>
<string>需要語音辨識權限的原因</string>
<key>NSMicrophoneUsageDescription</key>
<string>需要麥克風權限的原因</string>
完整 Info.plist 範例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>Demo</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>demo</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>$(FLUTTER_BUILD_NAME)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(FLUTTER_BUILD_NUMBER)</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSSpeechRecognitionUsageDescription</key>
<string>需要使用語音辨識以計數念佛次數</string>
<key>NSMicrophoneUsageDescription</key>
<string>需要麥克風來辨識念佛</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
3.取得Android用戶權限
(1)打開AndroidManifest.xml ,android/app/src/main/AndroidManifest.xml
AndroidManifest.xml的作用:這個 App 是誰、能做什麼、需要哪些權限
Flutter有多份AndroidManifest:權限添加在
main
這份。
android/app/src/main/AndroidManifest.xml
(main)android/app/src/debug/AndroidManifest.xml
android/app/src/profile/AndroidManifest.xml
(2)添加權限設置
不要放到application裡面
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
完整 AndroidManifest.xml 範例
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<application
android:label="demo"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity=""
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
</queries>
</manifest>
重點 | 內容 |
---|---|
語音辨識 | 語音即時轉成文字 |
speech_to_text | 不支援持續性使用 |
取得用戶權限 | Info.plist(iOS) / AndroidManifest(Android) |