After interruption, delayed audio route change notifications when recording

Posted by Frank Shearar on Stack Overflow See other posts from Stack Overflow or by Frank Shearar
Published on 2010-01-28T13:34:37Z Indexed on 2010/03/08 6:51 UTC
Read the original article Hit count: 293

My iPhone application requires that I know when a user has/has not plugged in her headphones. That's easy. AudioSessionAddPropertyListener with a callback listening to kAudioSessionProperty_AudioRouteChange.

I write logs with NSLog as things happen. User plugs the headphones in? Get a notification, and a line in the gdb console. User unplugs the headphones? Ditto.

At the same time I'm sensing the noise level of the environment by starting a recording audio queue. This, too, works great: I can get the mic noise level and listen for audio route changes just fine.

What I find is that after an interruption, and I've reactivated the audio session and restored the audio category to kAudioSessionCategory_RecordAudio, the audio route notifications go a bit haywire.

When I plug in the headphones, I see no notification. When I unplug the headphones I see BOTH the "plugged in" notification AND the "unplugged" notification, in rapid succession.

It's like the "plugged in" notification's delayed and, when the "unplugged" notification arrives, the queue of pending notifications is flushed.

What am I doing wrong? How do I correctly restore the audio session to get timeous notifications?

EDIT: iPhone OS 3.1.2, running on an iPhone 3G. I'm running a program compiled with the 3.0 SDK (from within XCode 3.1.2).

© Stack Overflow or respective owner

Related posts about iphone

Related posts about audioqueueservices