Calling a Objective C function from C++ Code

前端 未结 2 1254
野趣味
野趣味 2021-02-15 13:37

I googled around and I find a million results to this subject. But none of the pages helps me. I think that I have a very common problem. I\'m playing around with audio programm

2条回答
  •  不要未来只要你来
    2021-02-15 14:15

    self only exists in Objective-C methods and that is a C style function. You need to pass self from an Objective-C method to the inUserData when you set up the callback, then cast it back to the correct type.

    //This is an example for using AudioQueueNewInput
    //Call this in an Objective-C method passing self to inUserData
    AudioQueueNewInput (
       const AudioStreamBasicDescription  *inFormat,
       AudioQueueInputCallback            inCallbackProc,
    
       // this is where you will pass (void*)self
       void                               *inUserData, 
       CFRunLoopRef                       inCallbackRunLoop,
       CFStringRef                        inCallbackRunLoopMode,
       UInt32                             inFlags,
       AudioQueueRef                      *outAQ
    );
    

    And your original implementation

    static void HandleInputBuffer (void                                 *aqData,
                               AudioQueueRef                        inAQ,
                               AudioQueueBufferRef                  inBuffer,
                               const AudioTimeStamp                 *inStartTime,
                               UInt32                               inNumPackets,
                               const AudioStreamPacketDescription   *inPacketDesc ) 
    {
        AudioRecorder *ar_instance = (AudioRecorder*)aqData;
        ...
        [ar_instance playAlarmSound];
        ...
    }
    

提交回复
热议问题