Cocos2D encapsulates music/sound effects. The following is an example:
Define two files:
[Cpp]
# Define MUSIC_FILE "lolbgmusic.pdf"
# Define EFFECT_FILE "Attack.wav"
# Define MUSIC_FILE "lolbgmusic.pdf"
# Define EFFECT_FILE "Attack.wav"
Pre-load the music/sound file during initialization:
[Cpp]
// Pre-load
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> preloadBackgroundMusic (MUSIC_FILE );
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> preloadEffect (EFFECT_FILE );
// Set the volume
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> setjavastsvolume (0.5 );
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> setBackgroundMusicVolume (0.5 );
// Pre-load
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> preloadBackgroundMusic (MUSIC_FILE );
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> preloadEffect (EFFECT_FILE );
// Set the volume
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> setjavastsvolume (0.5 );
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> setBackgroundMusicVolume (0.5 );
Play:
[Cpp]
// Cyclically play background music,
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> playBackgroundMusic (MUSIC_FILE, true );
// Cyclically play background music,
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> playBackgroundMusic (MUSIC_FILE, true); [cpp] view plaincopyprint? // Play Sound Effects
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> playEffect (EFFECT_FILE );
// Play Sound Effects
CocosDenshion: SimpleAudioEngine: sharedEngine ()-> playEffect (EFFECT_FILE );
Other functions:
[Cpp]
// Stop background music
SimpleAudioEngine: sharedEngine ()-> stopBackgroundMusic ();
// Pause background music
SimpleAudioEngine: sharedEngine ()-> pauseBackgroundMusic ();
// Resume background music
SimpleAudioEngine: sharedEngine ()-> resumeBackgroundMusic ();
// Rewind background music
SimpleAudioEngine: sharedEngine ()-> rewindBackgroundMusic ();
// Is background music playing
SimpleAudioEngine: sharedEngine ()-> isBackgroundMusicPlaying ()
// Add bakcground music volume
SimpleAudioEngine: sharedEngine ()-> setBackgroundMusicVolume (SimpleAudioEngine: sharedEngine ()-> getBackgroundMusicVolume () + 0.1f );
SimpleAudioEngine: sharedEngine ()-> pauseEffect (m_nSoundId );
SimpleAudioEngine: sharedEngine ()-> resumeEffect (m_nSoundId );
SimpleAudioEngine: sharedEngine ()-> pauseAllEffects ();
SimpleAudioEngine: sharedEngine ()-> resumeAllEffects ();
SimpleAudioEngine: sharedEngine ()-> stopAllEffects ();
// Stop background music
SimpleAudioEngine: sharedEngine ()-> stopBackgroundMusic ();
// Pause background music
SimpleAudioEngine: sharedEngine ()-> pauseBackgroundMusic ();
// Resume background music
SimpleAudioEngine: sharedEngine ()-> resumeBackgroundMusic ();
// Rewind background music
SimpleAudioEngine: sharedEngine ()-> rewindBackgroundMusic ();
// Is background music playing
SimpleAudioEngine: sharedEngine ()-> isBackgroundMusicPlaying ()
// Add bakcground music volume
SimpleAudioEngine: sharedEngine ()-> setBackgroundMusicVolume (SimpleAudioEngine: sharedEngine ()-> getBackgroundMusicVolume () + 0.1f );
SimpleAudioEngine: sharedEngine ()-> pauseEffect (m_nSoundId );
SimpleAudioEngine: sharedEngine ()-> resumeEffect (m_nSoundId );
SimpleAudioEngine: sharedEngine ()-> pauseAllEffects ();
SimpleAudioEngine: sharedEngine ()-> resumeAllEffects ();
SimpleAudioEngine: sharedEngine ()-> stopAllEffects ();