在 ML Kit 的 GenAI Speech Recognition API 中,您可以將音訊內容轉錄為文字。這個 API 支援下列模式:
- 基本:Speech Recognition API 使用傳統的裝置端語音辨識模型,類似於 SpeechRecognizer API
- 適用於搭載 API 級別 31 以上版本的大多數 Android 裝置
- 進階:Speech Recognition API 使用生成式 AI 模型,可支援更多語言,整體品質也更優異
- 適用於 Pixel 10 裝置,更多裝置正在開發中
主要功能
- 從麥克風或音訊檔案擷取串流輸入內容
- 轉錄文字會以連續串流的形式提供,一開始可能只是部分內容 (且可能變更),之後才會成為最終內容。
搜尋結果範例
| 音訊 | 模式 | 語言代碼 | 語音轉錄 |
|---|---|---|---|
| audio_1 | 基本 | en-US | 「This is a short message」(這是簡短訊息) |
| audio_2 | 進階 | es-ES | 「Este es un mensaje corto.」 |
與平台 Speech Recognition API 比較
使用基本模式時,ML Kit Speech Recognition API 提供的核心功能與平台 Speech Recognition API 類似。ML Kit 的主要優點是支援更多 Android 平台版本,API 級別須為 31 以上,比部分平台 API 更廣泛。
此外,ML Kit Speech Recognition API 在「進階」模式下會使用裝置端 Gemini 模型,支援更多語言。
開始使用
在 build.gradle 設定中,將 ML Kit Speech Recognition API 新增為依附元件
implementation("com.google.mlkit:genai-speech-recognition:1.0.0-alpha1")
如要將 Speech Recognition API 整合到應用程式中,請建立 SpeechRecognizer 用戶端。檢查必要裝置端模型功能的狀態,並下載模型 (如果裝置上還沒有)。在 SpeechRecognizerRequest 中準備音訊輸入內容後,請使用用戶端執行推論,從 Kotlin 流程接收串流輸出內容。最後,請記得關閉用戶端,釋出資源。
// 1. Create a SpeechRecognizer with desired options.
val options: SpeechRecognizerOptions =
speechRecognizerOptions {
locale = Locale.US
preferredMode = SpeechRecognizerOptions.Mode.MODE_ADVANCED
}
val speechRecognizer: SpeechRecognizer = SpeechRecognition.getClient(options)
// 2. Check if the recognition model is available or needs downloading.
launch {
val status: Int = speechRecognizer.checkStatus()
if (status == FeatureStatus.DOWNLOADABLE) {
// 3. If needed, download the model and monitor progress.
speechRecognizer.download.collect { downloadStatus ->
when (downloadStatus) {
is DownloadStatus.DownloadCompleted -> {
// Model is ready, start recognition.
startMyRecognition(speechRecognizer)
}
is DownloadStatus.DownloadFailed -> {
// Handle download failure (e.g., inform the user).
}
is DownloadStatus.DownloadProgress -> {
// Handle download progress (e.g., update a progress bar).
}
}
}
} else if (status == FeatureStatus.AVAILABLE) {
// Model is already ready, start recognition immediately.
startMyRecognition(speechRecognizer)
} else {
// Handle other statuses (e.g., DOWNLOADING, UNAVAILABLE).
}
}
// 4. Define your recognition logic using a suspend function.
suspend fun startMyRecognition(recognizer: SpeechRecognizer) {
// Create a request (e.g., specifying audio source).
val request: SpeechRecognizerRequest
= speechRecognizerRequest { audioSource = AudioSource.fromMic() }
// Start recognition and process the continuous stream of responses.
recognizer.startRecognition(request).collect {
// Process the SpeechRecognitionResponse data here.
}
}
// 5. Stop recognition and clean up resources when the session is complete.
launch {
recognizer.stopRecognition()
recognizer.close()
}
支援的語言和裝置
| 眾數 | 語言代碼 |
| 基本 | en-US、fr-FR (Beta 版)、it-IT (Beta 版)、de-DE (Beta 版)、es-ES (Beta 版)、 hi-IN (Beta 版)、ja-JP (Beta 版)、pt-BR (Beta 版)、tr-TR (Beta 版)、pl-PL (Beta 版)、 cmn-Hans-CN (Beta 版)、ko-KR (Beta 版)、cmn-Hant-TW (Beta 版)、ru-RU (Beta 版)、 vi-VN (Beta 版) |
| 進階 | 通常準確度較高的語言代碼: en-US、ko-KR、es-ES、 fr-FR、de-DE、it-IT、pt-PT、cmn-Hans-CN、cmn-Hant-TW、ja-JP、th-TH、 ru-RU、nl-NL (Beta 版)、da-DK (Beta 版)、sv-SE (Beta 版)、pl-PL (Beta 版)、 hi-IN (Beta 版)、vi-VN (Beta 版)、id-ID (Beta 版)、ar-SA (Beta 版)、 tr-TR (Beta 版) |
支援的裝置
| 眾數 | 支援的裝置 |
| 基本 | 搭載 API 級別 31 以上版本的 Android 裝置。 |
| 進階 | Pixel 10 |
常見設定問題
ML Kit GenAI API 會透過 Android AICore 應用程式存取 Gemini Nano。如果裝置剛完成設定 (包括重設),或是 AICore 應用程式剛重設 (例如清除資料、解除安裝後重新安裝),AICore 應用程式可能沒有足夠時間完成初始化 (包括從伺服器下載最新設定)。因此 ML Kit GenAI API 可能無法正常運作。以下是您可能會看到的常見設定錯誤訊息和處理方式:
| 錯誤訊息範例 | 如何處理 |
| AICore 失敗,錯誤類型為 4-CONNECTION_ERROR,錯誤代碼為 601-BINDING_FAILURE:AICore 服務繫結失敗。 | 如果裝置設定完成後,您立即使用 ML Kit GenAI API 安裝應用程式,或是應用程式安裝完成後解除安裝 AICore,就可能發生這種情況。更新 AICore 應用程式,然後重新安裝應用程式,應該就能修正問題。 |
| AICore 失敗,錯誤類型為 3-PREPARATION_ERROR,錯誤代碼為 606-FEATURE_NOT_FOUND:功能「...」無法使用。 |
如果 AICore 尚未完成下載最新設定,就可能發生這種情況。裝置連上網際網路後,通常需要幾分鐘到幾小時才能完成更新。重新啟動裝置可加快更新速度。
請注意,如果裝置的系統啟動載入程式已解鎖,您也會看到這項錯誤訊息,因為這項 API 不支援系統啟動載入程式已解鎖的裝置。 |
| AICore failed with error type 1-DOWNLOAD_ERROR and error code 0-UNKNOWN: Feature ... failed with failure status 0 and error esz: UNAVAILABLE: Unable to resolve host ... | 保持網路連線,等待幾分鐘後再試一次。 |
程式碼範例
- 在 GitHub 上探索 ML Kit Speech Recognition API 程式碼範例