【问题标题】:Callback function with carbon API使用 carbon API 的回调函数
【发布时间】:2013-12-12 06:30:51
【问题描述】:

我正在尝试使用 carbon API 安装一个回调函数,但它不起作用:它被事件正确触发(当它完成讲话时)但它返回一个 分段错误 11而不是打印“...完成。”。

代码如下:

...
/* Creates SpeechChannel */
SpeechChannel speechchannel;
NewSpeechChannel(NULL,&speechchannel);


/* Sets callback */
CFNumberRef cbf = CFNumberCreate (
                            NULL,
                            kCFNumberLongType,
                            MySpeechDoneProc
                            );
OSErr error1;
error1 = SetSpeechProperty (speechchannel,kSpeechSpeechDoneCallBack,cbf);


/* Speaks it */
OSErr error2;
error2 = SpeakCFString(speechchannel, finalString, NULL);
...

后来有:

void MySpeechDoneProc (SpeechChannel chan,long refCon)
{
printf("...Finished.");
};

我猜我没有正确安装回调函数?

【问题讨论】:

    标签: macos callback text-to-speech macos-carbon


    【解决方案1】:

    问题是我在创建 CFNumberRef 时没有使用 指针 来回调函数。解决方案是双重的:

    1) 声明函数的指针和函数:

    /* callback function */
    void MySpeechDoneProc (SpeechChannel chan,long refCon);
    /* callback function pointer */
    void (*MySpeechDoneProcPtr)(SpeechChannel,long);
    

    2) 将回调函数指针的地址作为第三个参数传入CFNumberCreate:

    error1 = SetSpeechProperty (speechchannel,kSpeechSpeechDoneCallBack,cbf);
    

    这是工作代码:

    ...
    /* Creates SpeechChannel */
    SpeechChannel speechchannel;
    NewSpeechChannel(NULL,&speechchannel);
    
    
    /* Sets callback */
    MySpeechDoneProcPtr = &MySpeechDoneProc;
    CFNumberRef cbf = CFNumberCreate (
                                NULL,
                                kCFNumberLongType,
                                &MySpeechDoneProcPtr
                                );
    OSErr error1;
    error1 = SetSpeechProperty (speechchannel,kSpeechSpeechDoneCallBack,cbf);
    
    
    /* Speaks it */
    OSErr error2;
    printf("Speaking...\n");
    error2 = SpeakCFString(speechchannel, finalString, NULL);
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-19
      • 1970-01-01
      • 2018-12-29
      相关资源
      最近更新 更多