简体   繁体   English

C ++如何调用Objective-C方法?

[英]how to C++ call Objective-C method?

I'm using AudioQueue to analyze the audio frequency. 我正在使用AudioQueue分析音频频率。 I saw the SpeakHere example by Apple. 我看到了Apple的SpeakHere示例。

I know to use the callback function to get the audio data in inbuffer->mAudioData. 我知道使用回调函数来获取inbuffer-> mAudioData中的音频数据。 The question is when I get the audio data, I want to update the spectrum on the UI. 问题是当我获取音频数据时,我想更新UI上的频谱。 The UI is code use Objective-C and the SpeakHere code use C++. UI是使用Objective-C的代码,而SpeakHere代码则使用C ++。 I don't know how to call the UI method in the callback function and pass the mAudioData. 我不知道如何在回调函数中调用UI方法并传递mAudioData。

Here is the callback function: 这是回调函数:

// AudioQueue callback function, called when an input buffers has been filled.

void AQRecorder::MyInputBufferHandler(  void *                              inUserData,     
                                    AudioQueueRef                       inAQ,
                                    AudioQueueBufferRef                 inBuffer,   
                                    const AudioTimeStamp *              inStartTime,
                                    UInt32                              inNumPackets,
                                    const AudioStreamPacketDescription* inPacketDesc)
{
   AQRecorder *aqr = (AQRecorder *)inUserData;
   aqr->currentQueueBufferRef = inBuffer;

   try {
       if (inNumPackets > 0) {
           // write packets to file
            XThrowIfError(AudioFileWritePackets(aqr->mRecordFile, FALSE, inBuffer->mAudioDataByteSize,
                                         inPacketDesc, aqr->mRecordPacket, &inNumPackets, inBuffer->mAudioData),
                   "AudioFileWritePackets failed");

        ***//Here I want to call Objective c Method and pass the value.***

    }

    // if we're not stopping, re-enqueue the buffe so that it gets filled again
    if (aqr->IsRunning())
        XThrowIfError(AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL), "AudioQueueEnqueueBuffer failed");
   } catch (CAXException e) {
    char buf[256];
    fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
   }
}

If you want to use both C++ and Objective-C in the same file, please make sure the extension of the file is .mm instead of .m or .cpp . 如果要在同一文件中同时使用C++Objective-C ,请确保文件的扩展名是.mm而不是.m.cpp This way, the compiler knows it's Objective-C++ , and it compiles without a problem. 这样,编译器知道它是Objective-C++ ,并且编译没有问题。

Somebody has explained it in detail in this blog post: Mixing Objective-C, C++ and Objective-C++ 有人在此博客文章中对此做了详细说明: 混合使用Objective-C,C ++和Objective-C ++

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM