简体   繁体   English

将字节/数据从C类发送到Objective-C类

[英]Sending bytes/data from C class to an Objective-C class

I am using the apple example code SpleakHere . 我正在使用苹果示例代码SpleakHere in this code there is a calss called AQRecorder, it is a C class. 在这段代码中有一个叫做AQRecorder的calss,它是一个C类。 i want to send the sound bytes to an objective-c object i have. 我想将声音字节发送到我拥有的objective-c对象。 this object send the dat over udp. 该对象通过udp发送数据。 here is the function in which im trying to perform an objective-c function inside the c code. 这是我试图在c代码中执行objective-c函数的函数。

    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. 虽然NSData函数工作正常,但我无法将我的对象(Udp)添加到类中,因此无法调用其函数。 i tried declaring the Udp object everywhere (inside class,outside class,header .mm file ,as void*...) but nothing would compile... 我尝试到处声明Udp对象(在类内部,类外,标题.mm文件,作为void * ...)但没有任何东西可以编译...

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. 虽然我无法添加一个对象以发送数据...我使用NSNptification代替将数据从c对象移植到objective-c对象。

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 虽然我不能添加一个对象来发送数据...我使用NSNotification来代替将数据从c对象移植到objective-c对象

Try changing the extension of all .m files to .mm . 尝试将所有.m文件的扩展名更改为.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 . 如果Objective-C文件包含C ++文件,则该Obj-C文件的扩展名应为.mm而不是.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. 此外,如果此Objective-C文件现在包含在其他一些Objective-C文件中,那么该文件的扩展名也应该是.mm

Check file type. 检查文件类型。 Select "Objective C++ preprocessed". 选择“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. 您可以在XCode4的右侧面板上的“文件检查器”窗口中创建,或者在较旧的XCode版本的文件上下文菜单中的“获取信息”。

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

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