NSSpeechRecognizer example

2019-05-03 12:02发布

问题:

Ok so I need to do this:

Wait for command, "Goodnight". Then run an action.

Can someone explain how do accomplish this?

回答1:

Try this website:

http://www.cocoadev.com/index.pl?NSSpeechRecognizer

And modify as such:

NSSpeechRecognizer *listen;
NSArray *cmds = [NSArray arrayWithObjects:@"goodnight",nil];
listen = [[NSSpeechRecognizer alloc] init];
[listen setCommands:cmds];
[listen setDelegate:self];
[listen setListensInForegroundOnly:NO];
[listen startListening];
[listen setBlocksOtherRecognizers:YES];

- (void)speechRecognizer:(NSSpeechRecognizer *)sender didRecognizeCommand:(id)aCmd {
    if ([(NSString *)aCmd isEqualToString:@"goodnight"]) {
        [self performSelector:@selector(goodnightMethod:)];
    }
}

Your method for handling good night would be (with accordance to what I have written):

-(void)goodnightMethod:(id)sender {
    //Do stuff here...
}