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 35Scene* 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学习笔记(十)—— 音频系统(SimpleAudioEngine)全部内容,希望文章能够帮你解决Cocos2d-x学习笔记(十)—— 音频系统(SimpleAudioEngine)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)