使用 ML Kit 的 GenAI Text to Speech API,您可以將文字內容合成為語音音訊。Text to Speech API 採用生成式 AI 模型,可提供高品質的語音合成。
建議您盡可能使用 ML Kit Text to Speech API,因為這項 API 提供的語音非常擬真。如果無法使用,則可改用 Android 平台 TextToSpeech API。
主要功能
- 在單一 SDK 中提供統一的下載和推論選項。
- Kotlin 優先 API,使用協同程式和 Flow 串流播放狀態和音訊。
必要條件
如要使用 Text to Speech API,您必須符合下列規定:
- 你擁有下列其中一款支援的裝置:
- Google:Pixel 8、Pixel 9、Pixel 10、Pixel 11
- Samsung:Galaxy S25、Galaxy S26、Galaxy Z Fold8、Galaxy Z Fold8 Ultra、Galaxy Z Flip8
- 你已從 Google Play 下載最新版本語音辨識及合成應用程式 (
com.google.android.tts)。如果缺少應用程式或應用程式過時,在實作文字轉語音時,checkStatus()方法會傳回FeatureStatus.UNAVAILABLE。 - 專案使用的 SDK 最低版本 (
minSdk) 為 26 以上。
開始使用
首先,請將 ML Kit 文字轉語音依附元件新增至模組層級的 build.gradle.kts (或 build.gradle) 檔案:
Kotlin
dependencies {
// ML Kit GenAI TTS SDK
implementation("com.google.mlkit:genai-text-to-speech:1.0.0-alpha1")
}
Groovy
dependencies {
// ML Kit GenAI TTS SDK
implementation 'com.google.mlkit:genai-text-to-speech:1.0.0-alpha1'
}
整合 Text-to-Speech API
如要使用 Text-to-Speech API,請按照下列步驟操作:
- 初始化用戶端:傳遞
SpeechSynthesizerOptions設定物件,建立SpeechSynthesizer用戶端執行個體。您只需要一個用戶端執行個體,就能支援所有語言。選項物件中指定的語言代碼,只會決定getSupportedVoices()方法傳回的預設語言清單。 - 下載模型:確認目標語音是否可用。如果狀態為
FeatureStatus.DOWNLOADABLE,請呼叫download()方法,將語音模型下載到裝置。 - 合成內容:建構
SpeechSynthesizerRequest物件,其中包含輸入文字和目標語音。呼叫synthesize()方法生成音訊,並收集播放狀態更新和音訊區塊。
// Initialize the `SpeechSynthesizer` client.
// One client serves every language. `Locale.US` is only the default list locale.
val options = speechSynthesizerOptions(Locale.US)
val client = SpeechSynthesis.getClient(options)
// Query voices for multiple languages using the same client instance.
val englishVoice = client.getSupportedVoices().first() // Uses the default `Locale.US`
val frenchVoice = client.getSupportedVoices(Locale.FRANCE).first()
// Check and download a voice model (for example, the French voice model).
if (client.checkStatus(frenchVoice) == FeatureStatus.DOWNLOADABLE) {
client.download(frenchVoice).collect { status -> println("Download status: $status") }
}
// Construct the request.
// The `VoiceInfo` instance, `frenchVoice` here, determines the spoken language.
val request = SpeechSynthesizerRequest(
input = SpeechSynthesizerRequest.Input.Text("Bonjour tout le monde", frenchVoice),
audioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build()
)
// Speak and collect streaming audio or status.
client.synthesize(request).collect { response ->
when (response) {
is SpeechSynthesizerResponse.StatusUpdate -> println("Playback status: ${response.status}")
is SpeechSynthesizerResponse.AudioAvailable -> println("Audio chunk: ${response.audio.size} bytes")
is SpeechSynthesizerResponse.Error -> println("Error: ${response.errorCode}")
}
}
// Release resources when done.
client.close()
支援的語音
ML Kit Text to Speech API 支援下列語音。使用 VoiceInfo.name 屬性,依名稱選取語音。
- star02:女性,中音調
- star04:男性,中高音
- star05:男性,低音調
- star08:女性,低音調
- star11:男性、中音調、聽起來較年輕
- star12:女性,中高音
- star13:男性,中音調
- star16:女性,中低音
支援的語言
ML Kit Text to Speech API 支援下列語言代碼:
- 阿拉伯文 (ar-XA)
- 丹麥文 (da-DK)
- 德文 (de-DE)
- 英文 (en-GB)
- 英文 (en-US)
- 西班牙文 (es-ES)
- 西班牙文 (es-US)
- 法文 (fr-CA)
- 法文 (fr-FR)
- 北印度文 (hi-IN)
- 印尼文 (id-ID)
- 義大利文 (it-IT)
- 日文 (ja-JP)
- 韓文 (ko-KR)
- 挪威文 (nb-NO)
- 荷蘭文 (nl-NL)
- 波蘭文 (pl-PL)
- 葡萄牙文 (pt-BR)
- 俄文 (ru-RU)
- 瑞典文 (sv-SE)
- 土耳其文 (tr-TR)
常見設定問題
以下列舉幾個常見問題和疑難排解提示:
| 問題 | 可能原因 | 建議修正方式 |
|---|---|---|
語音清單 (來自 getSupportedVoices() 方法) 為空白。 |
裝置不支援,或缺少「語音辨識與合成」應用程式,或應用程式版本過舊。 | 確認裝置是否支援,並更新「語音辨識及合成」應用程式。 |
checkStatus() 方法會傳回 FeatureStatus.UNAVAILABLE。 |
裝置不支援,或缺少「語音辨識與合成」應用程式,或應用程式版本過舊。 | 確認裝置是否支援,並更新「語音辨識及合成」應用程式。 |
checkStatus() 方法會傳回 FeatureStatus.DOWNLOADABLE。 |
語音尚未下載到裝置。 | 呼叫 download() 方法,並等待方法完成,再呼叫 synthesize() 方法。 |
| 下載失敗或沒有進度。 | 沒有網路連線。 | 請在裝置連上網路後再試一次。 |