Dieses Dokuwiki verwendet ein von Anymorphic Webdesign erstelltes Thema.

CocosDenshion Cookbook

Some code “recipes” for working with CocosDenshion


One nice thing about SimpleAudioEngine is that it requires minimal set up. It makes intelligent choices for you and uses lazy initialization which greatly simplifies adding sound to your project. Once you have CocosDenshion added to your project as outlined in the FAQ then simply import “SimpleAudioEngine.h” in files that require sound access and then you can use code like the following for playing sound effects and music.

Play a sound

Play the sound stored in the file mysound.wav.

[[SimpleAudioEngine sharedEngine] playEffect:@"mysound.wav"];//play a sound

Play some background music

Play an mp3 file named somemusic.mp3 in an endless loop.

[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"somemusic.mp3"];//play background music

Pause the background music

Pause the background music.

[[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];//pause background music

Play background music from the start again

Just call playBackgroundMusic again. If the file matches the currently playing music then the background music will be restarted from the beginning.

[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"somemusic.mp3"];//play background music

Preload the background music

Use preloadBackgroundMusic to reduce start up time when you call playBackgroundMusic. You can also do this to ensure there is a background music object to set properties against (such as volume) before you call playBackgroundMusic.

SimpleAudioEngine *sae = [SimpleAudioEngine sharedEngine];
if (sae != nil) {
    [sae preloadBackgroundMusic:@"mula_tito_on_timbales.mp3"];
    if (sae.willPlayBackgroundMusic) {
        sae.backgroundMusicVolume = 0.5f;


Set background music volume

Set the volume property of the backgroundMusic AVAudioPlayer object directly. Range is 0.0 for off to 1.0 for maximum volume. Note this will only have an effect if there is a backgroundMusic object so you must have previously called playBackgroundMusic or preloadBackgroundMusic. Also note that volume is a property of each individual AVAudioPlayer therefore if you change the background music the volume property will not persist, it needs to be set for the new AVAudioPlayer/backgroundMusic object.

[CDAudioManager sharedManager].backgroundMusic.volume = 1.0f;

Get notified when the background music stops

Register a method/selector to get notified when background music stops. Note that music that is set up to loop repeatedly will never complete, you do not get a notification for each loop.

[[CDAudioManager sharedManager] setBackgroundMusicCompletionListener:self selector:@selector(backgroundMusicFinished)]; 

Loop my background music a specific number of times

Start the background music, it doesn't really matter whether you specify yes or no for looping. Then set the numberOfLoops property on the backgroundMusic AVAudioPlayer object directly.

[[CDAudioManager sharedManager] playBackgroundMusic:@"mula_tito_on_timbales.mp3" loop:YES];
[CDAudioManager sharedManager].backgroundMusic.numberOfLoops = 3;//To loop 3 times
cocosdenshion/cookbook.txt · Last modified: 2009/11/20 08:19 by oneline
Trace: cookbook
Dieses Dokuwiki verwendet ein von Anymorphic Webdesign erstelltes Thema.
CC Attribution-Noncommercial-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0