Study Notes on cocos2d-x (24)-music Sound Effects

Source: Internet
Author: User

Small man (Bill man) Personal Original, welcome to reprint, reprint please indicate the address, small man (Bill man) column address http://blog.csdn.net/bill_man

In the game, music is an indispensable part. Music is divided into two types: long-time background music and short sound effects, which can increase the effect for our game. Cocos2d-x supports multiple music formats MP3, WAV and other music formats

1. Background Music

To use a music, you must first pre-load the music. The pre-load method is as follows:

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

Music_file is the relative path of the music file to the res folder.

Then you can play the video. The playback method is as follows:

Simpleaudioengine: sharedengine ()-> playbackgroundmusic (STD: string (ccfileutils: fullpathfromrelativepath (music_file). c_str (), true );

In the first parameter, music_file is the relative path of the music file to the res folder, and then converted to the absolute path. The second parameter is whether to play cyclically. Because it is background music, we set it to true.

Other functions

Simpleaudioengine: sharedengine ()-> stopbackgroundmusic (); // stop the background music. A boolean parameter can be used to indicate whether to release the music file.

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

Simpleaudioengine: sharedengine ()-> rewindbackgroundmusic (); // call background music again

Simpleaudioengine: sharedengine ()-> isbackgroundmusicplaying () // return the Boolean parameter to check whether background music is playing.

Simpleaudioengine: sharedengine ()-> setbackgroundmusicvolume (0.5); // set the volume to 0.0-1.0

2. Sound Effects

The usage of sound effects is similar to that of music and requires pre-loading. The usage is as follows:

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

Effect_file is the relative path of the music to the res folder.

Then play the video.

M_nsoundid = simpleaudioengine: sharedengine ()-> playeffect (STD: string (ccfileutils: fullpathfromrelativepath (effect_file). c_str ());

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

Like background music, the second parameter is optional. If it is true, it means loop playback; otherwise, it means playing only once.

Other functions

Simpleaudioengine: sharedengine ()-> stopeffect (m_nsoundid); // stop the sound effect. You can choose to stop a sound effect independently and stop it with the m_nsoundid when creating the sound.

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

Simpleaudioengine: sharedengine ()-> pauseeffect (m_nsoundid); // pause a single sound effect

Simpleaudioengine: sharedengine ()-> resumeeffect (m_nsoundid); // start the sound effect again

Simpleaudioengine: sharedengine ()-> pausealleffects (); // pause all audio effects

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

Simpleaudioengine: sharedengine ()-> seteffectsvolume (0.5); // set the sound volume

Simpleaudioengine: sharedengine ()-> unloadeffect (STD: string (ccfileutils: fullpathfromrelativepath (effect_file). c_str (); // uninstall the sound effect

Remember that simpleaudioengine: sharedengine ()-> end (); release sharedengine ()

I just started to study this engine. If there are any errors, I hope you can correct them more.

Next, write down the ccrendertexture

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.