-
Notifications
You must be signed in to change notification settings - Fork 0
Ai Text To Speech
Mr.P edited this page Aug 25, 2026
·
2 revisions
Synthesize text through the selected AI provider and receive a local audio file that your app can play or otherwise process.
-
AIBudsAISDKis initialized and a registered provider is selected. - The provider supports
TTSServiceAPI. - Provider authentication and network access are available when required.
- Invoke synthesis from a background thread, as required by the public SDK contract.
AIBudsAI.xcframework
import AIBudsAI
import AIBudsAIFoundation#import <AIBudsAI/AIBudsAI-Swift.h>/// 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.
static func synthesizeText(
_ text: String,
config: TTSConfig = .default,
completion: ((
_ taskId: String?,
_ success: Bool,
_ response: TTSResultModel?,
_ error: NSError?
) -> Void)? = nil
)/// 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 *)text
config:(AIBudsTTSConfig *)config
completion:(void (^ _Nullable)(NSString * _Nullable taskId, BOOL success, AIBudsTTSResultModel * _Nullable response, NSError * _Nullable error))completion;See synthesizeText.
TTSConfig exposes an optional speakerId. When it is nil, the provider selects its default speaker.
On success, TTSResultModel provides:
| Property | Description |
|---|---|
audioFile |
Audio file path relative to the app's documents directory. |
audioFilePath |
Full path to the synthesized audio file. |
audioFormat |
Format of the synthesized audio file. |
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];
});
}];
});- This API synthesizes a file; it does not start device playback and does not expose
stopSpeaking()orisSpeaking(). - Treat
success,response, anderrortogether. A successful operation requiressuccess == trueand a non-nilresponse. - Speaker identifiers are provider-specific. Only set an identifier documented by the selected provider.
- Move UI and audio-player updates back to the main thread.
AIBuds SDK documentation · Full documentation · API Reference
- Introduction
- Getting Started
- Core Concepts
-
Core Features
- Basic Features
- Device Info
- Find Device
- Physical Operations
- Work Mode
- Work Status
- Wear Detection
- Volume Control
- Music Control
- TWS
- Equalizer
- ANC
- Audio
- Camera
- Remote Camera
- File Import
- Teleprompter
- Segment Navigation
- Live Streaming
- Device Applications
- OTA
- Camera OTA
- Service Auth
- AI Services
- Voice Assistant
- Logging
- Advanced Topics
- Releases
- Troubleshooting