How to do iPhone sound the easy way

There are probably too many choices for sound APIs on the iphone.  If you just want to “play some sounds”, they break down like this, pretty much in the order of complexity:

  • System Sound Services – a basic API meant to play short sounds, with a simple C API
  • AVAudioPlayer class in the AVFoundation framework- Easy to use, can play a wide variety for formats, and has methods like “play” and “stop” that pretty much do what you expect.
  • The Audio Toolbox framework – consider this an advanced version of AVFoundation
  • OpenAL – If you want control and performance like you need for a game, most people will tell you this is where you wan to be.
  • Audio Unit framework – low level access to the audio hardware.  If you are surfing the internet to figure out how to play a sound you don’t want to be here!

The first two options are the simplest path to getting the darn thing to play a sound.  I know tried both of these first to understand the trade-offs.  And judging from a lot of free and indie games out there, a lot of developers stop right here too.

The most common problem customers will see with sound one of these simple approaches is with the volume control.  If you use one of the simple APIs, you’ll notice that your app’s volume is tied to the ringer volume.  You can see this if you fire your app up and when you hit one of the volume buttons on the side you see something like this:

People who buy your app will find it really annoying if they turn down the sound in your game only to also turn down their ringer too.  When you have it setup correctly, you should see the general volume control that doesn’t say “ringer” on it:

To really fix this and take control over the sound, you will want to dive into OpenAL.  But who has the time?  If you have simple audio needs but still want to put out a polished product, there’s a better way.  Like most hard problems in programming, someone who actually does have the time did all the hard work for you.

Cocos2D is a fairly popular 2D game engine for the iPhone.  But even if you aren’t writing a game or don’t want to use the whole game framework, it has a very easy to use sound engine that can be used completely independently of the rest of the framework called CocosDenshion.  You can use the SimpleAudioEngine class in your app either by linking to Cocos2D or even by directly compiling only about 10 source files into your app.

SimpleAudioEngine lives up to it’s name.  It provides good performance, is easy to use, solves the volume-control problem, and generally just-works.  It’s a shame Apple couldn’t provide an API like that.

There isn’t an explicit initialization for SimpleAudioEngine, but you may want to create an instance in your applicationDidFinishLaunching method and just discard the returned pointer.  Don’t worry, you won’t leak since this a singleton instance that you will use for all of your audio.  This way you take the initialization hit up front instead of an annoying pause the first time you go to play a sound.  This will also make the volume controls work the way you want from the start.

Likewise, you will want to call preLoadEffect for the sounds you will likely need to cache them for quick playing.   Simply pass the file name of the sound file to load it up.

Playing a sound is just as easy:  call playEffect with the sound file name.  That’s it!  Easy, efficient, professional sound integration.

Download Cocos2D here.

Tags: , ,

Comments are closed.