質問

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?

役に立ちましたか?

解決

I found my mistake. It's in how I do the step Creating a Recording Audio Queue. I do it exactly as copy-pasted from the guide:

AudioQueueNewInput (                              // 1
    &aqData.mDataFormat,                          // 2
    HandleInputBuffer,                            // 3
    &aqData, //<------------------ HERE           // 4
    NULL,                                         // 5
    kCFRunLoopCommonModes,                        // 6
    0,                                            // 7
    &aqData.mQueue                                // 8
);

The problem was that in the guide, the variable aqData is a struct and therefor needs to be referenced (leading & sign) to convert it to a pointer, whereas my variable aqData already is a pointer to a struct and therefor the referencing (leading & sign) changes it to a "pointer to pointer to struct". Hence I passed the address of the pointer instead of the address of the struct to the function AudioQueueNewInput(...), which is wrong, and of course the memory at this other location has different content.

The compiler does not complain about this wrong type, because it expects a (void*), which can be any pointer, including (struct**), a pointer to a pointer to a struct.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top