Cocos2d SimpleAudioEngine Example

Sound effect always is very important part of Game, I used to use other library that playing sound effect on iPhone App, but it totally make the size of the game bigger and slower. Now, I have move my game design from Cocoa To Cocos2d and it's include a few audio playing library. since I just need a background music and some sound effect, so I have choose SimpleAudioEngine.

Steps for playing background music
1. add your sound effect file to project (e.g. BG.mp3)
2. add header file to AppDelegate
#import "SimpleAudioEngine.h"
3. make sure you have add "SimpleAudioEngine.h" and "SimpleAudioEngine.m" from cocos2d

because I want to play the backgrund music after the app have finish loading. so I have make following change for applicationDidFinishLaunching

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"BG.mp3"];

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
...
}

and Now, if we want to play a sound effect when user tap the button, we should preload the sound effect file on app delegate first, or any function before you play it.
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"BG.mp3"];
[[SimpleAudioEngine sharedEngine] preloadEffect:@"tapBtn.mp3"];\

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
...
}

and play it when you need it

-(void)tapButton:(id)sender
{
[audioEngine playEffect:@"tapBtn.mp3"];
}