我一直在使用 Dragonmobile sdk 和 Audio Sound System,尝试进行语音识别 -> 文本到语音链。
这是我的代码:
(这里有语音识别部分)
- (void)recognizer:(SKRecognizer *)recognizer didFinishWithResults:{
...
SystemSoundID id = [self playSound:url2]; //playSound
AudioServicesAddSystemSoundCompletion (
id,
NULL,
NULL,
detectSoundFinish,
NULL
);
...
}
- (void)performRecognition:(id)sender
{
if (!recognizer){
self.recognizer = [[SKRecognizer alloc] initWithType:SKDictationRecognizerType detection:SKLongEndOfSpeechDetection language:@"en_US" delegate:self];
}
}
void detectSoundFinish ( SystemSoundID ssID, void *clientData )
{
printf("end\n");
//I want to call performRecognition here, or an equivalent thing.
}
我是 Objective C 的新手(甚至 C。我主要用 Python 编写代码),并且我知道回调函数不属于我的类。所以,我的问题是 1. 有没有办法在回调函数中调用objc方法? 2. 或者,从回调访问主类的属性的方法? 我一整天都在进行搜索和尝试,但还没有弄清楚如何准确地做到这一点。 谢谢!
最佳答案
您不能使用 Objective-C 函数作为 C 函数的回调。
您可以做的是使用最后一个参数(旨在传递您想要的任何数据)来传递指向您的类实例的指针。
AudioServicesAddSystemSoundCompletion (id,
NULL,
NULL,
MyAudioServicesSystemSoundCompletionProc,
self);
并在 C 回调函数体内,将指针强制转换为您的类,然后使用它!
void MyAudioServicesSystemSoundCompletionProc (SystemSoundID ssID, void *clientData);
{
MyClass *obj = (MyClass*)clientData;
// use obj in the normal Objective-C way!!!
}
关于ios - 在回调函数中调用 Objective C 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22586472/