Cocos2d-x background music and sound effects using examples _c language

Source: Internet
Author: User
Tags addchild win32

Music and sound in the game are essential, good music can leave us a deep impression also determines the quality of the game. Today we'll learn how to use music and sound effects in cocos2d-x. The functions used are simpler and are now directly on the code.

The header file for the sound pre-processing class
#ifndef _audio_pretreat_h
#define _AUDIO_PRETREAT_H
#include "cocos2d.h"
// Use of the sound engine must contain the SimpleAudioEngeim.h header file
#include "SimpleAudioEngine.h"

using namespace cocos2d;
The name space of the sound engine is
using namespace cocosdenshion;

Class Audiopretrea Adopt single example design pattern

//Use music as enum type
enum Music_type
{
	music_type_background/background music
} ;

Use the music as enum type
enum Effect_type
{
	effect_type_effect1
};

Class Audiopretreat
{
private:
	static audiopretreat * m_audiopretreat;
Public:
	//single case design need to use two functions
	static audiopretreat * Sharedaudiopretreat ();
	static void Freeaudiopretreat ();
	Different sound files can be obtained based on different platforms
	char * GETMUSIC (Music_type TYPE);
	char * geteffect (Effect_type TYPE);
	Pre-loaded BOOL Init () for completing sound in Init
	();

#endif
The implementation of the sound pre-processing class #include the idiomatic way of "AudioPretreat.h"/"Single example design pattern" Audiopretreat * audiopretreat::m_audiopretreat = NULL; Audiopretreat * Audiopretreat::sharedaudiopretreat () {if (NULL = = m_audiopretreat) {m_audiopretreat = new AudioPretre
	at ();
return m_audiopretreat;
		This function is called at the time of appdelegate destructor void Audiopretreat::freeaudiopretreat () {if (m_audiopretreat) {delete m_audiopretreat;
	M_audiopretreat = NULL;
		}////depending on the platform for different sound files char * audiopretreat::getmusic (music_type TYPE) {switch (type) {case Music_type_background:
Win32,android,ios all use the MP3 format background music #if (cc_target_platform = = Cc_platform_win32) return "Background-music.mp3";
#else (Cc_target_platform = = cc_platform_android) return "Background-music.mp3";
	#endif break;
return NULL; ///Depending on the platform for different sound effects file char * Audiopretreat::geteffect (effect_type TYPE) {switch (type) {case EFFECT_TYPE_EFFECT1: #if (
Cc_target_platform = = Cc_platform_win32) return "Pew-pew-lei.wav"; #else (Cc_target_platform = = Cc_platform_android) return "Pew-pew-lei.ogg";
#endif} return NULL;
	Perform some initialization operations in the//INIT function bool Audiopretreat::init () {bool BRet = false; Do {//preload sound, parameter is sound file Simpleaudioengine::sharedengine ()->preloadbackgroundmusic (this->getmusic) (MUSIC_TYPE_
		BACKGROUND));
		Load Sound simpleaudioengine::sharedengine ()->preloadeffect (This->geteffect (Effect_type_effect1));
		Set the size of the sound 0.0-1.0 Simpleaudioengine::sharedengine ()->setbackgroundmusicvolume (0.1f);
	BRet = true;

	while (0);
return bRet; }
Let's take a look at how to use the Sound engine bool Helloworld::init () {bool BRet = false; do {cc_break_if (!

		Cclayer::init ());

		Ccsize size = Ccdirector::shareddirector ()->getvisiblesize ();

		Audiopretreat::sharedaudiopretreat ()->init ();
		Here are some action//setting strings for background music, and transcoding std::string str1 = "Play background music";
		std::string str2 = "Stop background music";
		std::string STR3 = "Pause background music";
		std::string STR4 = "Resume background music";
		std::string STR5 = "Play back the background music";
		std::string STR6 = "background music: Add";
		std::string STR7 = "background music: minus";
		GBKToUTF8 (str1, "GB2312", "UTF-8");
		GBKToUTF8 (str2, "GB2312", "UTF-8");
		GBKToUTF8 (STR3, "GB2312", "UTF-8");
		GBKToUTF8 (STR4, "GB2312", "UTF-8");
		GBKToUTF8 (STR5, "GB2312", "UTF-8");
		GBKToUTF8 (STR6, "GB2312", "UTF-8");

		GBKToUTF8 (STR7, "GB2312", "UTF-8");
		Ccmenuitemfont::setfontsize (24);
		Ccmenuitemfont::setfontname ("Arial");
		Ccmenuitemfont * menu1 = Ccmenuitemfont::create (Str1.c_str (), This,menu_selector (Helloworld::menu));
		Menu1->settag (1); Ccmenuitemfont * menu2 = ccmenuitemfont::create (str2.c_str(), This,menu_selector (Helloworld::menu));
		Menu2->settag (2);
		Ccmenuitemfont * menu3 = Ccmenuitemfont::create (Str3.c_str (), This,menu_selector (Helloworld::menu));
		Menu3->settag (3);
		Ccmenuitemfont * Menu4 = Ccmenuitemfont::create (Str4.c_str (), This,menu_selector (Helloworld::menu));
		Menu4->settag (4);
		Ccmenuitemfont * Menu5 = Ccmenuitemfont::create (Str5.c_str (), This,menu_selector (Helloworld::menu));
		Menu5->settag (5);
		Ccmenuitemfont * Menu6 = Ccmenuitemfont::create (Str6.c_str (), This,menu_selector (Helloworld::menu));
		Menu6->settag (6);
		Ccmenuitemfont * Menu7 = Ccmenuitemfont::create (Str7.c_str (), This,menu_selector (Helloworld::menu));

		Menu7->settag (7);
		Ccmenu * Leftmenu = ccmenu::create (menu1,menu2,menu3,menu4,menu5,menu6,menu7,null);
		Leftmenu->alignitemsverticallywithpadding (5);
		Leftmenu->setpositionx (SIZE.WIDTH/4);

		This->addchild (Leftmenu);
		The following is the operation of the sound effects std::string str11 = "Play sound effects";
		std::string str12 = "Stop sound effect"; std::string Str13 = "Pause sound Effects ";
		std::string str14 = "restore sound effect";
		std::string str15 = "Remove sound from cache";
		std::string str16 = "Sound effects: Add";
		std::string str17 = "sound effect: minus";
		GBKToUTF8 (Str11, "GB2312", "UTF-8");
		GBKToUTF8 (Str12, "GB2312", "UTF-8");
		GBKToUTF8 (Str13, "GB2312", "UTF-8");
		GBKToUTF8 (Str14, "GB2312", "UTF-8");
		GBKToUTF8 (Str15, "GB2312", "UTF-8");
		GBKToUTF8 (str16, "GB2312", "UTF-8");

		GBKToUTF8 (Str17, "GB2312", "UTF-8");
		Ccmenuitemfont * Menu11 = Ccmenuitemfont::create (Str11.c_str (), This,menu_selector (Helloworld::menu));
		Menu11->settag (11);
		Ccmenuitemfont * Menu12 = Ccmenuitemfont::create (Str12.c_str (), This,menu_selector (Helloworld::menu));
		Menu12->settag (12);
		Ccmenuitemfont * menu13 = Ccmenuitemfont::create (Str13.c_str (), This,menu_selector (Helloworld::menu));
		Menu13->settag (13);
		Ccmenuitemfont * menu14 = Ccmenuitemfont::create (Str14.c_str (), This,menu_selector (Helloworld::menu));
		Menu14->settag (14); Ccmenuitemfont * menu15 = Ccmenuitemfont::create (Str15.c_str (), This,menu_selector (helloworlD::menu));
		Menu15->settag (15);
		Ccmenuitemfont * menu16 = Ccmenuitemfont::create (Str16.c_str (), This,menu_selector (Helloworld::menu));
		Menu16->settag (16);
		Ccmenuitemfont * Menu17 = Ccmenuitemfont::create (Str17.c_str (), This,menu_selector (Helloworld::menu));

		Menu17->settag (17);
		Ccmenu * Rightmenu = ccmenu::create (menu11,menu12,menu13,menu14,menu15,menu16,menu17,null);
		Rightmenu->alignitemsverticallywithpadding (5);
		Rightmenu->setpositionx (SIZE.WIDTH*3/4);

    This->addchild (Rightmenu);
  BRet = true;

  while (0);
return bRet;
	} void Helloworld::menu (Ccobject * sender) {Ccnode * node = (Ccnode *) sender;
	int tag = Node->gettag (); Switch (TAG) {Case 1://play sound, and the second parameter indicates whether to loop Simpleaudioengine::sharedengine ()->playbackgroundmusic (audiopretre
		At::sharedaudiopretreat ()->getmusic (Music_type_background), true);
	Break
		Case 2:simpleaudioengine::sharedengine ()->stopbackgroundmusic ();
	Break Case 3://pause and resume used in pairs SimpleaUdioengine::sharedengine ()->pausebackgroundmusic ();
	Break
		Case 4:simpleaudioengine::sharedengine ()->resumebackgroundmusic ();
	Break
		Case 5:simpleaudioengine::sharedengine ()->rewindbackgroundmusic ();
	Break Case 6://This function can not change the size of the sound on the Win32, we use the F12 follow-up function when the inside is empty, but on the Android platform has the implementation of Simpleaudioengine::sharedengine ()->
		Setbackgroundmusicvolume (Simpleaudioengine::sharedengine ()->getbackgroundmusicvolume () +0.1f);
	Break Case 7://This function can not change the size of the sound on the Win32, we use the F12 follow-up function when the inside is empty, but on the Android platform has the implementation of Simpleaudioengine::sharedengine ()->
		Setbackgroundmusicvolume (Simpleaudioengine::sharedengine ()->getbackgroundmusicvolume () -0.1f);
	Break Case 11://The second parameter is set to True then loop, Soundid is defined in HelloWorld header file//The sound function differs from the background music by returning an id Soundid = simpleaudioengine::shared
		Engine ()->playeffect (Audiopretreat::sharedaudiopretreat ()->geteffect (Effect_type_effect1), true);
	Break Case 12://The sound function will mostly pass in an ID, because the sound effects in the game are more than one simpleaudioengine::sharedengine ()->stopeffect (sOundid);
	Break
		Case 13:simpleaudioengine::sharedengine ()->pauseeffect (Soundid);
	Break
	Case 14:simpleaudioengine::sharedengine ()->resumeeffect (Soundid); Case 15://removing sound effects from the cache, the name of the Simpleaudioengine::sharedengine ()->unloadeffect () is required to remove the sound effect (audiopretreat::sharedaudiopre
	Treat ()->geteffect (Effect_type_effect1)); Case 16:simpleaudioengine::sharedengine ()->seteffectsvolume (Simpleaudioengine::sharedengine ()->
		Geteffectsvolume () +0.1);
	Break Case 17:simpleaudioengine::sharedengine ()->seteffectsvolume (Simpleaudioengine::sharedengine ()->
		Geteffectsvolume ()-0.1);
	Break
	}//encoding format conversion function int GBKToUTF8 (std::string & gbkstr, const char* tocode, const char* fromcode) {iconv_t ICONVH;

	ICONVH = Iconv_open (Fromcode, Tocode);
	if (ICONVH = = 0) {return-1;
	} const char* Strchar = Gbkstr.c_str ();
	Const char** PIN = &strChar;
	size_t strlength = Gbkstr.length ();
	char* outbuf = (char*) malloc (strlength*4);
char* pbuff = outbuf;
	memset (outbuf, 0, strlength*4);

	size_t outlength = strlength*4;
		if ( -1 = = Iconv (ICONVH, PIN, &strlength, &OUTBUF, &outlength)) {iconv_close (ICONVH);
	return-1;
	} gbkstr = Pbuff;

	Iconv_close (ICONVH);
return 0; }

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.