GenAI Text to Speech API

ML Kit의 GenAI Text to Speech API를 사용하면 텍스트 콘텐츠를 음성 오디오로 합성할 수 있습니다. Text to Speech API는 생성형 AI 모델을 사용하여 고품질 음성 합성을 제공합니다.

ML Kit Text to Speech API는 매우 인간과 유사한 음성을 제공하므로 가능하면 이 API를 사용하는 것이 좋습니다. 하지만 사용할 수 없는 경우 Android 플랫폼 TextToSpeech API를 대신 사용할 수 있습니다.

주요 기능

  • 단일 SDK 내에서 통합된 다운로드 및 추론 옵션
  • 코루틴과 Flow를 사용하여 재생 상태 및 오디오를 스트리밍하는 Kotlin 우선 API입니다.

기본 요건

Text-to-Speech API를 사용하려면 다음 요구사항을 충족해야 합니다.

  • 다음과 같은 지원되는 기기 중 하나를 사용합니다.
    • Google: Pixel 8, Pixel 9, Pixel 10, Pixel 11
    • 삼성: 갤럭시 S25, 갤럭시 S26, 갤럭시 Z 폴드8, 갤럭시 Z 폴드8 울트라, 갤럭시 Z 플립8
  • Google Play에서 최신 버전의 음성 인식 및 합성 앱(com.google.android.tts)을 사용하고 있어야 합니다. 앱이 없거나 오래된 경우 TTS(텍스트 음성 변환)를 구현할 때 checkStatus() 메서드는 FeatureStatus.UNAVAILABLE를 반환합니다.
  • 프로젝트에서 최소 SDK (minSdk) 버전 26 이상을 사용합니다.

시작하기

시작하려면 모듈 수준 build.gradle.kts (또는 build.gradle) 파일에 ML Kit 텍스트 음성 변환 종속 항목을 추가합니다.

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를 사용하려면 다음 단계를 따르세요.

  1. 클라이언트 초기화: SpeechSynthesizerOptions 구성 객체를 전달하여 SpeechSynthesizer 클라이언트 인스턴스를 만듭니다. 모든 언어를 지원하려면 단일 클라이언트 인스턴스만 있으면 됩니다. 옵션 객체에 지정된 언어는 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()

지원되는 음성

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() 메서드를 호출합니다.
다운로드가 실패하거나 진행되지 않습니다. 네트워크에 연결되어 있지 않습니다. 기기가 온라인 상태일 때 다시 시도하세요.