GenAI Text to Speech API

GenAI Text to Speech API ของ ML Kit ช่วยให้คุณสังเคราะห์เนื้อหาข้อความเป็นเสียงพูดได้ Text to Speech API ใช้โมเดล Generative AI เพื่อให้การสังเคราะห์เสียงพูดคุณภาพสูง

เราขอแนะนำให้ใช้ Text to Speech API ของ ML Kit หากเป็นไปได้ เนื่องจาก API นี้ให้เสียงที่เหมือนมนุษย์มาก แต่หากไม่มี API ดังกล่าว คุณสามารถใช้ API TextToSpeech ของแพลตฟอร์ม Android แทนได้

ความสามารถหลัก

  • ตัวเลือกการดาวน์โหลดและการอนุมานแบบรวมภายใน SDK เดียว
  • Kotlin-first 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
  • คุณมีแอป Speech Recognition & Synthesis (com.google.android.tts) เวอร์ชันล่าสุดจาก Google Play หากไม่มีแอปหรือแอปเป็นเวอร์ชันเก่า เมื่อใช้การอ่านออกเสียงข้อความ เมธอด checkStatus() จะแสดงผลเป็น FeatureStatus.UNAVAILABLE
  • โปรเจ็กต์ของคุณใช้ SDK ขั้นต่ำ (minSdk) เวอร์ชัน 26 ขึ้นไป

เริ่มต้นใช้งาน

หากต้องการเริ่มต้นใช้งาน ให้เพิ่มทรัพยากร Dependency ของการอ่านออกเสียงข้อความของ 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")
}

ดึงดูด

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 ให้ทำตามขั้นตอนต่อไปนี้

  1. เริ่มต้นไคลเอ็นต์: สร้างอินสแตนซ์ไคลเอ็นต์ SpeechSynthesizer โดยส่งออบเจ็กต์การกำหนดค่า SpeechSynthesizerOptions คุณต้องมีอินสแตนซ์ไคลเอ็นต์เพียงรายการเดียวเพื่อรองรับทุกภาษา ภาษาที่ระบุในออบเจ็กต์ตัวเลือกจะกำหนดรายการภาษาเริ่มต้นที่เมธอด getSupportedVoices() แสดงผลเท่านั้น
  2. ดาวน์โหลดโมเดล: ตรวจสอบความพร้อมใช้งานของเสียงเป้าหมาย หากสถานะเป็น FeatureStatus.DOWNLOADABLE ให้เรียกใช้เมธอด download() เพื่อดาวน์โหลดรูปแบบเสียงลงในอุปกรณ์
  3. สังเคราะห์เนื้อหา: สร้างออบเจ็กต์ 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()

เสียงที่รองรับ

API การอ่านออกเสียงข้อความของ ML Kit รองรับเสียงต่อไปนี้ เลือกเสียงตามชื่อโดยใช้พร็อพเพอร์ตี้ VoiceInfo.name

  • star02: ผู้หญิง ระดับเสียงปานกลาง
  • star04: ชาย ระดับเสียงสูงปานกลาง
  • star05: ผู้ชาย ระดับเสียงต่ำ
  • star08: ผู้หญิง ระดับเสียงต่ำ
  • star11: ชาย, ระดับเสียงปานกลาง, เสียงค่อนข้างเด็ก
  • star12: ผู้หญิง, ระดับเสียงสูงปานกลาง
  • star13: ชาย ระดับเสียงปานกลาง
  • star16: ผู้หญิง ระดับเสียงต่ำปานกลาง

ภาษาที่รองรับ

API การอ่านออกเสียงข้อความของ ML Kit รองรับภาษาต่อไปนี้

  • อาหรับ (ar-XA)
  • เดนมาร์ก (da-DK)
  • German (de-DE)
  • อังกฤษ (en-GB)
  • อังกฤษ (อเมริกา)
  • Spanish (es-ES)
  • Spanish (es-US)
  • ฝรั่งเศส (fr-CA)
  • French (fr-FR)
  • Hindi (hi-IN)
  • Indonesian (id-ID)
  • อิตาลี (it-IT)
  • ญี่ปุ่น (ja-JP)
  • เกาหลี (ko-KR)
  • นอร์เวย์ (nb-NO)
  • Dutch (nl-NL)
  • โปแลนด์ (pl-PL)
  • โปรตุเกส (pt-BR)
  • รัสเซีย (ru-RU)
  • สวีเดน (sv-SE)
  • Turkish (tr-TR)

ปัญหาการตั้งค่าที่พบบ่อย

ปัญหาที่พบได้ทั่วไปและเคล็ดลับการแก้ปัญหามีดังนี้

ปัญหา สาเหตุที่เป็นไปได้ การแก้ไขที่แนะนำ
รายการเสียง (จากเมธอด getSupportedVoices()) ว่างเปล่า อุปกรณ์ไม่รองรับ หรือแอป Speech Recognition and Synthesis หายไปหรือล้าสมัย ตรวจสอบว่าอุปกรณ์ของคุณรองรับและอัปเดตแอป Speech Recognition and Synthesis
เมธอด checkStatus() จะแสดงผล FeatureStatus.UNAVAILABLE อุปกรณ์ไม่รองรับ หรือแอป Speech Recognition and Synthesis หายไปหรือล้าสมัย ตรวจสอบว่าอุปกรณ์ของคุณรองรับและอัปเดตแอป Speech Recognition and Synthesis
เมธอด checkStatus() จะแสดงผล FeatureStatus.DOWNLOADABLE ยังไม่ได้ดาวน์โหลดเสียงลงในอุปกรณ์ เรียกใช้เมธอด download() และรอให้เสร็จสิ้นก่อนเรียกใช้เมธอด synthesize()
การดาวน์โหลดไม่สำเร็จหรือไม่มีความคืบหน้า ไม่มีการเชื่อมต่อเครือข่าย ลองอีกครั้งเมื่ออุปกรณ์ออนไลน์