Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.

Commit de87874

Browse files
author
Anthony Oliveri
authored
Merge pull request #10 from leitdeux/update-swift-sdk
Update project to support Swift 4.2 and swift-sdk 1.1.0
2 parents 6671875 + e8d066a commit de87874

File tree

5 files changed

+31
-26
lines changed

5 files changed

+31
-26
lines changed

Cartfile.resolved

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
github "daltoniam/Starscream" "3.0.4"
2-
github "daltoniam/common-crypto-spm" "1.1.0"
3-
github "daltoniam/zlib-spm" "1.1.0"
4-
github "watson-developer-cloud/swift-sdk" "v0.21.0"
1+
github "watson-developer-cloud/swift-sdk" "1.1.0"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ Run `carthage update --platform iOS` in the command line.
2323
### Getting Started
2424

2525
1. Log in and create the service:
26-
1. Go to the [Text to Speech][sign_up] service on Bluemix and either sign up for a free account or log into your Bluemix account.
26+
1. Go to the [Text to Speech][sign_up] service on Bluemix and either sign up for a free account or log into your Bluemix account.
2727
2. Give your service a service name, and then click **Create**.
2828
2. On the new page that loads, Select **Service Credentials** and then **View Credentials** to get your authentication information.
29-
3. Update the `TextToSpeechUsername` and `TextToSpeechPassword` properties in `Credentials.swift`.
29+
3. Update the `TextToSpeechApiKey` property in `Credentials.swift`.
3030
4. Run the following command from the home directory to ignore new changes to the `Credentials.swift` file:
3131

3232
```sh
33-
$ git update-index --assume-unchanged Text\ to\ Speech/Credentials.swift
33+
$ git update-index --assume-unchanged Text\ to\ Speech/Credentials.swift
3434
```
3535

3636
## License

Text to Speech/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2222
var window: UIWindow?
2323

2424

25-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
25+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
2626
// Override point for customization after application launch.
2727
return true
2828
}

Text to Speech/Credentials.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@
1515
**/
1616

1717
struct Credentials {
18-
static let TextToSpeechUsername = "your-username-here"
19-
static let TextToSpeechPassword = "your-password-here"
18+
static let TextToSpeechApiKey = "your-api-key"
2019
}

Text to Speech/ViewController.swift

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)