टेक्स्ट से आवाज़
चुने गए AI provider से टेक्स्ट को आवाज़ में बदलें और local audio file पाएँ, जिसे app चला या आगे process कर सकता है।
आवश्यक शर्तें
AIBudsAISDKinitialize हो और registered provider चुना गया हो।- Provider
TTSServiceAPIsupport करता हो। - ज़रूरत पड़ने पर provider authentication और network access उपलब्ध हों।
- Public SDK contract के अनुसार synthesis को background thread से call करें।
AI की सहायता से लागू करें
AI से बनाएँ
इस वर्कफ़्लो को AI से लागू करें
आधिकारिक “AIBuds टेक्स्ट-टू-स्पीच लागू करें” स्किल से वर्कफ़्लो को अपने ऐप के अनुसार लागू करें।
https://docs-aibuds.github.io/hi/skills/implement-aibuds-text-to-speech को पढ़ें और निर्देशों का पालन करें। इस स्किल से “AIBuds टेक्स्ट-टू-स्पीच लागू करें” को इस iOS प्रोजेक्ट में लागू करें और परिणाम सत्यापित करें।API Reference
Framework
AIBudsAI.xcframework
Import
- Swift
- Objective-C
import AIBudsAI
import AIBudsAIFoundation#import <AIBudsAI/AIBudsAI-Swift.h>Declaration
- 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 देखें।
Configuration और Result
TTSConfig में optional speakerId है। इसके nil होने पर provider अपना default speaker चुनता है।
सफल होने पर TTSResultModel ये values देता है:
| Property | विवरण |
|---|---|
audioFile | App की Documents directory के सापेक्ष audio file path। |
audioFilePath | बनी हुई audio file का पूरा path। |
audioFormat | बनी हुई audio file का format। |
उपयोग के उदाहरण
- 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 audio file बनाती है; device playback शुरू नहीं करती और
stopSpeaking()याisSpeaking()expose नहीं करती। success,responseऔरerrorको साथ जाँचें। Operation तभी सफल है जबsuccess == trueहो और responsenilन हो।- Speaker identifiers provider-specific हैं। केवल चुने गए provider द्वारा documented identifier ही set करें।
- UI और audio-player updates को main thread पर करें।