Playing sounds and music

Most games use a variety of sound effects and at most a few different background music tracks. CocosDenshion is the audio library built into Cocos2d. It provides a number of features including the SimpleAudioEngine API. In this recipe, we will use this API to play sounds and music.

Playing sounds and music

Getting ready

Please refer to the project RecipeCollection02 for full working code of this recipe.

How to do it...

Execute the following code:

#import "SimpleAudioEngine.h" @implementation Ch6_SoundsAndMusic -(CCLayer*) runRecipe { //Initialize the audio engine sae = [SimpleAudioEngine sharedEngine]; //Background music is stopped on resign and resumed ...

Get Cocos2d for iPhone 1 Game Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.