Plug-And-Play OpenAL / AVFoundation Sound Engine For iPhone Apps

I recently found a great sound engine for iPhone applications that is pretty much plug-and-play.

openal Plug And Play OpenAL / AVFoundation Sound Engine For iPhone Apps

OpenAL lets you play multiple sounds at once

This sound engine was written by Alex Restrepo under a creative commons attribution-share alike licence, so you are free to use it so long as you attribute the code to the author and if you change it and share it you must do so under the same license. I have corrected one small mistake (a semi-colon where it should not have been) and also edited the formatting of the code to my personal preference.

You can get an Xcode example project that uses the sound engine here. If you just want the sound engine itself you can get it here.

If you look at the example project you will see how easy this sound engine is to use. It provides simple methods for playing and managing background music and for playing sound effects on top. These classes use AVFoundation to play the background music and OpenAL to play other sounds on top. I am not informed enough about either of these to know what advantages or disadvantages this may offer but will look into it and possibly do a pure OpenAL version.

To add the sound engine to your own project just add the class files included in to your project. Also be sure to add the OpenAL.framework, AVFoundation.framework and the AudioToolbox.framework otherwise you will get a host of strange errors.

You have the option of using the CMOpenALSoundManager as a singleton. If you do use it as a singleton (you can do this by setting USE_AS_SINGLETON to 1 at the top of CMOpenALSoundManager.h) you just need to initialize it somewhere in your code with:

[[CMOpenALSoundManager sharedCMOpenALSoundManager] init];

You also need to enumerate the tags for your sounds (ideally somewhere that every class that needs them will have access to them):


You can then add your sound effects by passing an array of sound file names. Assuming you have an array named mySoundFileNamesArray you would do this:

[CMOpenALSoundManager sharedCMOpenALSoundManager].soundFileNames = mySoundFileNamesArray;

To access your sound manager from anywhere in your project make sure you have included the CMOpenSoundManager header file and then reference the sound manager with:

[CMOpenALSoundManager sharedCMOpenALSoundManager]

Here is an example of playing some background music and then playing a sound effect:

[[CMOpenALSoundManager sharedCMOpenALSoundManager] playBackgroundMusic:@"backgroundLoop.m4a"]; [[CMOpenALSoundManager sharedCMOpenALSoundManager] playSoundWithID:BUTTONSOUND1];

One thing that I am not satisfied about with this system is the need for the enumeration. The reason being that you need to know which order the sounds are going to be loaded so that the correct sounds match the correct enum value. A better way in my opinion would be to use NSString keys, so that when you load a sound, you also pass a key. This is the approach taken in the code on the blog, which is another great resource for OpenAL on the iPhone. Doing it this way allows you to lazy load sounds in any order. If I make that change I will be sure to post it

Related posts