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
How to Make Fresh Sprouts | Live Free or Die: How to Homestead
I would like to show you how to make fresh clover sprouts. It’s one of the things that Tony and I have a hard time getting in the winter: our fresh vegetables. One solution to that is to make sprouts. Here I have a whole bunch of clover seeds. Just take a…
The Greatest Sled Dog - Deleted Scene | Life Below Zero
All right, done painting my boat. Now, the last thing to do is put the finishing touches on it. Gonna paint the name of the boat. The name of the boat is Queen Rosa, the best lead dog I’ve ever had. All my whole dog yard comes about out of her: their gran…
BONUS VIDEO | Origin of the Mutant Plural | Grammar | Khan Academy
Hello grammarians! I wanted to talk to you again about mutant plurals. So, to review, a mutant plural is… there are only seven of them in English, and they all change sound when they pluralize. You don’t add an “s,” you don’t add an “en,” you don’t change…
HOW TO TURN $100 INTO $1000 (6 BEST WAYS)
What’s up, Graham? It’s Guys here. So, the other week, I posted a video about how to make $100 a day in passive income, but I’ll be honest: most of that video assumes a long-term investment, consistent work, and slowly building up to an amount that would …
Writing standard equation of a circle | Mathematics II | High School Math | Khan Academy
[Voiceover] So we have a circle here and they specified some points for us. This little orangeish, or, I guess, maroonish-red point right over here is the center of the circle, and then this blue point is a point that happens to sit on the circle. And s…
HOW TO BUY: Bitcoin, Litecoin, and Ethereum (Step by Step)
What’s up you guys! It’s Gran here. So, this has been something that has been requested in the hundreds of times. People have been hitting me up on Snapchat, on Instagram, and many, many times in the comments, asking how to go about buying Bitcoin, Liteco…