Search code examples
objective-ccocoacore-audioaudioqueuecfrunloop

How to make AudioQueue play without freezing GUI?


I just began learning about AudioQueues from the CoreAudio book (rough cuts). I did the AudioQueue playback example tutorial, which is basically the same as the apple tutorial example. Everything is working fine.

The problems start when I try to implement the code in an app with a GUI. I tested it by pasting the code into the 'init' method of a NSObject subclass. The only way I can get the queue to do the callback is by inserting an empty DO...WHILE loop in the end of my init, but that makes the GUI freeze (obviously...)!!

Apparently the AudioQueue is supposed to be run in its own separate thread automatically as long as AudioQueueNewOutput is passed NULL for inCallbackRunLoop and CallbackRunLoopMode arguments. That's just not happening. I am only hearing the 1.5 seconds from the priming of the buffers.

Clearly, there is something fundamental that I don't understand about how things work...

Kasper

-(void) start
{
CheckError(
     AudioQueueStart(queue, NULL),
 "AudioQueueStart failed");

printf("Playing...\n");
do {
} while (0 == 0);            //WHY IS THIS MAKING IT PLAY???
}

Solution

  • Turns out that my userData struct wasn't declared as a ivar in the header file. Rookie mistake...