[Cocos2D-X] Getting a glimpse (9) playing music/sound effects

Source: Internet
Author: User

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 ();

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.