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
Causes of shifts in currency supply and demand curves | AP Macroeconomics | Khan Academy
Talk a little bit about what could cause the supply or demand curve for a currency to shift. So here we have the foreign exchange market for the Chinese yuan, which is why we have the quantity of one on the horizontal axis and the price of one in terms o…
Getting To Kiwalik - Behind the Scenes | Life Below Zero
Campers aren’t working. That’s getting super frustrating. This is what it’s like on Life Below Zero. Cameras are already down. Tough conditions all around. I fill in: no heat, no car, no anything; won’t even turn on. Too many times we’ve had batteries go …
Example: Comparing distributions | AP Statistics | Khan Academy
What we’re going to do in this video is start to compare distributions. So for example, here we have two distributions that show the various temperatures different cities get during the month of January. This is the distribution for Portland; for example,…
The Sci in Sci-Fi | StarTalk
Even though Kevin Smith is a huge pop culture fan and science fiction fan, he remains science-curiosity challenged. No. Yeah, yeah. I have evidence of that. Let’s check it out. You’re talking to a man who, at age 46, is still not quite sure how the water…
Examples identifying conditions for inference on two proportions | AP Statistics | Khan Academy
A sociologist suspects that men are more likely to have received a ticket for speeding than women are. The sociologist wants to sample people and create a two-sample z interval. In other videos, we introduce what that idea is: to estimate the difference b…
How Confidence Is Holding You Back
Hello Alexa, welcome back. Let’s be real here for a moment. Everything that is worth doing and everything that is worth getting needs a healthy dose of something that today’s society doesn’t really have anymore. And that thing is courage. The courage to …