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
An Icy Challenge, Accepted | StarTalk
So check this out. You guys are both athletes. So I read this great article, and it was talking about how athletes are able to deal with pain unlike regular people. Non-athletes cannot deal with pain the way athletes. So it’s real. Because I was suspectin…
Strategies for dividing multiples of 10, 100 and 1000
We’re going to do in this video is get some practice doing division with numbers that are multiples of 10, 100, 1000, things like that. So, let’s say we wanted to compute what 2400 divided by 30 is. Pause this video and see if you can calculate it using w…
10 Mental Mistakes That Keep You From Getting Rich
When it comes to getting rich, who do you think is your greatest enemy? We’ll answer that question for you: it is yourself, and you might not even be aware of it. That’s because our own psychology will work against us unless we make an effort to understan…
Advice on Organizing and Running Growth Teams from Dan Hockenmaier and Gustaf Alströmer
Today we have Dan Hakan Meyer and Gustav All Strimer. So, Dan was the founder, advised investor, and advisor at Basis One, which is growth strategy consulting. Previous to that, you were a director of growth marketing at Thumbtack. Gustav’s a partner at Y…
The Current State of the Stock Market (2021)
So that right there is Charlie Monkey. He’s essentially Warren Buffett’s right hand man. He is the vice chairman of Berkshire Hathaway. What we’re looking at right here is him giving a talk at the Daily Journal shareholders meeting, which happened a coupl…
Arteries vs. veins-what's the difference? | Circulatory system physiology | NCLEX-RN | Khan Academy
Let’s talk a little bit about arteries and veins and the roles they play in the circulatory system. So, I want you to pause this video and first think to yourself, do you have a sense of what arteries and veins are? Well, one idea behind arteries and vein…