Cocos2d-x学习笔记(十)—— 音频系统(SimpleAudioEngine)

前端之家收集整理的这篇文章主要介绍了Cocos2d-x学习笔记(十)—— 音频系统(SimpleAudioEngine)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Cocos2d音频系统主要用于播放游戏中的声音文件,由于声音文件的大小可分为音乐和音效两种。其中,音乐一般为游戏中的背景音乐,一般比较长,而音效一般是短暂的声音,比如爆炸的声音等,根据不同的大小,我们使用不同的方法加载音效,以提高运行效率。

直接上代码,新建HelloAudio项目:

HelloAudio.h:

#ifndef __HELLOAUdio_H__
#define __HELLOAUdio_H__

#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "Box2D\Box2D.h"

using namespace cocos2d;
using namespace CocosDenshion;

class HelloAudio : public cocos2d::Layer
{
private:
	// 菜单
	Menu* _menu;
	// 菜单项数
	int m_count;
	// 鼠标点击时的坐标
	CCPoint m_BeginPos;
	// 声音ID
	int sound_Id;

public:
	static cocos2d::Scene* createMusicScene();

	virtual bool init();

	void menuMusicCallBack(Ref* pSender);

	CREATE_FUNC(HelloAudio);

	void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches,cocos2d::Event* event);
	void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches,cocos2d::Event* event);

	virtual void onExit();
};

#endif // !__HELLOAUdio_H__

HelloAudio.cpp:

#include "HelloAudio.h"

USING_NS_CC;

// 定义Label的行间隔
#define LineSize 35

Scene* HelloAudio::createMusicScene()
{
	auto musicScene = Scene::create();
	auto musicLayer = HelloAudio::create();
	musicScene->addChild(musicLayer);

	return musicScene;
}

bool HelloAudio::init()
{
	if (!Layer::init())
	{
		return false;
	}

	// 设置标签数组
	std::string selectItem[] = {
		"play background music","stop background music","pause background music","resume background music","rewind background music","is background music playing","play effect","play effect repeatly","stop effect","unload effect","add background music volume","sub background music volume","add effect volume","sub effect volume","pause effect","resume effect","pause all effect","stop all effect","resume all effect"
	};

	_menu = Menu::create();
	// 获得标签数
	m_count = sizeof(selectItem) / sizeof(selectItem[0]);

	// 创建并为每个标签设置内容
	for (int i = 0; i < m_count; i++)
	{
		auto ttf = LabelTTF::create(selectItem[i].c_str(),"arial.ttf",25);
		// 添加标签菜单项,并调用回调函数
		auto menuItemLabel = MenuItemLabel::create(ttf,this,menu_selector(HelloAudio::menuMusicCallBack));

		// 将菜单项和索引添加菜单
		_menu->addChild(menuItemLabel,i + 10000);
		// 设置菜单项在菜单中的位置
		menuItemLabel->setPosition(240,320 - (i + 1) * LineSize);
	}
	// 设置菜单的大小范围
	_menu->setContentSize(CCSizeMake(480,(m_count + 1) * LineSize));
	_menu->setPosition(0,0);
	this->addChild(_menu);

	// 设置是否接受触碰
	setTouchEnabled(true);

	// 预先加载背景音乐和音效
	SimpleAudioEngine::getInstance()->preloadBackgroundMusic(
		FileUtils::getInstance()->fullPathForFilename("/music/ZARD.mp3").c_str());
	SimpleAudioEngine::getInstance()->preloadEffect(
		FileUtils::getInstance()->fullPathForFilename("/music/CANNON3.WAV").c_str());

	// 预先设置背景音乐和音效的音量
	SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.5f);
	SimpleAudioEngine::getInstance()->setEffectsVolume(0.5f);

	return true;
}

void HelloAudio::menuMusicCallBack(Ref* pSender)
{
	// 获取菜单项,并通过索引获取Id
	auto menuItem = (MenuItem*)pSender;
	int Id = menuItem->getZOrder() - 10000;

	switch (Id)
	{
	case 0:
		// 播放背景音乐
		SimpleAudioEngine::getInstance()->playBackgroundMusic(
			FileUtils::getInstance()->fullPathForFilename("/music/ZARD.mp3").c_str(),true);
		break;
	case 1:
		// 停止背景音乐
		SimpleAudioEngine::getInstance()->stopBackgroundMusic();
		break;
	case 2:
		// 暂停背景音乐
		SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
		break;
	case 3:
		// 继续播放背景音乐
		SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
		break;
	case 4:
		// 重新播放背景音乐
		SimpleAudioEngine::getInstance()->rewindBackgroundMusic();
		break;
	case 5:
		// 判断是否正在播放背景音乐
		if (SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying())
		{
			log("backgroup music is playing");
		} 
		else
		{
			log("backgroup music is not playing");
		}
		break;
	case 6:
		sound_Id = SimpleAudioEngine::getInstance()->playEffect(
			FileUtils::getInstance()->fullPathForFilename("/music/CANNON3.WAV").c_str(),false);
		break;
	case 7:
		sound_Id = SimpleAudioEngine::getInstance()->playEffect(
			FileUtils::getInstance()->fullPathForFilename("/music/CANNON3.WAV").c_str(),true);
		break;
	case 8:
		SimpleAudioEngine::getInstance()->stopEffect(sound_Id);
		break;
	case 9:
		// 卸载音效
		SimpleAudioEngine::getInstance()->unloadEffect(
			FileUtils::getInstance()->fullPathForFilename("/music/CANNON3.WAV").c_str());
		break;
	case 10:
		// 增大背景音乐音量
		SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(
			SimpleAudioEngine::getInstance()->getBackgroundMusicVolume() + 0.1f);
		break;
	case 11:
		// 降低背景音乐音量
		SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(
			SimpleAudioEngine::getInstance()->getBackgroundMusicVolume() - 0.1f);
		break;
	case 12:
		SimpleAudioEngine::getInstance()->setEffectsVolume(
			SimpleAudioEngine::getInstance()->getEffectsVolume() + 0.1f);
		break;
	case 13:
		SimpleAudioEngine::getInstance()->setEffectsVolume(
			SimpleAudioEngine::getInstance()->getEffectsVolume() - 0.1f);
		break;
	case 14:
		SimpleAudioEngine::getInstance()->pauseEffect(sound_Id);
		break;
	case 15:
		SimpleAudioEngine::getInstance()->resumeEffect(sound_Id);
		break;
	case 16:
		// 暂停所有音效
		SimpleAudioEngine::getInstance()->pauseAllEffects();
		break;
	case 17:
		SimpleAudioEngine::getInstance()->stopAllEffects();
		break;
	case 18:
		SimpleAudioEngine::getInstance()->resumeAllEffects();
		break;
	}
}

void HelloAudio::onTouchesBegan(const std::vector<cocos2d::Touch*>& touches,cocos2d::Event* event)
{
	// 由于win32不存在多点触控,所以取第一次点击,并获取第一次点击的位置
	auto touch = touches[0];
	m_BeginPos = touch->getLocation();
}

void HelloAudio::onTouchesMoved(const std::vector<cocos2d::Touch*>& touches,cocos2d::Event* event)
{
	auto touch = touches[0];

	// 获得移动后的位置
	auto touchLocal = touch->getLocation();
	// 获得移动后与移动前的Y轴位置变化
	float moveY = touchLocal.y - m_BeginPos.y;

	// 获得菜单的当前坐标
	auto curPos = _menu->getPosition();
	// 设置获得移动后菜单的坐标
	auto nextPos = Vec2(curPos.x,curPos.y + moveY);

	// 设置菜单的顶部和底部
	if (nextPos.y < 0.0f)
	{
		_menu->setPosition(0,0);
		return;
	}
	if (nextPos.y > ((m_count + 1)*LineSize - 320))
	{
		_menu->setPosition(0,(m_count + 1)*LineSize - 320);
		return;
	}

	_menu->setPosition(nextPos);

	// 将移动后的位置设置为开始位置
	m_BeginPos = touchLocal;
}

void HelloAudio::onExit()
{
	// 退出图层
	Layer::onExit();
	// 停止音乐的播放
	SimpleAudioEngine::getInstance()->end();
}

由于项目文件过大,这里只上传代码文件http://download.csdn.net/detail/u013707014/9009295

猜你在找的Cocos2d-x相关文章