본문으로 건너뛰기

텍스트 음성 변환

선택한 AI 서비스 제공자를 통해 텍스트를 음성으로 합성하고, 앱에서 재생하거나 처리할 수 있는 로컬 오디오 파일을 받습니다.

사전 요구 사항

  • AIBudsAISDK가 초기화되어 있고 등록된 서비스 제공자를 선택해야 합니다.
  • 서비스 제공자가 TTSServiceAPI를 지원해야 합니다.
  • 필요한 경우 서비스 제공자 인증과 네트워크 연결을 사용할 수 있어야 합니다.
  • 공개 SDK 계약에 따라 음성 합성은 백그라운드 스레드에서 호출해야 합니다.

AI를 활용해 구현

AI로 구현

AI로 이 워크플로 구현

공식 “AIBuds 텍스트 음성 변환 구현” 스킬을 사용해 앱에 맞게 구현하세요.

https://docs-aibuds.github.io/ko/skills/implement-aibuds-text-to-speech을 읽고 지침을 따르세요. 이 스킬로 “AIBuds 텍스트 음성 변환 구현”을 이 iOS 프로젝트에 구현하고 검증하세요.
공식 스킬 보기

API Reference

프레임워크

AIBudsAI.xcframework

가져오기

Swift
import AIBudsAI
import AIBudsAIFoundation

선언

Swift
/// Synthesizes text into speech.
/// - Parameters:
///   - text: The text to synthesize.
///   - config: The synthesis configuration.
///   - completion: Called with the task identifier, success state, optional
///     result, and optional error when synthesis completes.
/// - Important: Call this method from a background thread to avoid blocking
///   the main thread.
public static func synthesizeText(_ text: String,
                                  config: TTSConfig = .default,
                              completion: ((
                                  _ taskId: String?,
                                  _ success: Bool,
                                  _ response: TTSResultModel?,
                                  _ error: NSError?
                              ) -> Void)? = nil) -> Void

synthesizeText를 참고하세요.

설정 및 결과

TTSConfig는 선택 사항인 speakerId를 제공합니다. 값이 nil이면 서비스 제공자가 기본 화자를 선택합니다.

성공하면 TTSResultModel에서 다음 정보를 제공합니다.

속성설명
audioFile앱 Documents 디렉터리를 기준으로 한 오디오 파일 상대 경로입니다.
audioFilePath합성된 오디오 파일의 전체 경로입니다.
audioFormat합성된 오디오 파일의 형식입니다.

사용 예제

Swift
let config = TTSConfig.default
config.speakerId = nil

DispatchQueue.global(qos: .userInitiated).async {
    AIBudsAISDK.synthesizeText(
        "Hello, how can I help you?",
        config: config
    ) { taskId, success, response, error in
        guard success, let response else {
            print(error?.localizedDescription ?? "Synthesis failed")
            return
        }

        print("Task: \(taskId ?? "Unavailable")")
        print("Audio: \(response.audioFilePath)")

        DispatchQueue.main.async {
            playAudio(atPath: response.audioFilePath)
        }
    }
}

참고

  • 이 API는 오디오 파일을 합성합니다. 기기 재생을 시작하지 않으며 stopSpeaking()이나 isSpeaking()도 제공하지 않습니다.
  • success, response, error를 함께 확인하세요. 성공하려면 success == true이고 response가 nil이 아니어야 합니다.
  • 화자 식별자는 서비스 제공자마다 다릅니다. 선택한 제공자가 문서화한 식별자만 설정하세요.
  • UI와 오디오 플레이어 갱신은 메인 스레드에서 수행하세요.