Cocos2d-x 3.0 background music and sound beginner mciplayer class
* *********************************** Please specify the source: bytes ********************************************
Statement:
This article is compiled with vs2012, cocos2d-x version 3.0.
I am also a newbie. If you have any mistakes, please point out and make progress together.
Hmm ~ This time I learned how to use background music and sound effects ~
Are you happy!
Create a project first ~ (What? How to create it?!..., go out and find it)
First of all, you need to know that the cocos2d-x for music, sound effects, and so on to set up a separate class, the connector file can be included -- simpleaudioengine. h
The member functions include:
-- Background Music:
// Pre-load background music simpleaudioengine: sharedengine ()-> preloadbackgroundmusic ("music/xxxx.mp3"); // start playing background music. True indicates loop simpleaudioengine: sharedengine () -> playbackgroundmusic ("music/xxxx.mp3", true); // stop the background music. This is a default parameter function. Passing a parameter indicates whether to release the music file simpleaudioengine: sharedengine () -> stopbackgroundmusic (); // pause background music simpleaudioengine: sharedengine ()-> pausebackgroundmusic (); // call background music simpleaudioengine: sharedengine () -> rewindbackgroundmusic (); // return the Boolean parameter, indicating whether the background music simpleaudioengine: sharedengine ()-> isbackgroundmusicplaying () // set the volume to 0.0-1.0 simpleaudioengine :: sharedengine ()-> setbackgroundmusicvolume (0.5 );
-- Sound effects
// Pre-loaded sound effects simpleaudioengine: sharedengine ()-> preloadeffect ("music/xxxx1_"); // start playing background sound effects. False indicates that simpleaudioengine: sharedengine () is not cyclically played () -> playeffect ("music/xxxxect", false); // you can choose to stop a sound effect independently. The value is simpleaudioengine: sharedengine () returned by playeffect () -> stopeffect (m_nsoundid); // stop all sound effects simpleaudioengine: sharedengine ()-> stopalleffects (); // pause a single sound effect simpleaudioengine: sharedengine () -> pauseeffect (m_nsoundid); // restart the sound effects simpleaudioengine: sharedengine ()-> resumeeffect (m_nsoundid); // pause all sound effects simpleaudioengine: sharedengine () -> pausealleffects (); // restart all sound effects simpleaudioengine: sharedengine ()-> resumealleffects (); // set the sound volume to 0.0-1.0 simpleaudioengine: sharedengine () -> seteffectsvolume (0.5); // uninstall simpleaudioengine: sharedengine ()-> unloadeffect ("music/xxxxect ");
For others, take a look at the API documentation.
Note that the simpleaudioengine. h header file is not used. When calling a function,
Cocosdenshion namespace is also required
using namespace CocosDenshion;
You can also add cocosdenshion before each simpleaudioengine ::
Add music. Add two buttons, one on/off button, one on/off button, and one sound button,
This blog post uses materials and code, which will be provided at the end of the article.
Switch button, indicating (a little simple, sorry ~.~) :
Definition of the switch button:
// The enable or disable button menuitemimage * _ turnon, * _ turnoff; _ turnon = menuitemimage: Create ("button_voi_on.png", "button_voi_on.png"); _ turnoff = menuitemimage :: create ("success", "success"); menuitemtoggle * toggleitem = menuitemtoggle: createwithcallback (cc_callback_1 (helloworld: menumusiccallback, this), _ turnon, _ turnoff, null ); toggleitem-> setscale (0.3f); toggleitem-> setposition (point (visiblesize. width/3, visiblesize. height/2 ));
It's easy to understand. Define two buttons. The two buttons are displayed during regular display and click.
Because it is a switch button, use menuitemtoggle to associate it. Do not forget to declare the callback function in. h.
In the following two rows, one is that my image resources are too large and reduced, and the other is the placement location.
There are also buttons for implementing sound effects. The music selected this time is the music of classic super Mary, and the sound effect is the one that eats life mushrooms.
Of course, big green mushrooms must be used for images ~
Add a small mushroom button:
// Add the mushroom button auto moguitem = menuitemimage: Create ("mogu_1.png", "mogu_2.png", cc_callback_1 (helloworld: menumogucallback, this )); moguitem-> setposition (point (visiblesize. width * 2/3, visiblesize. height/2 ));
Then, pre-load the background music and sound effects:
// Pre-load background music and sound effects simpleaudioengine: sharedengine ()-> preloadbackgroundmusic ("music/back_musicloud"); simpleaudioengine: sharedengine () -> preloadeffect ("music/eat_mogu.pdf ");
In fact, this step is fine, because we have very little content and can directly find it. It takes a little time.
However, if there will be more things in the future to avoid being stuck or slow, you need to pre-load them.
In init, first play the music:
// First, the background music simpleaudioengine is played by default: sharedengine ()-> playbackgroundmusic ("music/back_musicloud", true); // bool type: checks whether the background music is paused. ispause = false;
Ispause is defined in the header file. It is used to determine whether the music has been paused.
Two callback functions:
void HelloWorld::menuMusicCallback( cocos2d::Ref* pSender ){if( isPause==false ){SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();isPause=true;}else{SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();isPause=false;}}void HelloWorld::menuMoguCallback( cocos2d::Ref* pSender ){SimpleAudioEngine::sharedEngine()->playEffect("music/eat_mogu.mp3"); }
If you are careful enough, you will find that there are functions in simpleaudioengine to show whether the current background music is played:
// Return a Boolean parameter indicating whether the background music simpleaudioengine: sharedengine ()-> isbackgroundmusicplaying () is being played ()
Why is it necessary to set a variable for management?
I had a problem here, but I kept searching for it and found that,
<span style="font-size:18px;">bool SimpleAudioEngine::isBackgroundMusicPlaying(){ return sharedMusic().IsPlaying();}</span>
Continue to look forward:
bool MciPlayer::IsPlaying(){ return _playing;}
In the mciplayer class,
_ Playing: false is returned only when it is stopped; otherwise, true is always returned.
void MciPlayer::Stop(){ _SendGenericCommand(MCI_STOP); _playing = false;}
When stop is called, _ playing is always false and cannot be changed to true.
Therefore, the above program is not used as we think.
Solution: either change the mciplayer class, or use a bool variable like me.
Oh, by the way, there are also releases of Music:
SimpleAudioEngine::sharedEngine()->end();
The code will be done in this way,
You can run it to see,
Listen to the passionate music of super Mary.
Click it. The little green mushroom adds a life ~
Wow, it's now today.
Resources used in this blog post, code can be downloaded here → http://download.csdn.net/detail/lx417147512/7855969
* *********************************** Please specify the source: bytes ********************************************
Cocos2d-x 3.0 Background Music & Sound Effects