4

I am analyzing incoming audio on an iPhone/iPod Touch using the Audio Queue Services. The desired behavior of the analyzer boils down to this: when the App becomes active, start analyzing; when the App is sent to the background, stop analyzing.

I am using a straight forward approach, using the AppDelegate to start and stop the analyzer whenever the App state changes.

Here's some code:

- (void)applicationWillResignActive:(UIApplication *)application {

    AudioQueueFlush(_aqRef);
    AudioQueueStop(_aqRef, true);
    AudioQueueDispose(_aqRef, true);
}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    AudioQueueNewInput(&_formatDescription, _renderCallback, NULL, NULL, NULL, 0, &_aqRef);
    AudioQueueAllocateBuffer(_aqRef, _aqBufferSize, &_aqBufferRef);
    AudioQueueEnqueueBuffer(_aqRef, _aqBufferRef, 0, NULL);
    AudioQueueStart(_aqRef, NULL);
}

All variable are initialized and are working – take that as given and tested. The queue and the buffers shouldn't be disposed and allocated each time, I know, but it doesn't make a difference for now and it's more complete this way.

Here is what happens:

Step 1) Launch the App, the audio queue starts, Bob's your uncle.

Step 2) Push the power button and the App is suspended. You don't hear the locking-sound though, because the audio queue still seems to block the system sounds. However, sometimes it does appear, mostly after the first launch of the App, but that is not reliable. First glitch.

Step 3) Push the home button and unlock the phone. Again, you won't hear the system sound of unlocking the phone because the audio queue has already taken over. You may hear a short bit of the unlocking-sound, though. Sometimes. Not reliable either. Second glitch.

Step 4) Push the home button and send the App into the background. About a second after the App has disappeared, the system remembers that it couldn't play the unlocking sound in Step 3 and plays it NOW. At full volume. That's really bad.

This leads me to two questions:

Question 1): When the power button gets pushed, I don't see any other way to stop the audio queue any earlier than in applicationWillResignActive:. Anything I am missing here? I am ok with the locking-sound missing, but I am happy to learn about any other way to make it sound (or not sound) reliably.

Question 2): The delayed unlocking sound in Step 4 is giving me a headache. An easy way to prevent this, would be to delay the start of the audio queue so the unlocking sound can be played unhindered. But that seems like a hack and if anybody can point me in a better direction I would be a much more happy coder.

Thanks /Andreas

Andi Wagner
  • 310
  • 2
  • 9

0 Answers0