Sounds and sounds in Cocos2d-X
There will be a variety of game sound when playing the game, such as starting the game there will be background sound, victory or failure there will be some sound, In the Cocos2d-X can use CocosDenshion for a variety of sounds
Before using CocosDenshion, you must add a header file and a namespace in the program.
#include "SimpleAudioEngine.h"using namespace CocosDenshion;
Then, add the music file to the Resource folder under the project directory.
Play background music
// Play background music // The first parameter: music file name // The second parameter: whether to play CocosDenshion cyclically: SimpleAudioEngine: sharedEngine ()-> playBackgroundMusic ("OnLand.wma ", true );
Stop playing background music
// Stop playing background music SimpleAudioEngine: sharedEngine ()-> stopBackgroundMusic ();
Playing Sound Effects
// Play sound effect // parameter: music file name // return value: audio number unsigned int effectID = CocosDenshion: SimpleAudioEngine: sharedEngine ()-> playEffect ("EatCoin.wma ");
Stop playing sound effects
// CocosDenshion: SimpleAudioEngine: sharedEngine ()-> stopEffect (effectID );
Set the volume of background music
// Set the volume of the background music. // The volume range is 0 ~ 1 CocosDenshion: SimpleAudioEngine: sharedEngine ()-> setBackgroundMusicVolume (. 5f );
Set the sound volume
// Set the sound volume. // The volume range is 0 ~ 1 CocosDenshion: SimpleAudioEngine: sharedEngine ()-> setjavastsvolume (. 5f );
Cache background music
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("OnLand.wma");
Accelerated cache sound effects
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("EatCoin.wma");
Zookeeper