Page updated Jan 16, 2024

Text-to-speech

Amplify iOS v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Swift to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for iOS, you can access the documentation here.

Set up the backend

If you haven't already done so, run amplify init inside your project and then amplify add auth (we recommend selecting the default configuration).

Run amplify add predictions, then use the following answers:

1? Please select from one of the categories below
2 Identify
3❯ Convert
4 Interpret
5 Infer
6 Learn More
7
8? What would you like to convert?
9 Translate text into a different language
10❯ Generate speech audio from text
11 Transcribe text from audio
12
13? Provide a friendly name for your resource
14 <Enter a friendly name here>
15
16? What is the source language? (Use arrow keys)
17 <Select your default source language>
18
19? Select a speaker (Use arrow keys)
20 <Select your default speaker voice>
21
22? Who should have access?
23 Auth users only
24❯ Auth and Guest users

Run amplify push to create the resources in the cloud

Working with the API

Here is an example of converting text to speech. In order to override any choices you made while adding this resource using the Amplify CLI, you can pass in a voice in the options object as shown below.

1import Amplify
2import AWSPredictionsPlugin
3import AVFoundation
4
5//...
6
7var player: AVAudioPlayer?
8
9//...
10
11func textToSpeech(text: String) {
12 let options = PredictionsTextToSpeechRequest.Options(
13 voiceType: .englishFemaleIvy,
14 pluginOptions: nil
15 )
16
17 Amplify.Predictions.convert(textToSpeech: text, options: options) { event in
18 switch event {
19 case let .success(result):
20 print(result.audioData)
21 self.player = try? AVAudioPlayer(data: result.audioData)
22 if let player = self.player {
23 player.play()
24 }
25 case let .failure(error):
26 print(error)
27 }
28 }
29}
1import Amplify
2import AWSPredictionsPlugin
3import AVFoundation
4
5//...
6
7var player: AVAudioPlayer?
8var textToSpeechSink: AnyCancellable?
9
10//...
11
12func textToSpeech(text: String) {
13 let options = PredictionsTextToSpeechRequest.Options(
14 voiceType: .englishFemaleIvy,
15 pluginOptions: nil
16 )
17
18 textToSpeechSink = Amplify.Predictions.convert(textToSpeech: text, options: options)
19 .resultPublisher
20 .sink {
21 if case let .failure(error) = $0 {
22 print(error)
23 }
24 }
25 receiveValue: { result in
26 print(result.audioData)
27 self.player = try? AVAudioPlayer(data: result.audioData)
28 if let player = self.player {
29 player.play()
30 }
31 }
32}

As a result of running this code, you will hear audio of the text being emitted from your device.