Skip to content

Instantly share code, notes, and snippets.

@sag333ar
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sag333ar/c1caeaaef0aa1ebfb5d7 to your computer and use it in GitHub Desktop.
Save sag333ar/c1caeaaef0aa1ebfb5d7 to your computer and use it in GitHub Desktop.
Load, play, pause, stop - sound-effects & background music in Cocos2d-x & also set the volume for both
// load sound effect
CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("audio/menu-click.caf");
// load background sound
CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("audio/landing.mp3");
// play backgorund sound
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("audio/landing.mp3");
// play sound effect
unsigned int a = CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("audio/menu-click.caf");
// stop all sound effects
CocosDenshion::SimpleAudioEngine::getInstance()->stopAllEffects();
// stop background music
CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic();
// stop specific sound effect
CocosDenshion::SimpleAudioEngine::getInstance()->stopEffect(a); // where 'a' is unsigned int variable - see line 11
// pause all sound effect
CocosDenshion::SimpleAudioEngine::getInstance()->pauseAllEffects();
// pause background music
CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
// pause specific background music
CocosDenshion::SimpleAudioEngine::getInstance()->pauseEffect(a); // where 'a' is unsigned int variable - see line 11
// get the 'sound effect' volume level (from 0 to 1)
float volume = CocosDenshion::SimpleAudioEngine::getInstance()->getEffectsVolume();
// set the 'sound effect' volume level (from 0 to 1) - here we set 0.5
CocosDenshion::SimpleAudioEngine::getInstance()->setEffectsVolume(0.5);
// get the 'background music' volume level (from 0 to 1)
float volume = CocosDenshion::SimpleAudioEngine::getInstance()->getBackgroundMusicVolume();
// set the 'background music' volume level (from 0 to 1) - here we set 0.5
CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment