When I got a Mac mini in 2008, one of the coolest things it could do was speaking. That is an accessibility feature, but not being disabled never prevented me from staying hours typing random stuff on terminal just to get to hear the computer talking.
It turns out we developers can use that feature too. And it just takes a few lines of code. Start by opening Xcode and creating a new project.
To demonstrate the capatibilities of NSSpeechSynthesizer, we will need a NSTableView, a NSTextField and a button.
It will return a NSArray of NSStrings which are voice identifiers(e.g. com.apple.speech.synthesis.voice.Bells). To get more information about a voice(e.g. name, age, language, gender), use the following line:
It will return a NSDictionary. Now lets get the app speaking:
NSSpeechSynthesizer *synthesizer = [[NSSpeechSynthesizer alloc] init];
NSArray *voices = [NSSpeechSynthesizer availableVoices];
NSInteger selectedVoice = [tableViewVoices selectedRow];
NSString *voice = [voices objectAtIndex:selectedVoice];
NSString *text = [textToSpeak stringValue];
And that’s it! Just a few lines of code and you can get a cool feature on your app. To download the sample project, click here.