I am currently learning to use Audio Queue Services in iOS. For this, I followed Audio Queue Services Programming Guide - Recording Audio. This works.
Now I would like to update a UI element (a power meter) when the callback function receives the input data from the microphone. This is where I run into a problem.
I am not sure what is a good way to call from the callback function into the view/view controller for updating the power meter. I tried this:
To the custom struct to manage state, which is described in the guide, I added a pointer to my view controller:
struct AQRecorderState {
AudioStreamBasicDescription mDataFormat; // 2
AudioQueueRef mQueue; // 3
AudioQueueBufferRef mBuffers[kNumberBuffers]; // 4
AudioFileID mAudioFile; // 5
UInt32 bufferByteSize; // 6
SInt64 mCurrentPacket; // 7
bool mIsRunning; // 8
void* mViewController; // <- I've added this line
};
I initialize this pointer when I start recording. This is done in a method of the view controller, which gets called when the start button receives a TouchUp event:
- (IBAction)startAudioRecording:(id)sender {
struct AQRecorderState* aqData = createAQRecorderState();
void* pViewControllerAsVoidPointer = (__bridge void*) self;
aqData->mViewController = pViewControllerAsVoidPointer;
//...
The method createAQRecorderState(), which allocates the struct, looks as follows:
struct AQRecorderState* createAQRecorderState() {
struct AQRecorderState* pAqData = (struct AQRecorderState*) malloc(sizeof(AQRecorderState));
initializeAQRecorderState(*pAqData); // sets members of the struct; does not set pointer to view controller
return pAqData;
}
AFAIK, the very instance of the struct that I create when starting recording gets delivered to later calls to the callback function. For this reason, I've added the following code to my callback function in order to then call to the view controller:
void HandleInputBuffer(
void *aqData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp *inStartTime,
UInt32 inNumPackets,
const AudioStreamPacketDescription *inPacketDesc
) {
//...
NSLog(@"interesting part start");
void* pViewControllerAsVoidPtr = pAqData->mViewController;
NSLog(@"localized pointer to void");
ViewController* pViewController = (__bridge ViewController*) pViewControllerAsVoidPtr; //EXC_BAD_ACCESS
NSLog(@"localized pointer to ViewController");
//...
}
However, I get a EXC_BAD_ACCESS (code=1, address=0xffffffff) in the callback function above in the marked line when I do the bridged pointer conversion. I've then checked this with the debugger and in fact pViewControllerAsVoidPtr
does not hold the address that I stored in pAqData->mViewController
during the execution of the method startAudioRecording:
.
Why does that pointer get changed? Or alternatively: How to correctly call from my callback function into the view controller for updating the UI?