如何在回调函数中调用目标c函数
回调功能:
static OSStatus inputRenderCallback (
void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData
) {
soundStructPtr soundStructPointerArray = (soundStructPtr) inRefCon;
UInt32 frameTotalForSound = soundStructPointerArray[inBusNumber].frameCount;
BOOL isStereo = soundStructPointerArray[inBusNumber].isStereo;
AudioUnitSampleType *dataInLeft;
AudioUnitSampleType *dataInRight;
dataInLeft = soundStructPointerArray[inBusNumber].audioDataLeft;
if (isStereo) dataInRight = soundStructPointerArray[inBusNumber].audioDataRight;
AudioUnitSampleType *outSamplesChannelLeft;
AudioUnitSampleType *outSamplesChannelRight;
outSamplesChannelLeft = (AudioUnitSampleType *) ioData->mBuffers[0].mData;
if (isStereo) outSamplesChannelRight = (AudioUnitSampleType *) ioData->mBuffers[1].mData;
UInt32 sampleNumber = soundStructPointerArray[inBusNumber].sampleNumber;
for (UInt32 frameNumber = 0; frameNumber < inNumberFrames; ++frameNumber) {
outSamplesChannelLeft[frameNumber] = dataInLeft[sampleNumber];
if (isStereo) outSamplesChannelRight[frameNumber] = dataInRight[sampleNumber];
sampleNumber++;
if (sampleNumber >= frameTotalForSound){
sampleNumber = 0;
}
}
soundStructPointerArray[inBusNumber].sampleNumber = sampleNumber;
return noErr;
}
目标c功能:
- (void) stopAUGraph {
NSLog (@"Stopping audio processing graph");
Boolean isRunning = false;
OSStatus result = AUGraphIsRunning (processingGraph, &isRunning);
if (noErr != result) {[self printErrorMessage: @"AUGraphIsRunning" withStatus: result]; return;}
if (isRunning) {
result = AUGraphStop (processingGraph);
if (noErr != result) {[self printErrorMessage: @"AUGraphStop" withStatus: result]; return;}
self.playing = NO;
}
}
现在在
if (sampleNumber >= frameTotalForSound){
sampleNumber = 0;
}
回调函数部分如何调用stopAUGraph函数
答案 0 :(得分:2)
一个。对象有方法,没有函数。不调用方法,但将消息发送到执行方法的接收方。
B中。因此,您需要引用实例作为消息的接收者,这将导致执行该方法。有两种方法可以获得它:
B.A。通常,回调具有userInfo指针,该指针可以直接或在结构中保存引用。
B.B。如果您没有这样的信息,您可以询问单身人士或使用全局变量。这更糟糕。
℃。有了这样的接收器引用之后,如果它被编译为Objective-C代码(filename.m或Xcode中的设置),你可以在C函数中使用通常的消息语法:
void function (void *userInfo)
{
id reference = userInfo; // Using ARC you have to clarify the ownership with castings.
[reference message];
}
如果获得方法的实现指针或使用rte,也可以使用函数调用,例如objc_msgSend()。获取实现指针更糟糕,因为它关闭了多态机制。