我试图让语音识别在 MacBook(OS 10.8.2)上工作,但我从来没有在委托方法上得到任何回调。将 XCode 4.6 与 ARC 一起使用,这是我的简单测试代码。我确实在控制台中得到了“监听”输出。“麦克风”出现在屏幕上,如果我按 ESC 键,那么我可以在麦克风的显示屏上看到我的讲话模式,但仍然没有代表回调。必须有一些基本的东西,但我还没有找到。
我看过很多 SO 问题,但没有一个能解决这个问题。有些人谈论控制面板中的校准,但我没有找到任何校准(也许那是以前的操作系统?)。
github中提供了完整的项目源代码。
#import "RBListener.h"
@interface RBListener() <NSSpeechRecognizerDelegate>
@property (nonatomic, strong, readonly) NSSpeechRecognizer* recognizer;
@property (nonatomic, strong) NSArray* commands;
@end
@implementation RBListener
@synthesize recognizer = _recognizer;
- (id)init
{
self = [super init];
if (self) {
// initialize
_commands = @[@"hi", @"yes", @"no", @"hello", @"good", @"time"];
_recognizer = [[NSSpeechRecognizer alloc] init];
_recognizer.delegate = self;
_recognizer.commands = _commands;
_recognizer.listensInForegroundOnly = NO;
_recognizer.blocksOtherRecognizers = YES;
[_recognizer startListening];
DLog(@"listening");
}
return self;
}
#pragma mark -
#pragma mark NSSpeechRecognizerDelegate methods
- (void)speechRecognizer:(NSSpeechRecognizer*)sender didRecognizeCommand:(id)command
{
DLog(@"command: %@", command);
}
@end