@@ -52,10 +52,7 @@ class ViewController: UIViewController {
5252 super. viewDidLoad ( )
5353
5454 // Instantiate Text to Speech service
55- textToSpeech = TextToSpeech (
56- username: Credentials . TextToSpeechUsername,
57- password: Credentials . TextToSpeechPassword
58- )
55+ textToSpeech = TextToSpeech ( apiKey: Credentials . TextToSpeechApiKey)
5956
6057 // Load the supported voices
6158 loadVoices ( )
@@ -84,11 +81,21 @@ class ViewController: UIViewController {
8481 }
8582
8683 func loadVoices( ) {
87- let failure = { ( error: Error ) in print ( error) }
88- textToSpeech. getVoices ( failure: failure) { voices in
84+ textToSpeech. listVoices { ( response, error) in
85+ if let error = error {
86+ print ( error)
87+ return
88+ }
89+
90+ guard let voices = response? . result? . voices else {
91+ print ( " Failed to get voices " )
92+ return
93+ }
94+
8995 for voice in voices {
9096 self . voices. append ( voice. name)
9197 }
98+
9299 DispatchQueue . main. async {
93100 self . voicesTableView. reloadData ( )
94101 }
@@ -97,8 +104,8 @@ class ViewController: UIViewController {
97104
98105 /** Error handling to make sure user fills in required fields before speaking. */
99106 func alertUser( title: String , text: String ) {
100- let alert = UIAlertController ( title: title, message: text, preferredStyle: UIAlertControllerStyle . alert)
101- alert. addAction ( UIAlertAction ( title: " Dismiss " , style: UIAlertActionStyle . default, handler: nil ) )
107+ let alert = UIAlertController ( title: title, message: text, preferredStyle: UIAlertController . Style . alert)
108+ alert. addAction ( UIAlertAction ( title: " Dismiss " , style: UIAlertAction . Style . default, handler: nil ) )
102109 self . present ( alert, animated: true , completion: nil )
103110 }
104111
@@ -125,14 +132,16 @@ class ViewController: UIViewController {
125132 }
126133
127134 // Synthesize the text
128- let failure = { ( error: Error ) in print ( error) }
129- textToSpeech. synthesize (
130- text,
131- voice: voice,
132- audioFormat: . wav,
133- failure: failure)
134- {
135- data in
135+ textToSpeech. synthesize ( text: text, accept: " audio/wav " , voice: voice) { ( response, error) in
136+ if let error = error {
137+ print ( error)
138+ }
139+
140+ guard let data = response? . result else {
141+ print ( " Failed to synthesize text " )
142+ return
143+ }
144+
136145 do {
137146 self . player = try AVAudioPlayer ( data: data)
138147 self . player!. play ( )
0 commit comments