Swift Examples - ElevenLabs
Service setup
Create a ElevenLabs service in the AIProxy dashboard
Follow the integration guide, selecting the ElevenLabs icon on the 'Create a New Service' form.
How to use ElevenLabs for text-to-speech
import AIProxy
let elevenLabsService = AIProxy.elevenLabsService(
partialKey: "partial-key-from-your-developer-dashboard",
serviceURL: "service-url-from-your-developer-dashboard"
)
do {
let body = ElevenLabsTTSRequestBody(
text: "Hello world"
)
let mpegData = try await elevenLabsService.ttsRequest(
voiceID: "EXAVITQu4vr4xnSDxMaL",
body: body
)
// Do not use a local `let` or `var` for AVAudioPlayer.
// You need the lifecycle of the player to live beyond the scope of this function.
// Instead, use file scope or set the player as a member of a reference type with long life.
// For example, at the top of this file you may define:
//
// fileprivate var audioPlayer: AVAudioPlayer? = nil
//
// And then use the code below to play the TTS result:
audioPlayer = try AVAudioPlayer(data: mpegData)
audioPlayer?.prepareToPlay()
audioPlayer?.play()
} catch AIProxyError.unsuccessfulRequest(let statusCode, let responseBody) {
print("Received \(statusCode) status code with response body: \(responseBody)")
} catch {
print("Could not create ElevenLabs TTS audio: \(error.localizedDescription)")
}
- See the full range of TTS controls by viewing ElevenLabsTTSRequestBody.swift.
- See https://api.elevenlabs.io/v1/voices for the IDs that you can pass to voiceID.