开发者

Sending bytes/data from C class to an Objective-C class

开发者 https://www.devze.com 2023-03-13 20:22 出处:网络
I am using the apple example code SpleakHere. in this code there is a calss called AQRecorder, it is a C class. i want to s开发者_开发百科end the sound bytes to an objective-c object i have. this obje

I am using the apple example code SpleakHere. in this code there is a calss called AQRecorder, it is a C class. i want to s开发者_开发百科end the sound bytes to an objective-c object i have. this object send the dat over udp. here is the function in which im trying to perform an objective-c function inside the c code.

    void AQRecorder::MyInputBufferHandler(  void *                                  inUserData,
                                        AudioQueueRef                       inAQ,
                                    AudioQueueBufferRef                 inBuffer,
                                    const AudioTimeStamp *              inStartTime,
                                    UInt32                              inNumPackets,
                                    const AudioStreamPacketDescription* inPacketDesc)
{
AQRecorder *aqr = (AQRecorder *)inUserData;
try {
    if (inNumPackets > 0) {
        // write packets to file
        XThrowIfError(AudioFileWritePackets(aqr->mRecordFile, FALSE, inBuffer->mAudioDataByteSize,
                                         inPacketDesc, aqr->mRecordPacket, &inNumPackets, inBuffer->mAudioData),
                   "AudioFileWritePackets failed");
        aqr->mRecordPacket += inNumPackets;
        NSData* data=[NSData dataWithBytes:inBuffer->mAudioData length:inBuffer->mAudioDataByteSize];
      [(Udp*)udpp  sendData:data];
    }

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

}

although the NSData functions works fine, i am not able to ad my object (Udp) to the class and therefore cant call its functions. i tried declaring the Udp object everywhere (inside class,outside class,header .mm file ,as void*...) but nothing would compile...

though i wan't able to add an object in order to send the data... i used NSNptification instead in order to port the data from the c object to the objective-c object.

help please


though i wan't able to add an object in order to send the data... i used NSNotification instead in order to port the data from the c object to the objective-c object


Try changing the extension of all .m files to .mm.

If that works, then you have a dependency issue. If an Objective-C file includes a C++ file, then the extension of that Obj-C file should be .mm instead of .m. Moreover, if this Objective-C file now gets included in some other Objective-C file, then the extension of that file should be .mm as well.


Check file type. Select "Objective C++ preprocessed". You can make in File inspector window on the right panel for XCode4 or "Get info" in file context menu for older XCode versions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号