NSSpeechRecognizer示例

时间:2011-05-24 00:13:30

标签: objective-c command

好的,我需要这样做:

等待命令,“晚安”。然后运行一个动作。

有人可以解释如何做到这一点吗?

1 个答案:

答案 0 :(得分:9)

试试这个网站:

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

并按原样修改:

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:)];
    }
}

你处理晚安的方法是(根据我所写的):

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