Cocos2d-x elementary school (15)-> background music and sound effects

Source: Internet
Author: User

Reprinted! Please indicate the source when reprint: http://write.blog.csdn.net/postedit/8140083

First of all, I am sorry to everyone here. I have prepared the action editor for this lecture, but there is really no good action editor on the Internet. spritex can barely join in, however, I only have the Java parsing version. If I have time, I will parse it and share it with you ~

A game with no sound will make the game less attractive. For some classic games, we will know what the game is when we hear the sound... this is the sound effect that the voice leaves on players.

There are two types of sounds in the game: background music and sound effects.

The following describes how to add a sound and how to control the sound.

Step 1: pre-load background music and sound effects

// Pre-load background music and sound effects: files in relative paths such as .midand .wav

Simpleaudioengine: sharedengine ()-> preloadbackgroundmusic (ccfileutils: sharedfileutils ()-> fullpathfromrelativepath (music_file ));
Simpleaudioengine: sharedengine ()-> preloadeffect (ccfileutils: sharedfileutils ()-> fullpathfromrelativepath (effect_file ));

Step 2: control the sound

1. Background Music control method:

// Play background music

// Music_file is the file name in the relative path of the music file to the resources folder, and then converted to the absolute path. The second parameter is whether to play cyclically.
Simpleaudioengine: sharedengine ()-> playbackgroundmusic (STD: string (ccfileutils: sharedfileutils ()-> fullpathfromrelativepath (music_file). c_str (), true );

// Stop playing background music
Simpleaudioengine: sharedengine ()-> stopbackgroundmusic ();

// Pause playing background music
Simpleaudioengine: sharedengine ()-> pausebackgroundmusic ();

// Continue playing background music
Simpleaudioengine: sharedengine ()-> resumebackgroundmusic ();

// Replay the background music.
Simpleaudioengine: sharedengine ()-> rewindbackgroundmusic ();

// Whether the background music is playing

Simpleaudioengine: sharedengine ()-> isbackgroundmusicplaying ()

// Increase the background music volume
Simpleaudioengine: sharedengine ()-> setbackgroundmusicvolume (simpleaudioengine: sharedengine ()-> getbackgroundmusicvolume () + 0.1f );

// Reduce the background music volume
Simpleaudioengine: sharedengine ()-> setbackgroundmusicvolume (simpleaudioengine: sharedengine ()-> getbackgroundmusicvolume ()-0.1f );

2. Sound Control Method:

// Play sound 1
M_nsoundid = simpleaudioengine: sharedengine ()-> playeffect (STD: string (ccfileutils: sharedfileutils ()-> fullpathfromrelativepath (effect_file). c_str ());

// Play sound 2
M_nsoundid = simpleaudioengine: sharedengine ()-> playeffect (STD: string (ccfileutils: sharedfileutils ()-> fullpathfromrelativepath (effect_file). c_str (), true );

// Stop playing sound effects (Note: If no playback is available, do not stop, because no playback is available. Why do you stop ~ The same is true for other processing)
Simpleaudioengine: sharedengine ()-> stopeffect (m_nsoundid );

// Pause audio playback
Simpleaudioengine: sharedengine ()-> pauseeffect (m_nsoundid );

// Resume audio playback
Simpleaudioengine: sharedengine ()-> resumeeffect (m_nsoundid );

// Pause all sound effects
Simpleaudioengine: sharedengine ()-> pausealleffects ();

// Continue all sound effects
Simpleaudioengine: sharedengine ()-> resumealleffects ();

// Stop all sound effects
Simpleaudioengine: sharedengine ()-> stopalleffects ();

// Uninstall sound effects
Simpleaudioengine: sharedengine ()-> unloadeffect (STD: string (ccfileutils: sharedfileutils ()-> fullpathfromrelativepath (effect_file). c_str ());

// Increase the sound volume
Simpleaudioengine: sharedengine ()-> setjavastsvolume (simpleaudioengine: sharedengine ()-> getjavastsvolume () + 0.1f );

// Reduce the sound volume
Simpleaudioengine: sharedengine ()-> setjavastsvolume (simpleaudioengine: sharedengine ()-> getjavastsvolume ()-0.1f );

Step 3: Release the sound

Simpleaudioengine: sharedengine ()-> end ();

I wrote 14 articles. If I didn't want to create a physical engine game, I read these 14 articles. It would be okay to develop the game. I will certainly encounter a memory control problem on the way, I will talk about it later...

Get started with cocos2dx Game Development

Haha ~ If you have not added a game sound, add a sound to your game! Make him more perfect

Thank you for reading my blog and hope you can see your footprints!
If something is wrong or incorrect,I also hope you can point it out!

 

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.