텍스트 음성 변환
선택한 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
- Objective-C
import AIBudsAI
import AIBudsAIFoundation#import <AIBudsAI/AIBudsAI-Swift.h>선언
- Swift
- Objective-C
/// 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/// 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.
+ (void)synthesizeText:(NSString * _Nonnull)text
config:(AIBudsTTSConfig * _Nonnull)config
completion:(void (^ _Nullable)(NSString * _Nullable, BOOL, AIBudsTTSResultModel * _Nullable, NSError * _Nullable))completion;synthesizeText를 참고하세요.
설정 및 결과
TTSConfig는 선택 사항인 speakerId를 제공합니다. 값이 nil이면 서비스 제공자가 기본 화자를 선택합니다.
성공하면 TTSResultModel에서 다음 정보를 제공합니다.
| 속성 | 설명 |
|---|---|
audioFile | 앱 Documents 디렉터리를 기준으로 한 오디오 파일 상대 경로입니다. |
audioFilePath | 합성된 오디오 파일의 전체 경로입니다. |
audioFormat | 합성된 오디오 파일의 형식입니다. |
사용 예제
- Swift
- Objective-C
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)
}
}
}AIBudsTTSConfig *config = [AIBudsTTSConfig defaultConfig];
config.speakerId = nil;
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
[AIBudsAISDK
synthesizeText:@"Hello, how can I help you?"
config:config
completion:^(
NSString *taskId, BOOL success, AIBudsTTSResultModel *response, NSError *error) {
if (!success || response == nil) {
NSLog(@"%@", error.localizedDescription ?: @"Synthesis failed");
return;
}
NSLog(@"Task: %@", taskId ?: @"Unavailable");
NSLog(@"Audio: %@", response.audioFilePath);
dispatch_async(dispatch_get_main_queue(), ^{
[self playAudioAtPath:response.audioFilePath];
});
}];
});참고
- 이 API는 오디오 파일을 합성합니다. 기기 재생을 시작하지 않으며
stopSpeaking()이나isSpeaking()도 제공하지 않습니다. success,response,error를 함께 확인하세요. 성공하려면success == true이고 response가nil이 아니어야 합니다.- 화자 식별자는 서비스 제공자마다 다릅니다. 선택한 제공자가 문서화한 식별자만 설정하세요.
- UI와 오디오 플레이어 갱신은 메인 스레드에서 수행하세요.