Audio Services Freezing App

If you are using Audio Services to play sound effects, then you’re going to have problems if you’re also updating your program state at a high frame rate. 

Although incredibly simple to use, the Audio Services should really only be used for one off alerts or sounds, and never as the sound effect backend of a game or similar. 

Like most Apple APIs, they use the technique of lazy loading.  That is, don’t bother doing anything like setting up audio buffers etc until you actually call the API into action for the first time.  This is useful for faster loading apps, and a smaller average memory footprint. 

But the downside to this is that the first time you try to play a sound, the Audio Services will be initialized and your app will pause for tenths of a second.  After this, however, all will be fine. 

A better way to go is to use OpenAL to handle audio playback.  It is much more flexible, and allows you to call the initialize function when your application loads, to avoid delays when you actually want sound to be played.  

The best place to start with this is to look at the Apple ‘Crash Landing’ example.  It includes a ‘SoundEngine’ module that can pretty much just be copied into your applications for use.  Remember to include the OpenAL framework though.

2 Responses to “Audio Services Freezing App”

  1. John says:

    Hi,

    I’ve also been seeing this freezing issue while using Audio Services. Whether I do the implementation with AVAudioPlayer, or more directly in the manner of the SpeakHere example. One difference from what you’ve described, though, is that I get the pause every time I play the sound. Not just the initial time. It’s beginning to look like I should abandon any hope of getting Audio Services to work for a high frame rate OpenGL game. This is pretty frustrating, because Apple’s docs make it seem like Audio Services is the way to go for most iPhone audio stuff.

    I’d like to use OpenAL, but it appears the CrashLanding example has been deprecated and removed from Apple’s dev center, because the SoundEngine stuff is apparently no longer considered production ready. Do you know of any other OpenAL iPhone example code out there? I do still have the CrashLanding example locally, but I’m not sure I want to use it now.

    Thanks.

  2. John says:

    Just found this. I haven’t had a chance to try it out yet (day job), but I’m hoping to check it out tonight. Maybe you’ll find it useful.

    http://developer.apple.com/iphone/library/samplecode/oalTouch/