If you are developing a game probably you need to add sound to your app.
As many other things, although Apple offers you a library to manage the audio, Cocos2D has it's own class: SimpleAudioEngine. It makes you really easy to insert background music and sound effects. It also allows you to modify basic properties such as pitch, pan and gain, and the possibility of looping a sound.
You can preload the sound (if you have a lot and heavy sounds). For loopings you need a CDSoundsource class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
soundEngine = [SimpleAudioEngine sharedEngine]; | |
[soundEngine preloadEffect:@"sound1.mp3"]; | |
[soundEngine preloadEffect:@"sound2.mp3"]; | |
[soundEngine preloadBackgroundMusic:@"backgroundMusic.mp3"]; | |
loopingSound = [[soundEngine soundSourceForFile:@"loopingSound.mp3"] retain]; | |
loopingSound.looping = YES; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[soundEngine playBackgroundMusic:@"backgroundMusic.mp3" loop:YES]; | |
[soundEngine playEffect:@"sound1.mp3"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[soundEngine setBackgroundMusicVolume:0.4]; | |
[soundEngine playEffect:@"sound1.mp3" pitch:1 pan:0 gain:0.4]; | |
[loopingSound setMute:YES]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[loopingSound play]; | |
[loopingSound pause]; | |
[loopingSound rewind]; | |
[loopingSound stop]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Never forget to release the looping sound in the dealloc method | |
[loopingSound release]; |