Cocos2dX music and sound effects

Source: Internet
Author: User

Today, let's learn aboutMusicAndSound EffectsWhy do you like watching silent movies? (Narrator: I like playing with my voice closed). Let's leave it alone. She's a madman.


Old Rules: Prepare resources:

A piece of <Forbidden City Shensi> is used as the background music, saying that I like this kind of tune-up most. I use hitbeats as the background sound, but I haven't found any other formats for a long time, so I will use mp3, hey



Well, by the way, let's take a look at the differences between music and sound effects:

Music is generally a song, used as the background sound, the sound effect is generally relatively short, used as a short promotion sound when fighting effect or when pressed

Let's take a look at the common functions:


MUSIC:

PreloadBackgroundMusic (music file); // load music

PlayBackgroundMusic (music file, bool); // play music. true indicates loop playback, and false indicates non-loop playback.

PauseBackgroundMusic () // pause

ResumeBackgroundMusic () // continue

StopBackgroundMusic () // stop

RewindBackgroundMusic () // playback from scratch

IsBackgroundMusicPlaying () // whether the video is being played

SetBackgroundMusicVolume () // sets the volume.

GetBackgroundMusicVolume () // gets the volume


Sound EFFECT:

Well, this is similar to music. Can I list it?

Click "list". I will not comment it out. It should be okay.

PreloadEffect (sound file)

UnloadEffect (sound file)

PlayEffect (sound file, repeated)

StopEffect (sound effect ID)

StopAllEffects ()

PauseEffect (sound effect ID)

PauseAllEffects ()

ResumeEffect (sound effect ID)

ResumeAllEffects ()

GetEffectsVolume ()

SetEffectsVolume ()


Well, it looks like it's almost the same. Let's start to write the code:

Header file. We write several callback functions to control music:

Void playMusicCallback (CCObject * pSender); // play the background music void pauseMusicCallback (CCObject * pSender); // pause the background music void recoverMusicCallback (CCObject * pSender ); // restore the background music void stopMusicCallback (CCObject * pSender); // stop the background music



Cpp file:

We create four menu buttons to control the sound:

// Create four menu buttons CCMenuItemFont * playBG = Operator: create ("play", this, menu_selector (HelloWorld: playMusicCallback); CCMenuItemFont * pauseBG = CCMenuItemFont :: create ("pause", this, menu_selector (HelloWorld: pauseMusicCallback); CCMenuItemFont * recoverBG = CCMenuItemFont: create ("recover", this, menu_selector (HelloWorld :: recoverMusicCallback); optional * stopBG = CCMenuItemFont: create ("stop", this, menu_selector (HelloWorld: stopMusicCallback); CCMenu * pMenu2 = CCMenu: create (playBG, pauseBG, recoverBG, stopBG, NULL); pMenu2-> setPosition (ccp (visibleSize. width/2, visibleSize. height/2); pMenu2-> alignItemsVertically (); addChild (pMenu2 );



Load the music file and set the default volume:

SimpleAudioEngine: sharedEngine ()-> preloadBackgroundMusic ("palacemoriesmusic"); // loads background music SimpleAudioEngine: sharedEngine ()-> setBackgroundMusicVolume (1.0); // sets the Default background volume.



Let's implement four callback functions:

Play the background music, pause the background music, resume the background music, and stop the background music. Well, each callback function also has a sound effect. You can click the menu to play it once.

Void HelloWorld: playMusicCallback (CCObject * pSender) {SimpleAudioEngine: sharedEngine ()-> playBackgroundMusic ("palacemoriesmusic"); {// play sound SimpleAudioEngine: sharedEngine () -> playEffect ("hitrate") ;}} void HelloWorld: pauseMusicCallback (CCObject * pSender) {SimpleAudioEngine: sharedEngine ()-> pauseBackgroundMusic (); {// playing sound SimpleAudioEngine: sharedEngine ()-> playEffect ("hitrate") ;}} void HelloWorld: recoverMusicCallback (CCObject * pSender) {SimpleAudioEngine: sharedEngine () -> resumeBackgroundMusic (); {// play SimpleAudioEngine: sharedEngine ()-> playEffect ("hitrate") ;}} void HelloWorld: stopMusicCallback (CCObject * pSender) {SimpleAudioEngine: sharedEngine ()-> stopBackgroundMusic (); {// play sound effects SimpleAudioEngine: sharedEngine ()-> playEffect ("hitrate ");}}



Let's take a look at the effect:


In fact, I can't see any effect. I only have to look at the volume mark in the lower right corner to prove that she is playing. Today's code is very simple. I hope you can write it and see the effect, if you do not understand it, you can go to the Group for discussion:223856254

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.