cocos2dx 音乐与音效
2013-04-13 19:49
295 查看
//根据不同的平台使用的预编译索引不同音频文件 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #define EFFECT_FILE "effect2.ogg" #elif (CC_TARCET_PLATFORM == CC_PLATFORM_MARMALADE) #define EFFECT_FILE "effect1.raw" #else #define EFFECT_FILE "effect1.wav" #endif #if (CC_TARGET_PLATEFOM == CC_PLATFORM_WIN32) #define MUSIC_FILE "music.mid" #elif (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY) #define MUSIC_FILE "background.ogg" #else #define MUSIC_FILE "background.mp3" #endif std::string items[]={ "播放背景音乐","停止背景音乐","暂停背景音乐","继续播放背景音乐","后退背景音乐", "背景音乐是否播放", "播放音效","重复播放音效","停止音效播放" ,"释放音效" ,"增加背景音乐音量", "减少背景音效音量","增加音效音量","减少音效音量","暂停音效","继续播放音效","暂停所有音效","继续所有音效","停止所有音效" }; CCScene* HelloWorld::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } // CCMenu * m_pItmeMenu = CCMenu::create(); CCSize size = CCDirector::sharedDirector()->getWinSize(); int m_nTestCount = sizeof(items)/sizeof(items[0]); for (int i = 0 ; i <m_nTestCount; i++) { CCLabelTTF * label = CCLabelTTF::create(items[i].c_str(), "", 20); CCMenuItemLabel * pMenuItem = CCMenuItemLabel::create(label , this, menu_selector(HelloWorld::menuBack)); m_pItmeMenu->addChild(pMenuItem,i); pMenuItem->setPosition(CCPointMake(0 , (size.height*0.5-20-(i+1)*15))); } addChild(m_pItmeMenu,0 ,100); // 预加载音乐和音效 SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE)); SimpleAudioEngine::sharedEngine()->preloadEffect(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)); // 设置默认音量 SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5); SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5); return true; } void HelloWorld:: menuBack(CCObject * pSender) { CCMenuItem * pMenuItem = (CCMenuItem * )(pSender); int nIdx = pMenuItem->getZOrder(); switch (nIdx) { case 0: // 播放背景音乐 SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE)).c_str(), true); break; case 1: // 停止背景音乐 SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(); break; case 2: // 暂停背景音乐 SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); break; case 3: // 继续播放背景音乐 SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); break; case 4: // 后退背景音乐 SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic(); break; case 5: // 背景音乐是否正在播放 if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()) { CCLOG("正在播放"); }else CCLOG("没在播放"); break; case 6: // 播放音效,并且得到此音效的ID SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE)).c_str()); break; case 7: // 重复播放音效 SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE)).c_str(), true); break; case 8: // 停止播放音效 SimpleAudioEngine::sharedEngine()->stopEffect(effectId); break; case 9: // 释放音效 SimpleAudioEngine::sharedEngine()->unloadEffect (std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE)).c_str() ); break; case 10: // 增加背景音乐音量 SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()+0.1f); break; case 11: // 减少背景音乐音量 SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()-0.1f); break; case 12: // 增加背景音效音量 SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume()+0.1f); break; case 13: // 减少背景音效音量 SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume()-0.1f); break; case 14: // 暂停音效 SimpleAudioEngine::sharedEngine()->pauseEffect(effectId); break; case 15: // 继续 播放音效 SimpleAudioEngine::sharedEngine()->resumeEffect(effectId); break; case 16: // 暂停所有音效 SimpleAudioEngine::sharedEngine()->pauseAllEffects(); break; case 17: // 继续所有音效 SimpleAudioEngine::sharedEngine()->resumeAllEffects(); break; case 18: // 停止所有音效 SimpleAudioEngine::sharedEngine()->stopAllEffects(); break; default: break; } }
相关文章推荐
- 和屌丝一起学cocos2dx-音乐音效
- 22、Cocos2dx 3.0游戏开发找小三之音乐与音效:假如世界上没有了音乐,你的耳朵会孤单吗?
- cocos2dx基础篇——音乐音效SimpleAudioEngine
- cocos2dX 之音乐与音效
- cocos2dx-音乐音效
- Cocos2dx 下面,对 音乐 和音效简单封装
- cocos2dx2.2添加音乐音效以及设置音量
- cocos2dX 音乐音效,就是声音啦
- cocos2dx基础篇(19)——音乐音效SimpleAudioEngine
- cocos2dx2.2添加音乐音效以及设置音量 (转)
- cocos2dx-音乐音效
- 22、Cocos2dx 3.0游戏开发找小三之音乐与音效:假如世界上没有了音乐,你的耳朵会孤单吗?
- cocos2dx基础篇——音乐音效SimpleAudioEngine
- cocos2dx2.2添加音乐音效以及设置音量
- 22、Cocos2dx 3.0游戏开发找小三之音乐与音效:假如世界上没有了音乐,你的耳朵会孤单吗?
- cocos2D-x之音乐与音效
- Android设备 cocos2dx 骨骼动画注册事件播放音效,退到后台再返回黑屏问题
- 播放器 9.0以前的视屏播放 AVFoundation音乐播放 AudioToolbox音效播放
- cocos2d-x 音乐与音效
- 游戏音乐音效配音制作