views:

242

answers:

1

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).

A: 

I have the same problem, but more information, and thus questions for you. Are you using OpenAL for sound like me. Do you also support iPod music like me. I get headphone disconnects in quartz menu screens fine, but in the glView game screen it mostly fails like you. Sometimes the first disconnect works, then failure. Sometime the reconnect also fails to fire, so when I switch to quartz screen I get a que of a half dozen disconnect dialogs I have to click on. This is with OpenAL sound. With iPod music I switch to a NSNotificationCenter MPMusicPlayerControllerPlaybackStateDidChangeNotification notification and that works, but only while the iPod music is playing. OS 3.1.3 with a build to OS 3.1.0 on iPhone and iTouch. Look at your return error code from AudioSessionInitialize(), that was my problem, it was not being called first, and failed. EDIT: Fix failed, it is an intermittent/random issue, seemingly caused by high CPU load.

Brett
I'm not using OpenAL, just straight audio session/audio queue calls. Also, no iPod music! I log failures, and AudioSessionInitialize() is definitely indicating success. (It's also definitely being called before any other audio session/audio queue functions.
Frank Shearar