Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////
- // .h
- /////////////
- //
- // AppDelegate.h
- // SpeakLine
- //
- // Created by Michael Crook on 12/01/13.
- // Copyright (c) 2013 Michael Crook. All rights reserved.
- //
- #import <Cocoa/Cocoa.h>
- @interface AppDelegate : NSObject <NSApplicationDelegate, NSSpeechSynthesizerDelegate, NSTableViewDelegate>
- {
- NSArray *_voices;
- NSSpeechSynthesizer *_speechSynth;
- }
- @property (assign) IBOutlet NSWindow *window;
- @property (weak) IBOutlet NSTextField *textField;
- @property (weak) IBOutlet NSButton *stopBtn;
- @property (weak) IBOutlet NSButton *speekBtn;
- @property (weak) IBOutlet NSTableView *tableView;
- - (IBAction)stopIt:(id)sender;
- - (IBAction)sayIt:(id)sender;
- @end
- /////////////
- // .m
- /////////////
- //
- // AppDelegate.m
- // SpeakLine
- //
- // Created by Michael Crook on 12/01/13.
- // Copyright (c) 2013 Michael Crook. All rights reserved.
- //
- #import "AppDelegate.h"
- @implementation AppDelegate
- @synthesize window = _window;
- @synthesize textField = _textField;
- @synthesize tableView = _tableView;
- - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
- {
- // Insert code here to initialize your application
- }
- - (id)init
- {
- self = [super init];
- if(self)
- {
- NSLog(@"AppDelegate initiating");
- // initiate speach synth.
- _speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:Nil];
- }
- [_speechSynth setDelegate:self];
- [_tableView setDelegate:self];
- [_stopBtn setEnabled:FALSE];
- [_speekBtn setEnabled:TRUE];
- _voices = [NSSpeechSynthesizer availableVoices];
- return (self);
- }
- - (NSInteger)numberOfRowsInTableView:(NSTableView *)tv
- {
- return (NSInteger)[_voices count];
- }
- - (id)tableView:(NSTableView *)tv
- objectValueForTableColumn:(NSTableColumn *)tableColumn
- row:(NSInteger)row
- {
- NSString *v = [_voices objectAtIndex:row];
- NSDictionary *dict = [NSSpeechSynthesizer attributesForVoice:v];
- return [dict objectForKey:NSVoiceName];
- }
- - (void)tableViewSelectionDidChange:(NSNotification *)notification
- {
- NSInteger row = [_tableView selectedRow];
- if (row == –1)
- {
- return;
- }
- NSString *selectedVoice = [_voices objectAtIndex:row];
- [_speechSynth setVoice:selectedVoice];
- NSLog(@"new voice = %@", selectedVoice);
- }
- - (void)speechSynthesizer:(NSSpeechSynthesizer *)sender
- didFinishSpeaking:(BOOL)finishedSpeaking
- {
- NSLog(@"finishedSpeaking = %d", finishedSpeaking);
- [_stopBtn setEnabled:FALSE];
- [_speekBtn setEnabled:TRUE];
- }
- -(void)speechSynthesizer:(NSSpeechSynthesizer *)sender
- willSpeakWord:(NSRange)characterRange ofString:(NSString *)string
- {
- [_stopBtn setEnabled:TRUE];
- }
- - (IBAction)stopIt:(id)sender
- {
- NSLog(@"Stopping Speaking");
- // stop talking
- [_speechSynth stopSpeaking];
- }
- - (IBAction)sayIt:(id)sender
- {
- NSString *string = [_textField stringValue];
- // is the string 0 long
- if([string length] == 0)
- {
- NSLog(@"string from %@ is of zero length", _textField);
- }
- else
- {
- [_speechSynth startSpeakingString:string];
- NSLog(@"Started speaking: %@", string);
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment