Skip to main content

Text to Speech

Synthesize text through the selected AI provider and receive a local audio file that your app can play or otherwise process.

Prerequisites

  • AIBudsAISDK is 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.

Implement with AI Assistance

Build with AI

Implement this workflow with AI

Use the official Implement AIBuds Text to Speech skill to adapt this workflow to your app.

Read and follow https://docs-aibuds.github.io/skills/implement-aibuds-text-to-speech. Use it to implement Implement AIBuds Text to Speech in this iOS project and verify the result.
View official skill

API Reference

Framework

AIBudsAI.xcframework

Import

Swift
import AIBudsAI
import AIBudsAIFoundation

Declaration

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

See synthesizeText.

Configuration and Result

TTSConfig exposes an optional speakerId. When it is nil, the provider selects its default speaker.

On success, TTSResultModel provides:

PropertyDescription
audioFileAudio file path relative to the app's documents directory.
audioFilePathFull path to the synthesized audio file.
audioFormatFormat of the synthesized audio file.

Usage Examples

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)
        }
    }
}

Notes

  • This API synthesizes a file; it does not start device playback and does not expose stopSpeaking() or isSpeaking().
  • Treat success, response, and error together. A successful operation requires success == true and a non-nil response.
  • 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.