This is a really common problem. self does not work here because it is not a method of the AudioRecorder class, and not because it has Objective-C code. You are in an Objective-C ++ file, so all valid Objective-C code will work. [anAudioRecorder playAlarmSound] will work fine if you have a good link to anAudioRecorder .
So, how do we get the link if we do not have access to self ? The usual way is to use the void* aqData this function as a pointer to your AudioRecorder object. When you registered this callback, you told him that the void* argument would, in this case, be a pointer to your object or AQRecorderState structure, which seems not to be used yet. Instead, you can use a pointer to self when registering so you can use this object here.
Another option would be to use a shared AudioRecorder object, in which case you call something like [AudioRecorder sharedInstance] (a class method, not an instance) to get the desired AudioRecorder object. Since another answer here details the first method, how to use the shared instance parameter: add a static instance of AudioRecorder and a method of the sharedInstance class to your AudioRecorder object, for example:
static AudioRecorder* sharedMyInstance = nil; + (id) sharedInstance { @synchronized(self) { if( sharedMyInstance == nil ) sharedMyInstance = [[super allocWithZone:NULL] init]; } return sharedMyInstance; }
Then, when you want to use AudioRecorder from your callback, you can get the shared instance using [AudioRecorder sharedInstance] . This is a very useful paradigm if there is only one AudioRecorder - it removes many links.
Luke
source share