Calling a Objective C function from C++ Code

前端 未结 2 1206
野趣味
野趣味 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

    This is indeed a common problem. self doesn't work here because this is not a method of the AudioRecorder class, not because it's Objective-C code. You're in an Objective-C++ file, so all valid Objective-C code will work. [anAudioRecorder playAlarmSound] will work fine, provided you have a good reference to anAudioRecorder.

    So how do we get a reference if we don't have access to self? The usual way is to use the void* aqData argument of this function as a pointer to your AudioRecorder object. When you registered this callback, you told it what the void* argument would be, in this case a pointer to your AQRecorderState object or struct, which you don't seem to use anyway. Instead you can use a pointer to self when you register so that you can use that object here.

    Another option would be to use a shared AudioRecorder object, in which case you would call something like [AudioRecorder sharedInstance] (a class, not an instance, method) to get the AudioRecorder object you want. Because the other answer here elaborates on the first method, here's how to use the shared instance option: Add a static instance of AudioRecorder and a class method sharedInstance to your AudioRecorder object, like this:

    static AudioRecorder* sharedMyInstance = nil;
    
    + (id) sharedInstance {
        @synchronized(self) {
            if( sharedMyInstance == nil )
                sharedMyInstance = [[super allocWithZone:NULL] init];
        }
        return sharedMyInstance;
    } // end sharedInstance()
    

    Then, when you want to use the AudioRecorder from your callback, you can get the shared instance using [AudioRecorder sharedInstance]. This is a very useful paradigm if there's only going to be one AudioRecorder - it eliminates a lot of reference passing.

提交回复
热议问题