开发者

Simple NSSpeechRecognizer code, not working!

开发者 https://www.devze.com 2022-12-19 20:16 出处:网络
I noticed NSSpeechRecognizer in ADC library and I found it to be very interesting, so to play with it I prepared a simple application which will just listen the command and if recognized it displays i

I noticed NSSpeechRecognizer in ADC library and I found it to be very interesting, so to play with it I prepared a simple application which will just listen the command and if recognized it displays it in log.

The code used is:

- (id)init {

    if (self = [super init]) {
        // Insert code here to initialize your application 
        NSArray *cmds = [NSArray arrayWithObjects:@"A",@"B", @开发者_开发问答"C",@"alpha",@"beta",@"vodka",@"wine",nil]; 
        recog = [[NSSpeechRecognizer alloc] init]; // recog is an ivar
        [recog setCommands:cmds];
        [recog setDelegate:self];
    }
    return self;
}

- (IBAction)listen:(id)sender
{   NSLog(@"listen:");
    if ([sender state] == NSOnState) { // listen
        [recog startListening];
    } else {
        [recog stopListening];
    }
}

- (void)speechRecognizer:(NSSpeechRecognizer *)sender didRecognizeCommand:(id)aCmd {
    NSLog(@"speechRecognizer: %@",(NSString *)aCmd);
}

I tried it many times for the commands registered but I was unable to get none of the messages in log, in delegate :(

There was always some noise in the background.. could this be the reason for it or I have done something wrong in the code??

Can anyone suggest me some solution for it??

Thanks,

Miraaj


Code looks fine so far.

The NSSpeechRecognizer is a bit tricky sometimes and refuses to listen to the right words. Did you try different words?

Did you try setting startListening as default?

I wrote a little tutorial some time ago. Its in german language but maybe it will help you anyway or you use some translation tool.

http://cocoa-coding.de/spracherkennung/nsspeechrecognizer1.html

0

精彩评论

暂无评论...
验证码 换一张
取 消