yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Mac Programming Lesson 5


4m read
·Nov 3, 2024

Hey guys, this is Mac Kids, no one with Mac programming lesson five.

In this lesson, I'm going to be showing you how to use a speech synthesizer, which is the way that you make a computer say stuff through the actual speakers using a physical human voice. So the class for this is just NS Speech Synthesizer, and it's pretty easy to use.

I'll show you how to do this by creating a new project in Xcode and making a simple app that uses it. So I'll create a new project. I'll make it a Cocoa application, and I'll call it "S". Now under classes, I'll create a new class, Cocoa Objective-C class, and I'll call it "AppController".

So in "AppController," this is where we're going to be typing all our code. First of all, in "AppController," we're going to declare a new NS Speech Synthesizer. I'll declare it right here, and I'll call it "SS" for speech synthesizer.

Now up here, I'm going to have two IB Outlets: one for an NS Text Field, and I'll just call it "textField1." I'll have one for an NS Button, and I'll call it "button1." So that's easy. Now I'll add one IB Action called "startSpeaking," and I'll do an ID sender here.

So I'll just copy this, paste it into the implementation, and I'll get right started typing the code. But before we do that, you have to understand one thing: a speech synthesizer is like an NS String in the way that it has to be allocated and initialized in order to actually work.

And what better time to do that than when the application first opens? The thing is that awake from nib, which we learned in the last lesson, really isn't the best way to initialize things and start up your application when you're doing things like setting properties on user interface controls.

Awake from nib is perfect, but when you're doing more things like allocating stuff and setting the delegate of stuff and stuff like that, you really want to use another way of running code when the application initializes, and this is ID init.

So ID init has to return an ID, of course. We'll return self, and you have to run super init at the beginning to initialize and run the original thing. So you can kind of ignore that for now. Right in this ID init, between the super init and return self, all we're going to do is "SS = NSSpeechSynthesizer alloc] initWithVoice:nil."

Of course, that nil is not a string. So the way it works is if you pass it nil as the voice, then it just makes the voice be the default computer voice that you can set through system preferences.

Now, I'm going to call speech synthesizer, or SS in this case, set delegate self. The reason we're doing this is basically a delegate means that now SS knows who to tell when something happens, in that case itself, or this current class.

The way it tells when something happens, it'll run functions on its delegate. So for instance, now if I add a little function right at the bottom of here, this code right here, believe it or not, will run when the speech synthesizer is done because this is a magic method that's declared in the delegate of any speech synthesizer.

So it'll call stuff on the delegate. So I'm setting the delegate itself; that means I know when it's done, and I know all this whole stuff, so just keep that in mind. We do need this code right here, and we're going to put stuff in here when it's done speaking.

So now under this IB Action, we're going to say button1 setEnabled:no, and we're going to start speaking. So we're going to say "SS startSpeaking:textField1.stringValue." In this way, we're going to start speaking the text of textField1, and textField1 is where they're going to type some text.

And button1, in this case, is going to be this speak button that we're going to disable while it's talking so that way you can't say two things at once. And then when it's done talking, we're going to reenable the button by doing button1 setEnabled:yes.

And by the way, if you don't really want to type this in right from the video, I will have a link in the description to download the Xcode project for this, so that way you can use it instead.

So anyway, here we go. Now all we need to do is the interface. So I'll go into Interface Builder by double-clicking on the MainMenu.xib, and I will drag on two necessary things: a button and a text field. So the text field is going to go here, and the button here.

Now resize this window. So now I'm going to grab a little blue NS object and drag it to my document. You can get by going to Window > Document. On this object, I'm going to set the class to be AppController.

And now I'm going to click control from AppController to this first text field and link it up. Same thing with the button, back again. And just to ensure that everything's working, we're going to call an NSLog here, and we're going to log textField1.stringValue so that way we know what we're speaking.

So now if I build and go into the console, we'll see the application appear. I'll type "foo," and my computer just said "f" in a very, I guess it doesn't really know how to pronounce it. And there we go.

So as you can see, the button disables, and it comes up in the console. So we can get rid of this NSLog right here, but it's great! It works. I'll have the source code downloadable in the description.

So thanks for watching Mac kids in one. You can try making this application; you can mess around with the code a little bit. You have full permissions to do whatever you want with this code.

So anyway, thanks for watching, mades in one. Post any comments or questions you have in the description of the video. Subscribe, and goodbye!

More Articles

View All
Space Probe Cemetery | Exomars: The Hunt For Life
The first successful Mars mission was in 1964. Along the way, Mars would become the space probe cemetery. Bogalusa, a lot of space probes have been lost along the way; some of them we lost track of upon their arrival. Others flew past the planet without s…
Plotting inequalities on a number line | Equations & inequalities | 6th grade | Khan Academy
We’re told that Pierre has 48 minutes until he needs to get ready for his dance lesson. Graph how many minutes he can spend playing with his pet before getting ready. If you are so inspired, I encourage you to be so inspired, pause the video, and see if y…
What Will Happen If the Rivers Disappear? | Short Film Showcase
[Music] It would make a huge difference, folks. We’re making the decisions; we’d get out and see just how special Texas rivers are and Texas bays are. I have to believe it would change the way they approach decisions, really understand what’s at stake. S…
Crossing a Snow Packed River | Primal Survivor
The big danger here is I could fall through, and depending on how deep it is, if it’s deep, that river could suck me under the ice. So, I’ve got to come up with a plan. This is where a little bit of, uh, mountaineering strategy comes in. Get my snow shov…
Example visually evaluating discrete functions
What we have here is a visual depiction of a function, and this is a depiction of y is equal to h of x. Now, when a lot of people see function notation like this, they can see it as somewhat intimidating until you realize what it’s saying. All a function …
JOKER | Psychology & Philosophy (based on Carl Jung & Albert Camus)
The Joker is a recurring supervillain from DC comics and is the archenemy of Batman. In the many books and movies about the Joker, he is portrayed as an unpredictable, bloodthirsty, crazy psychopath that wants to see the world burn. Hence, his nicknames l…