{"_id":"547b1d074366a708001b3b61","category":{"_id":"5463e333447e0a100024a08b","project":"5463e333447e0a100024a087","pages":["5463e334447e0a100024a08d","54646a285871e90800f50174","54667e7af2b6390800dfd82a","54679c8c98b6840800268982","54679d8498b6840800268987","546a2ed0394c861a00c3a413","546b6a4862515a14007ebbba","547b1c2e7eec0f080099e1dd","55d33d90b9ef4435006dec70","56570c764d1c720d001f51bb"],"version":"5463e333447e0a100024a08a","__v":10,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-11-12T22:46:11.951Z","from_sync":false,"order":1,"slug":"documentation","title":"Documentation"},"version":{"_id":"5463e333447e0a100024a08a","project":"5463e333447e0a100024a087","__v":6,"createdAt":"2014-11-12T22:46:11.486Z","releaseDate":"2014-11-12T22:46:11.486Z","categories":["5463e333447e0a100024a08b","54645c365871e90800f50151","547b1c5a4366a708001b3b50","547b1cdf4366a708001b3b5a","54e3bef8464a9c3700f7ca67","54ec651d7ab3172d00c01ab7"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"__v":11,"project":"5463e333447e0a100024a087","user":"54639d06447e0a1000249f7f","updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-11-30T13:35:03.160Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"try":true,"basic_auth":false,"auth":"never","params":[],"url":""},"isReference":false,"order":15,"body":"##**Setup Audio** - to use audio in Cocos it needs to be setup first\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// include this in every file that uses audio\\n#include \\\"SimpleAudioEngine.h\\\"\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++\"\n    }\n  ]\n}\n[/block]\nNote: *Cocos2d-JS doesn't require any setup for audio playback*\n\n##**Sound Effects** - sound effects are good for playing small files such as an explosion\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// load the sound file into memory, this is optional but recommended if you are using the audio file several times. Useful on a loading screen for example\\nCocosDenshion::SimpleAudioEngine::getInstance( )->preloadEffect( \\\"audioFilePath\\\" );\\n\\n// unload the sound file from memory when it no longer needs to be used\\nCocosDenshion::SimpleAudioEngine::getInstance( )->unloadEffect ( \\\"audioFilePath\\\" );\\n\\n// the second parameter states whether or not the sound effect is played repeatedly, it is an optional parameter\\nCocosDenshion::SimpleAudioEngine::getInstance( )->playEffect( \\\"audioFilePath\\\", true );\\n\\n// stops a particular sound effect\\nCocosDenshion::SimpleAudioEngine::getInstance( )->stopEffect( soundEffectID );\\n\\n// pauses a particular sound effect\\nCocosDenshion::SimpleAudioEngine::getInstance( )->pauseEffect( soundEffectID );\\n\\n// resumes a particular sound effect\\nCocosDenshion::SimpleAudioEngine::getInstance( )->resumeEffect( soundEffectID );\\n\\n// stops all sound effects\\nCocosDenshion::SimpleAudioEngine::getInstance( )->stopAllEffect( soundEffectID );\\n\\n// pauses all sound effects\\nCocosDenshion::SimpleAudioEngine::getInstance( )->pauseAllEffect( soundEffectID );\\n\\n// resumes all sound effects\\nCocosDenshion::SimpleAudioEngine::getInstance( )->resumeAllEffect( soundEffectID );\\n\\n// set the volume of all sound effects (ranges between 0 and 1)\\nCocosDenshion::SimpleAudioEngine::getInstance( )->setEffectsVolume( 0.25 );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++\"\n    },\n    {\n      \"code\": \"// unload the sound file from memory when it no longer needs to be used\\ncc.audioEngine.playEffect( \\\"audioFilePath\\\" );\\n\\n// the second parameter states whether or not the sound effect is played repeatedly, it is an optional parameter\\ncc.audioEngine.playEffect( \\\"audioFilePath\\\" );\\n\\n// stops a particular sound effect\\ncc.audioEngine.stopEffect->stopEffect( soundEffectID );\\n\\n// pauses a particular sound effect\\ncc.audioEngine.pauseEffect( soundEffectID );\\n\\n// resumes a particular sound effect\\ncc.audioEngine.resumeEffect( soundEffectID );\\n\\n// stops all sound effects\\ncc.audioEngine.stopAllEffect( soundEffectID );\\n\\n// pauses all sound effects\\ncc.audioEngine.pauseAllEffect( soundEffectID );\\n\\n// resumes all sound effects\\ncc.audioEngine.resumeAllEffect( soundEffectID );\\n\\n// set the volume of all sound effects (ranges between 0 and 1)\\ncc.audioEngine.setEffectsVolume( 0.25 );\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\nNote: *The **soundEffectID** is the value that is returned when using the **playEffect** method*\n\n##**Music** - play background music using an audio file\n\nNote: *Only a single music file can be played at any given time*\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// load the music file into memory, this is optional but recommended if you are using the audio file several times. Useful on a loading screen for example\\nCocosDenshion::SimpleAudioEngine::getInstance( )->preloadBackgroundMusic( \\\"audioFilePath\\\" );\\n\\n// the second parameter states whether or not the music file is played repeatedly, it is an optional parameter\\nCocosDenshion::SimpleAudioEngine::getInstance( )->playBackgroundMusic( \\\"audioFilePath\\\", true );\\n\\n// stops the background music\\nCocosDenshion::SimpleAudioEngine::getInstance( )->stopBackgroundMusic( );\\n\\n// pause the background music\\nCocosDenshion::SimpleAudioEngine::getInstance( )->pauseBackgroundMusic( );\\n\\n// resume the background music\\nCocosDenshion::SimpleAudioEngine::getInstance( )->resumeBackgroundMusic( );\\n\\n// set the volume of the background music (ranges between 0 and 1)\\nCocosDenshion::SimpleAudioEngine::getInstance( )->setBackgroundMusicVolume( 0.25 );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++\"\n    },\n    {\n      \"code\": \"// the second parameter states whether or not the music file is played repeatedly, it is an optional parameter\\ncc.audioEngine.playMusic( \\\"audioFilePath\\\", true );\\n\\n// stops the background music\\ncc.audioEngine.stopMusic( );\\n\\n// pause the background music\\ncc.audioEngine.pauseMusic( );\\n\\n// resume the background music\\ncc.audioEngine.resumeMusic( );\\n\\n// set the volume of the background music (ranges between 0 and 1)\\ncc.audioEngine.setMusicVolume( ( 0.25 );\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\nTo pause and resume music when changing apps open the **AppDelegate.cpp** file and modify these functions:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// This function will be called when the app is inactive. When comes a phone call,it's be invoked too\\nvoid AppDelegate::applicationDidEnterBackground() {\\n    Director::getInstance()->stopAnimation();\\n\\n    // if you use SimpleAudioEngine, it must be pause\\n    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();\\n}\\n\\n// this function will be called when the app is active again\\nvoid AppDelegate::applicationWillEnterForeground() {\\n    Director::getInstance()->startAnimation();\\n\\n    // if you use SimpleAudioEngine, it must resume here\\n    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Before\"\n    },\n    {\n      \"code\": \"// include this at the top of the time\\n#include \\\"SimpleAudioEngine.h\\\"\\n  \\n// This function will be called when the app is inactive. When comes a phone call,it's be invoked too\\nvoid AppDelegate::applicationDidEnterBackground() {\\n    Director::getInstance()->stopAnimation();\\n\\n    // if you use SimpleAudioEngine, it must be pause\\n    CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic();\\n}\\n\\n// this function will be called when the app is active again\\nvoid AppDelegate::applicationWillEnterForeground() {\\n    Director::getInstance()->startAnimation();\\n\\n    // if you use SimpleAudioEngine, it must resume here\\n   \\tCocosDenshion::SimpleAudioEngine::getInstance()->resumeBackgroundMusic();\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"After\"\n    }\n  ]\n}\n[/block]","excerpt":"This page will cover how to implement audio in an application using Cocos","slug":"audio","type":"basic","title":"Audio"}

Audio

This page will cover how to implement audio in an application using Cocos

##**Setup Audio** - to use audio in Cocos it needs to be setup first [block:code] { "codes": [ { "code": "// include this in every file that uses audio\n#include \"SimpleAudioEngine.h\"", "language": "cplusplus", "name": "Cocos2d-x C++" } ] } [/block] Note: *Cocos2d-JS doesn't require any setup for audio playback* ##**Sound Effects** - sound effects are good for playing small files such as an explosion [block:code] { "codes": [ { "code": "// load the sound file into memory, this is optional but recommended if you are using the audio file several times. Useful on a loading screen for example\nCocosDenshion::SimpleAudioEngine::getInstance( )->preloadEffect( \"audioFilePath\" );\n\n// unload the sound file from memory when it no longer needs to be used\nCocosDenshion::SimpleAudioEngine::getInstance( )->unloadEffect ( \"audioFilePath\" );\n\n// the second parameter states whether or not the sound effect is played repeatedly, it is an optional parameter\nCocosDenshion::SimpleAudioEngine::getInstance( )->playEffect( \"audioFilePath\", true );\n\n// stops a particular sound effect\nCocosDenshion::SimpleAudioEngine::getInstance( )->stopEffect( soundEffectID );\n\n// pauses a particular sound effect\nCocosDenshion::SimpleAudioEngine::getInstance( )->pauseEffect( soundEffectID );\n\n// resumes a particular sound effect\nCocosDenshion::SimpleAudioEngine::getInstance( )->resumeEffect( soundEffectID );\n\n// stops all sound effects\nCocosDenshion::SimpleAudioEngine::getInstance( )->stopAllEffect( soundEffectID );\n\n// pauses all sound effects\nCocosDenshion::SimpleAudioEngine::getInstance( )->pauseAllEffect( soundEffectID );\n\n// resumes all sound effects\nCocosDenshion::SimpleAudioEngine::getInstance( )->resumeAllEffect( soundEffectID );\n\n// set the volume of all sound effects (ranges between 0 and 1)\nCocosDenshion::SimpleAudioEngine::getInstance( )->setEffectsVolume( 0.25 );", "language": "cplusplus", "name": "Cocos2d-x C++" }, { "code": "// unload the sound file from memory when it no longer needs to be used\ncc.audioEngine.playEffect( \"audioFilePath\" );\n\n// the second parameter states whether or not the sound effect is played repeatedly, it is an optional parameter\ncc.audioEngine.playEffect( \"audioFilePath\" );\n\n// stops a particular sound effect\ncc.audioEngine.stopEffect->stopEffect( soundEffectID );\n\n// pauses a particular sound effect\ncc.audioEngine.pauseEffect( soundEffectID );\n\n// resumes a particular sound effect\ncc.audioEngine.resumeEffect( soundEffectID );\n\n// stops all sound effects\ncc.audioEngine.stopAllEffect( soundEffectID );\n\n// pauses all sound effects\ncc.audioEngine.pauseAllEffect( soundEffectID );\n\n// resumes all sound effects\ncc.audioEngine.resumeAllEffect( soundEffectID );\n\n// set the volume of all sound effects (ranges between 0 and 1)\ncc.audioEngine.setEffectsVolume( 0.25 );", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] Note: *The **soundEffectID** is the value that is returned when using the **playEffect** method* ##**Music** - play background music using an audio file Note: *Only a single music file can be played at any given time* [block:code] { "codes": [ { "code": "// load the music file into memory, this is optional but recommended if you are using the audio file several times. Useful on a loading screen for example\nCocosDenshion::SimpleAudioEngine::getInstance( )->preloadBackgroundMusic( \"audioFilePath\" );\n\n// the second parameter states whether or not the music file is played repeatedly, it is an optional parameter\nCocosDenshion::SimpleAudioEngine::getInstance( )->playBackgroundMusic( \"audioFilePath\", true );\n\n// stops the background music\nCocosDenshion::SimpleAudioEngine::getInstance( )->stopBackgroundMusic( );\n\n// pause the background music\nCocosDenshion::SimpleAudioEngine::getInstance( )->pauseBackgroundMusic( );\n\n// resume the background music\nCocosDenshion::SimpleAudioEngine::getInstance( )->resumeBackgroundMusic( );\n\n// set the volume of the background music (ranges between 0 and 1)\nCocosDenshion::SimpleAudioEngine::getInstance( )->setBackgroundMusicVolume( 0.25 );", "language": "cplusplus", "name": "Cocos2d-x C++" }, { "code": "// the second parameter states whether or not the music file is played repeatedly, it is an optional parameter\ncc.audioEngine.playMusic( \"audioFilePath\", true );\n\n// stops the background music\ncc.audioEngine.stopMusic( );\n\n// pause the background music\ncc.audioEngine.pauseMusic( );\n\n// resume the background music\ncc.audioEngine.resumeMusic( );\n\n// set the volume of the background music (ranges between 0 and 1)\ncc.audioEngine.setMusicVolume( ( 0.25 );", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] To pause and resume music when changing apps open the **AppDelegate.cpp** file and modify these functions: [block:code] { "codes": [ { "code": "// This function will be called when the app is inactive. When comes a phone call,it's be invoked too\nvoid AppDelegate::applicationDidEnterBackground() {\n Director::getInstance()->stopAnimation();\n\n // if you use SimpleAudioEngine, it must be pause\n // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();\n}\n\n// this function will be called when the app is active again\nvoid AppDelegate::applicationWillEnterForeground() {\n Director::getInstance()->startAnimation();\n\n // if you use SimpleAudioEngine, it must resume here\n // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();\n}", "language": "cplusplus", "name": "Before" }, { "code": "// include this at the top of the time\n#include \"SimpleAudioEngine.h\"\n \n// This function will be called when the app is inactive. When comes a phone call,it's be invoked too\nvoid AppDelegate::applicationDidEnterBackground() {\n Director::getInstance()->stopAnimation();\n\n // if you use SimpleAudioEngine, it must be pause\n CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic();\n}\n\n// this function will be called when the app is active again\nvoid AppDelegate::applicationWillEnterForeground() {\n Director::getInstance()->startAnimation();\n\n // if you use SimpleAudioEngine, it must resume here\n \tCocosDenshion::SimpleAudioEngine::getInstance()->resumeBackgroundMusic();\n}", "language": "cplusplus", "name": "After" } ] } [/block]