Metinden Konuşmaya
Metni seçilen AI sağlayıcısıyla sese dönüştürün ve uygulamanızın oynatabileceği veya işleyebileceği yerel ses dosyasını alın.
Ön Koşullar
AIBudsAISDKbaşlatılmış ve kayıtlı bir sağlayıcı seçilmiştir.- Sağlayıcı
TTSServiceAPIdesteği sunar. - Gerektiğinde sağlayıcı kimlik doğrulaması ve ağ erişimi kullanılabilir.
- Genel SDK sözleşmesinin gerektirdiği gibi ses üretimini arka plan iş parçacığından çağırın.
AI desteğiyle uygulayın
AI ile geliştirin
Bu iş akışını AI ile uygulayın
Resmî “AIBuds Metinden Sese Dönüşümü Uygulama” becerisini kullanarak iş akışını uygulamanıza uyarlayın.
https://docs-aibuds.github.io/tr/skills/implement-aibuds-text-to-speech adresini okuyup yönergeleri izleyin. Bu beceriyle “AIBuds Metinden Sese Dönüşümü Uygulama” iş akışını iOS projesinde uygulayıp doğrulayın.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 başvurusuna bakın.
Yapılandırma ve Sonuç
TTSConfig isteğe bağlı speakerId sunar. Değer nil olduğunda sağlayıcı varsayılan konuşmacısını seçer.
Başarılı olduğunda TTSResultModel şunları sağlar:
| Özellik | Açıklama |
|---|---|
audioFile | Uygulamanın documents dizinine göre ses dosyası yolu. |
audioFilePath | Oluşturulan ses dosyasının tam yolu. |
audioFormat | Oluşturulan ses dosyasının biçimi. |
Kullanım Örnekleri
- 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];
});
}];
});Notlar
- Bu API bir dosya oluşturur; aygıtta oynatmayı başlatmaz ve
stopSpeaking()veyaisSpeaking()sunmaz. success,responseveerrordeğerlerini birlikte değerlendirin. Başarılı işlem içinsuccess == truevenilolmayan yanıt gerekir.- Konuşmacı tanımlayıcıları sağlayıcıya özeldir. Yalnızca seçilen sağlayıcının belgelediği tanımlayıcıyı ayarlayın.
- Arayüz ve ses oynatıcı güncellemelerini ana iş parçacığına taşıyın.