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

// include this in every file that uses audio
#include "SimpleAudioEngine.h"

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

// 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
CocosDenshion::SimpleAudioEngine::getInstance( )->preloadEffect( "audioFilePath" );

// unload the sound file from memory when it no longer needs to be used
CocosDenshion::SimpleAudioEngine::getInstance( )->unloadEffect ( "audioFilePath" );

// the second parameter states whether or not the sound effect is played repeatedly, it is an optional parameter
CocosDenshion::SimpleAudioEngine::getInstance( )->playEffect( "audioFilePath", true );

// stops a particular sound effect
CocosDenshion::SimpleAudioEngine::getInstance( )->stopEffect( soundEffectID );

// pauses a particular sound effect
CocosDenshion::SimpleAudioEngine::getInstance( )->pauseEffect( soundEffectID );

// resumes a particular sound effect
CocosDenshion::SimpleAudioEngine::getInstance( )->resumeEffect( soundEffectID );

// stops all sound effects
CocosDenshion::SimpleAudioEngine::getInstance( )->stopAllEffect( soundEffectID );

// pauses all sound effects
CocosDenshion::SimpleAudioEngine::getInstance( )->pauseAllEffect( soundEffectID );

// resumes all sound effects
CocosDenshion::SimpleAudioEngine::getInstance( )->resumeAllEffect( soundEffectID );

// set the volume of all sound effects (ranges between 0 and 1)
CocosDenshion::SimpleAudioEngine::getInstance( )->setEffectsVolume( 0.25 );
// unload the sound file from memory when it no longer needs to be used
cc.audioEngine.playEffect( "audioFilePath" );

// the second parameter states whether or not the sound effect is played repeatedly, it is an optional parameter
cc.audioEngine.playEffect( "audioFilePath" );

// stops a particular sound effect
cc.audioEngine.stopEffect->stopEffect( soundEffectID );

// pauses a particular sound effect
cc.audioEngine.pauseEffect( soundEffectID );

// resumes a particular sound effect
cc.audioEngine.resumeEffect( soundEffectID );

// stops all sound effects
cc.audioEngine.stopAllEffect( soundEffectID );

// pauses all sound effects
cc.audioEngine.pauseAllEffect( soundEffectID );

// resumes all sound effects
cc.audioEngine.resumeAllEffect( soundEffectID );

// set the volume of all sound effects (ranges between 0 and 1)
cc.audioEngine.setEffectsVolume( 0.25 );

Note: ThesoundEffectID 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

// 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
CocosDenshion::SimpleAudioEngine::getInstance( )->preloadBackgroundMusic( "audioFilePath" );

// the second parameter states whether or not the music file is played repeatedly, it is an optional parameter
CocosDenshion::SimpleAudioEngine::getInstance( )->playBackgroundMusic( "audioFilePath", true );

// stops the background music
CocosDenshion::SimpleAudioEngine::getInstance( )->stopBackgroundMusic( );

// pause the background music
CocosDenshion::SimpleAudioEngine::getInstance( )->pauseBackgroundMusic( );

// resume the background music
CocosDenshion::SimpleAudioEngine::getInstance( )->resumeBackgroundMusic( );

// set the volume of the background music (ranges between 0 and 1)
CocosDenshion::SimpleAudioEngine::getInstance( )->setBackgroundMusicVolume( 0.25 );
// the second parameter states whether or not the music file is played repeatedly, it is an optional parameter
cc.audioEngine.playMusic( "audioFilePath", true );

// stops the background music
cc.audioEngine.stopMusic( );

// pause the background music
cc.audioEngine.pauseMusic( );

// resume the background music
cc.audioEngine.resumeMusic( );

// set the volume of the background music (ranges between 0 and 1)
cc.audioEngine.setMusicVolume( ( 0.25 );

To pause and resume music when changing apps open the AppDelegate.cpp file and modify these functions:

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    Director::getInstance()->stopAnimation();

    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
// include this at the top of the time
#include "SimpleAudioEngine.h"
  
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    Director::getInstance()->stopAnimation();

    // if you use SimpleAudioEngine, it must be pause
    CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
   	CocosDenshion::SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}