Quirk in Wire Whizz Audio Interruptions

OpenAL - the Good and the Bad

Well, I went with OpenAL in the end, as AVAudioPlayer objects seemed to stall Wire Whizz randomly when they fired up, [sound prepareToPlay] seemingly having no effect.

The GOOD then, is that all has been very well with OpenAL and the small amount of pain endured to add the interface (a few days part time), has meant that my audio works well, there’s no drop in frame rate, and I get 3D positional sounds for virtually no effort with the added bonus that headphone users will have a much more immersive audio experience. Of course I now have a workable audio system as well :)

The BAD however is the result of audio interruptions simply not working properly. I was calling alcMakeContextCurrent(NULL) during beginInterruption and then restoring my context during endInterruption. All should be fine, but after each call to OpenAL I was checking the OpenAL error with alGetError().  Sadly, when restoring my context I was getting OpenAL error 0xa004 (AL_INVALID_OPERATION).  Why though?  Why was this?

The Solution

The reality - I discovered after about 5 hours slogging away - was that alcMakeContextCurrent() was not blowing up at all and not responsible for the 0xa004 error code. What actually is causing this error code I don’t know, but the solution was simple: to call alGetError() during endInterruption, or at least, before trying to make the context current again. alGetError() clears any existing error, so for reasons I don’t understand, given that I check the error code every time I make an OpenAL call, my application’s audio interruption is ending and an error has been lodged with OpenAL.  Clearing it enables the context to be set without any error being returned and everything else continues to run smoothly after that.

I would be tempted to log a bug, but I reworked the aolTouch sample to use the AVAudioSession class instead of the C interface and added checks against alGetError() and I don’t see this.

Anyway, I do hope this helps some other poor iPhone developer who is in danger of scalping themselves.

 

Leave a Reply