Cocos2D offers CocosDenshion, an easy to use framework to work with audio.
CocosDenshion allows you to play a background music and then some effects on top of that. This is clearly aimed at games and it usually works really well. The difference between background music and effects is summarized as follows:
- background music is thought of as potentially of long duration, so it is handled as a long audio stream;
- background music is thought of as continuous, so it is played in exclusive mode;
- effects are thought of as smaller in size, so they are loaded into memory entirely;
- effects are by their definition itself aimed at being played many times, so they are cached into memory for reuse;
- effect can be mixed to background music, so they do not “steal” the audio subsystem.
This works well until your effects are pretty small in size, otherwise your memory requirements will quickly grow. On iOS this is a no-no, since your app has very little memory to run with. Another case when CocosDenshion falls short is when you want your effects to be played continuously in a loop.
Say, for example, that you have a main background theme that is played in a loop, and then a secondary theme that you would like to be also played in a loop when your character enters some given state.
Suche scenario led me to create a small category on CocosDenshion SimpleAudioManager which defines four methods:
-(void) playForegroundMusic:(NSString*)filePath loop:(BOOL)loop;
playForegroundMusic will simply play your secondary theme on top of your background music without claiming exclusive access to the audio subsystem. You can find it on my github together with the rest of my Cocos2D snippets.