2013-12-12 32 views
0

我想安裝一個碳API的回調函數,但它不起作用:事件正確觸發它(當它完成講話時),但它返回分段錯誤11而不是打印「......已完成」。回調函數與碳API

下面的代碼:

... 
/* 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."); 
}; 

我想我沒有正確安裝回調函數?

回答

0

問題是我沒有使用指針回調函數創建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); 
...