ios - 在回调函数中调用 Objective C 方法

标签 ios objective-c c

我一直在使用 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/

相关文章:

c - 如果子进程在父进程调用 wait() 之前退出怎么办?

c - 为什么代码只输出 'Have a nice day?' 尽管输入了 Friday 并声明了给定的字符串?

c++ - C 宏的使用 '##' 和 '###' 也是指针

objective-c - Cocoa 中与 void* 上下文相关的内存管理

iphone - 如何在 iPhone 的 UIGraphicsImageContext 中绘制带有属性(特别是描边和填充颜色)的文本

iphone - 使用 CGContext 显示 iPhone 的 Sprite 表

ios - 希望快速将用户输入发送到网络服务器并加载结果

iOS UITableView - 向左滑动并单击单元格时显示删除按钮

ios - UIDocumentInteractionController 日历访问

ios - 如何在Xcode中进行Wi-Fi文件共享