So far all six of the apps I’ve released to the Apple App Store have been games. In fact they have all been pretty basic games.
The next two apps in my release schedule are also games, although one is a quiz app with multiple quiz types – more on that soon.
But after that I’ll be releasing my first non-game app and this one almost built itself.
IOS and Swift are surprisingly easy to work with, particularly due to the huge number of easy to use and well documented libraries. In fact they get easier with each new release.
While researching AVFoundation to find better ways and more options for a sound based app I’m working on I came across AVSpeechSynthesizer. It basically allows you to have the phones voice read a defined string.
Using the utterance method, a textView and a button I could allow the user write something and have it read to them. Say It For Me was born
I added some style, a few predefined quick phrases and a stop button. I added some design to the app and it was ready to go.
That’s pretty much all version one will do – let you write text and have it read to you or select from one of four predefined text fields.
Version two will have more default phrases, let you create your own defaults and even read a few top news stories to you.
So how is it made?
You can download this project on GitHub and reuse the code or using it as a guide.
First we have to import AVFoundation as it is part of that incredible toolkit.
Then we need to set a variable for the speech synth so you can call it later and another for the utterance (what you want to say). We do this above View Did Load.
let synth = AVSpeechSynthesizer() var myUtterance = AVSpeechUtterance(string: "")
Add a button to your Storyboard and ctrl drag it as an IBAction into the View Controller class. Then within that Action add the following:
myUtterance = AVSpeechUtterance(string: "I've lost my voice") myUtterance.rate = Float(utteranceRate) synth.speak(myUtterance)
The string is what your device will actually say when you click the button.
This is a VERY basic example of how to use it and is locked to whatever you write within the button. If you want to change the text then you’d just create a textField or a textView, import it as an outlet and replace “I’ve lost my voice” (above) with textField.text – or whatever you call it.
But what if the user inputs a huge amount of text and gets bored with it talking? Simple just create a stop button, import it as an action and add the following within the action:
You can also use pausSpeaking and continueSpeaking to add even more functionality or add a slider tied to the utteranceRate to let the user speed up or slow down how fast the phone is reading.
You’d just make sure you declare the utteranceRate as a variable then increase or reduce it with a slider or stepper.